3. Content Categorization
Automatically categorize articles and posts by semantic similarity.
1import { Router, Route, OpenAIEncoder } from 'semantic-node-router';
2
3// Define content categories
4const routes = [
5 new Route({
6 name: 'technology',
7 utterances: [
8 'latest smartphone releases',
9 'AI breakthrough',
10 'new software update',
11 'coding tutorial',
12 'tech startup news'
13 ]
14 }),
15 new Route({
16 name: 'sports',
17 utterances: [
18 'football match results',
19 'Olympics highlights',
20 'basketball playoffs',
21 'tennis tournament',
22 'athlete interview'
23 ]
24 }),
25 new Route({
26 name: 'business',
27 utterances: [
28 'stock market update',
29 'company earnings',
30 'merger announcement',
31 'economic forecast',
32 'startup funding'
33 ]
34 }),
35 new Route({
36 name: 'entertainment',
37 utterances: [
38 'movie review',
39 'new album release',
40 'celebrity news',
41 'TV show premiere',
42 'concert tour'
43 ]
44 })
45];
46
47const encoder = new OpenAIEncoder({
48 apiKey: process.env.OPENAI_API_KEY
49});
50
51const router = new Router({ routes, encoder });
52await router.initialize();
53
54// Categorize articles
55async function categorizeArticle(title: string, content: string) {
56 const text = `${title}. ${content.substring(0, 500)}`;
57 const result = await router.route(text);
58
59 return {
60 category: result.route || 'uncategorized',
61 confidence: result.score
62 };
63}
64
65// Example usage
66const article = {
67 title: 'Apple Announces New iPhone with AI Features',
68 content: 'Apple unveiled its latest iPhone model today...'
69};
70
71const category = await categorizeArticle(article.title, article.content);
72console.log(`Category: ${category.category} (${(category.confidence * 100).toFixed(0)}% confident)`);
73// Category: technology (94% confident)