@rankcli/agent-runtime 0.0.1

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (178) hide show
  1. package/README.md +242 -0
  2. package/dist/analyzer-2CSWIQGD.mjs +6 -0
  3. package/dist/chunk-YNZYHEYM.mjs +774 -0
  4. package/dist/index.d.mts +4012 -0
  5. package/dist/index.d.ts +4012 -0
  6. package/dist/index.js +29672 -0
  7. package/dist/index.mjs +28602 -0
  8. package/package.json +53 -0
  9. package/scripts/build-deno.ts +134 -0
  10. package/src/audit/ai/analyzer.ts +347 -0
  11. package/src/audit/ai/index.ts +29 -0
  12. package/src/audit/ai/prompts/content-analysis.ts +271 -0
  13. package/src/audit/ai/types.ts +179 -0
  14. package/src/audit/checks/additional-checks.ts +439 -0
  15. package/src/audit/checks/ai-citation-worthiness.ts +399 -0
  16. package/src/audit/checks/ai-content-structure.ts +325 -0
  17. package/src/audit/checks/ai-readiness.ts +339 -0
  18. package/src/audit/checks/anchor-text.ts +179 -0
  19. package/src/audit/checks/answer-conciseness.ts +322 -0
  20. package/src/audit/checks/asset-minification.ts +270 -0
  21. package/src/audit/checks/bing-optimization.ts +206 -0
  22. package/src/audit/checks/brand-mention-optimization.ts +349 -0
  23. package/src/audit/checks/caching-headers.ts +305 -0
  24. package/src/audit/checks/canonical-advanced.ts +150 -0
  25. package/src/audit/checks/canonical-domain.ts +196 -0
  26. package/src/audit/checks/citation-quality.ts +358 -0
  27. package/src/audit/checks/client-rendering.ts +542 -0
  28. package/src/audit/checks/color-contrast.ts +342 -0
  29. package/src/audit/checks/content-freshness.ts +170 -0
  30. package/src/audit/checks/content-science.ts +589 -0
  31. package/src/audit/checks/conversion-elements.ts +526 -0
  32. package/src/audit/checks/crawlability.ts +220 -0
  33. package/src/audit/checks/directory-listing.ts +172 -0
  34. package/src/audit/checks/dom-analysis.ts +191 -0
  35. package/src/audit/checks/dom-size.ts +246 -0
  36. package/src/audit/checks/duplicate-content.ts +194 -0
  37. package/src/audit/checks/eeat-signals.ts +990 -0
  38. package/src/audit/checks/entity-seo.ts +396 -0
  39. package/src/audit/checks/featured-snippet.ts +473 -0
  40. package/src/audit/checks/freshness-signals.ts +443 -0
  41. package/src/audit/checks/funnel-intent.ts +463 -0
  42. package/src/audit/checks/hreflang.ts +174 -0
  43. package/src/audit/checks/html-compliance.ts +302 -0
  44. package/src/audit/checks/image-dimensions.ts +167 -0
  45. package/src/audit/checks/images.ts +160 -0
  46. package/src/audit/checks/indexnow.ts +275 -0
  47. package/src/audit/checks/interactive-tools.ts +475 -0
  48. package/src/audit/checks/internal-link-graph.ts +436 -0
  49. package/src/audit/checks/keyword-analysis.ts +239 -0
  50. package/src/audit/checks/keyword-cannibalization.ts +385 -0
  51. package/src/audit/checks/keyword-placement.ts +471 -0
  52. package/src/audit/checks/links.ts +203 -0
  53. package/src/audit/checks/llms-txt.ts +224 -0
  54. package/src/audit/checks/local-seo.ts +296 -0
  55. package/src/audit/checks/mobile.ts +167 -0
  56. package/src/audit/checks/modern-images.ts +226 -0
  57. package/src/audit/checks/navboost-signals.ts +395 -0
  58. package/src/audit/checks/on-page.ts +209 -0
  59. package/src/audit/checks/page-resources.ts +285 -0
  60. package/src/audit/checks/pagination.ts +180 -0
  61. package/src/audit/checks/performance.ts +153 -0
  62. package/src/audit/checks/platform-presence.ts +580 -0
  63. package/src/audit/checks/redirect-analysis.ts +153 -0
  64. package/src/audit/checks/redirect-chain.ts +389 -0
  65. package/src/audit/checks/resource-hints.ts +420 -0
  66. package/src/audit/checks/responsive-css.ts +247 -0
  67. package/src/audit/checks/responsive-images.ts +396 -0
  68. package/src/audit/checks/review-ecosystem.ts +415 -0
  69. package/src/audit/checks/robots-validation.ts +373 -0
  70. package/src/audit/checks/security-headers.ts +172 -0
  71. package/src/audit/checks/security.ts +144 -0
  72. package/src/audit/checks/serp-preview.ts +251 -0
  73. package/src/audit/checks/site-maturity.ts +444 -0
  74. package/src/audit/checks/social-meta.test.ts +275 -0
  75. package/src/audit/checks/social-meta.ts +134 -0
  76. package/src/audit/checks/soft-404.ts +151 -0
  77. package/src/audit/checks/structured-data.ts +238 -0
  78. package/src/audit/checks/tech-detection.ts +496 -0
  79. package/src/audit/checks/topical-clusters.ts +435 -0
  80. package/src/audit/checks/tracker-bloat.ts +462 -0
  81. package/src/audit/checks/tracking-verification.test.ts +371 -0
  82. package/src/audit/checks/tracking-verification.ts +636 -0
  83. package/src/audit/checks/url-safety.ts +682 -0
  84. package/src/audit/deno-entry.ts +66 -0
  85. package/src/audit/discovery/index.ts +15 -0
  86. package/src/audit/discovery/link-crawler.ts +232 -0
  87. package/src/audit/discovery/repo-routes.ts +347 -0
  88. package/src/audit/engine.ts +620 -0
  89. package/src/audit/fixes/index.ts +209 -0
  90. package/src/audit/fixes/social-meta-fixes.test.ts +329 -0
  91. package/src/audit/fixes/social-meta-fixes.ts +463 -0
  92. package/src/audit/index.ts +74 -0
  93. package/src/audit/runner.test.ts +299 -0
  94. package/src/audit/runner.ts +130 -0
  95. package/src/audit/types.ts +1953 -0
  96. package/src/content/featured-snippet.ts +367 -0
  97. package/src/content/generator.test.ts +534 -0
  98. package/src/content/generator.ts +501 -0
  99. package/src/content/headline.ts +317 -0
  100. package/src/content/index.ts +62 -0
  101. package/src/content/intent.ts +258 -0
  102. package/src/content/keyword-density.ts +349 -0
  103. package/src/content/readability.ts +262 -0
  104. package/src/executor.ts +336 -0
  105. package/src/fixer.ts +416 -0
  106. package/src/frameworks/detector.test.ts +248 -0
  107. package/src/frameworks/detector.ts +371 -0
  108. package/src/frameworks/index.ts +68 -0
  109. package/src/frameworks/recipes/angular.yaml +171 -0
  110. package/src/frameworks/recipes/astro.yaml +206 -0
  111. package/src/frameworks/recipes/django.yaml +180 -0
  112. package/src/frameworks/recipes/laravel.yaml +137 -0
  113. package/src/frameworks/recipes/nextjs.yaml +268 -0
  114. package/src/frameworks/recipes/nuxt.yaml +175 -0
  115. package/src/frameworks/recipes/rails.yaml +188 -0
  116. package/src/frameworks/recipes/react.yaml +202 -0
  117. package/src/frameworks/recipes/sveltekit.yaml +154 -0
  118. package/src/frameworks/recipes/vue.yaml +137 -0
  119. package/src/frameworks/recipes/wordpress.yaml +209 -0
  120. package/src/frameworks/suggestion-engine.ts +320 -0
  121. package/src/geo/geo-content.test.ts +305 -0
  122. package/src/geo/geo-content.ts +266 -0
  123. package/src/geo/geo-history.test.ts +473 -0
  124. package/src/geo/geo-history.ts +433 -0
  125. package/src/geo/geo-tracker.test.ts +359 -0
  126. package/src/geo/geo-tracker.ts +411 -0
  127. package/src/geo/index.ts +10 -0
  128. package/src/git/commit-helper.test.ts +261 -0
  129. package/src/git/commit-helper.ts +329 -0
  130. package/src/git/index.ts +12 -0
  131. package/src/git/pr-helper.test.ts +284 -0
  132. package/src/git/pr-helper.ts +307 -0
  133. package/src/index.ts +66 -0
  134. package/src/keywords/ai-keyword-engine.ts +1062 -0
  135. package/src/keywords/ai-summarizer.ts +387 -0
  136. package/src/keywords/ci-mode.ts +555 -0
  137. package/src/keywords/engine.ts +359 -0
  138. package/src/keywords/index.ts +151 -0
  139. package/src/keywords/llm-judge.ts +357 -0
  140. package/src/keywords/nlp-analysis.ts +706 -0
  141. package/src/keywords/prioritizer.ts +295 -0
  142. package/src/keywords/site-crawler.ts +342 -0
  143. package/src/keywords/sources/autocomplete.ts +139 -0
  144. package/src/keywords/sources/competitive-search.ts +450 -0
  145. package/src/keywords/sources/competitor-analysis.ts +374 -0
  146. package/src/keywords/sources/dataforseo.ts +206 -0
  147. package/src/keywords/sources/free-sources.ts +294 -0
  148. package/src/keywords/sources/gsc.ts +123 -0
  149. package/src/keywords/topic-grouping.ts +327 -0
  150. package/src/keywords/types.ts +144 -0
  151. package/src/keywords/wizard.ts +457 -0
  152. package/src/loader.ts +40 -0
  153. package/src/reports/index.ts +7 -0
  154. package/src/reports/report-generator.test.ts +293 -0
  155. package/src/reports/report-generator.ts +713 -0
  156. package/src/scheduler/alerts.test.ts +458 -0
  157. package/src/scheduler/alerts.ts +328 -0
  158. package/src/scheduler/index.ts +8 -0
  159. package/src/scheduler/scheduled-audit.test.ts +377 -0
  160. package/src/scheduler/scheduled-audit.ts +149 -0
  161. package/src/test/integration-test.ts +325 -0
  162. package/src/tools/analyzer.ts +373 -0
  163. package/src/tools/crawl.ts +293 -0
  164. package/src/tools/files.ts +301 -0
  165. package/src/tools/h1-fixer.ts +249 -0
  166. package/src/tools/index.ts +67 -0
  167. package/src/tracking/github-action.ts +326 -0
  168. package/src/tracking/google-analytics.ts +265 -0
  169. package/src/tracking/index.ts +45 -0
  170. package/src/tracking/report-generator.ts +386 -0
  171. package/src/tracking/search-console.ts +335 -0
  172. package/src/types.ts +134 -0
  173. package/src/utils/http.ts +302 -0
  174. package/src/wasm-adapter.ts +297 -0
  175. package/src/wasm-entry.ts +14 -0
  176. package/tsconfig.json +17 -0
  177. package/tsup.wasm.config.ts +26 -0
  178. package/vitest.config.ts +15 -0
@@ -0,0 +1,349 @@
1
+ // Keyword Density Analysis & LSI Keyword Detection
2
+ // Analyzes keyword usage and detects semantically related terms
3
+
4
+ export interface KeywordDensityAnalysis {
5
+ primaryKeyword: string;
6
+ // Density metrics
7
+ density: number; // Percentage
8
+ occurrences: number;
9
+ wordCount: number;
10
+ // Placement analysis
11
+ inTitle: boolean;
12
+ inH1: boolean;
13
+ inFirstParagraph: boolean;
14
+ inLastParagraph: boolean;
15
+ inHeadings: number; // Count of H2-H6 with keyword
16
+ // Warnings
17
+ isKeywordStuffing: boolean;
18
+ isUnderOptimized: boolean;
19
+ // Related terms found
20
+ lsiKeywords: LSIKeyword[];
21
+ // Recommendations
22
+ recommendations: string[];
23
+ }
24
+
25
+ export interface LSIKeyword {
26
+ keyword: string;
27
+ occurrences: number;
28
+ relevance: 'high' | 'medium' | 'low';
29
+ }
30
+
31
+ export interface ContentElements {
32
+ title?: string;
33
+ h1?: string;
34
+ headings: string[];
35
+ body: string;
36
+ firstParagraph?: string;
37
+ lastParagraph?: string;
38
+ }
39
+
40
+ // Common LSI/semantic relations for various topics
41
+ const LSI_PATTERNS: Record<string, string[]> = {
42
+ // Generic patterns
43
+ 'software': ['application', 'tool', 'platform', 'solution', 'program', 'app'],
44
+ 'website': ['site', 'webpage', 'page', 'web application', 'portal'],
45
+ 'seo': ['search engine optimization', 'ranking', 'serp', 'google', 'keywords', 'backlinks'],
46
+ 'marketing': ['advertising', 'promotion', 'campaign', 'strategy', 'content', 'audience'],
47
+ 'business': ['company', 'enterprise', 'organization', 'startup', 'firm'],
48
+ 'tutorial': ['guide', 'how-to', 'instructions', 'steps', 'learn'],
49
+ 'best': ['top', 'leading', 'recommended', 'popular', 'favorite'],
50
+ };
51
+
52
+ /**
53
+ * Analyze keyword density in content
54
+ */
55
+ export function analyzeKeywordDensity(
56
+ primaryKeyword: string,
57
+ content: ContentElements
58
+ ): KeywordDensityAnalysis {
59
+ const keyword = primaryKeyword.toLowerCase();
60
+ const keywordWords = keyword.split(/\s+/);
61
+ const isPhrase = keywordWords.length > 1;
62
+
63
+ const recommendations: string[] = [];
64
+
65
+ // Count occurrences in body
66
+ const bodyText = content.body.toLowerCase();
67
+ const words = bodyText.split(/\s+/).filter(w => w.length > 0);
68
+ const wordCount = words.length;
69
+
70
+ // Count exact phrase/word occurrences
71
+ let occurrences = 0;
72
+ if (isPhrase) {
73
+ // Count phrase occurrences
74
+ const regex = new RegExp(escapeRegex(keyword), 'gi');
75
+ const matches = bodyText.match(regex);
76
+ occurrences = matches ? matches.length : 0;
77
+ } else {
78
+ // Count word occurrences
79
+ occurrences = words.filter(w => w.replace(/[^a-z]/g, '') === keyword).length;
80
+ }
81
+
82
+ // Calculate density
83
+ const density = (occurrences / Math.max(wordCount, 1)) * 100;
84
+
85
+ // Check placement
86
+ const inTitle = content.title ? content.title.toLowerCase().includes(keyword) : false;
87
+ const inH1 = content.h1 ? content.h1.toLowerCase().includes(keyword) : false;
88
+ const inFirstParagraph = content.firstParagraph
89
+ ? content.firstParagraph.toLowerCase().includes(keyword)
90
+ : false;
91
+ const inLastParagraph = content.lastParagraph
92
+ ? content.lastParagraph.toLowerCase().includes(keyword)
93
+ : false;
94
+ const inHeadings = content.headings.filter(h => h.toLowerCase().includes(keyword)).length;
95
+
96
+ // Check for keyword stuffing (> 3% is generally considered stuffing)
97
+ const isKeywordStuffing = density > 3;
98
+ if (isKeywordStuffing) {
99
+ recommendations.push(`Keyword density (${density.toFixed(2)}%) is too high. Reduce to 1-2% to avoid penalties`);
100
+ }
101
+
102
+ // Check for under-optimization
103
+ const isUnderOptimized = density < 0.5 || occurrences < 2;
104
+ if (isUnderOptimized && wordCount > 100) {
105
+ recommendations.push(`Keyword density (${density.toFixed(2)}%) is low. Consider adding more natural mentions`);
106
+ }
107
+
108
+ // Placement recommendations
109
+ if (!inTitle) {
110
+ recommendations.push('Add primary keyword to the page title');
111
+ }
112
+ if (!inH1) {
113
+ recommendations.push('Add primary keyword to the H1 heading');
114
+ }
115
+ if (!inFirstParagraph) {
116
+ recommendations.push('Include keyword in the first 100 words of content');
117
+ }
118
+ if (inHeadings < 2 && wordCount > 500) {
119
+ recommendations.push('Add keyword to at least 2 subheadings (H2/H3)');
120
+ }
121
+
122
+ // Find LSI keywords
123
+ const lsiKeywords = findLSIKeywords(keyword, bodyText);
124
+
125
+ // LSI recommendations
126
+ const highRelevanceLSI = lsiKeywords.filter(l => l.relevance === 'high' && l.occurrences === 0);
127
+ if (highRelevanceLSI.length > 0) {
128
+ recommendations.push(`Consider adding related terms: ${highRelevanceLSI.slice(0, 3).map(l => l.keyword).join(', ')}`);
129
+ }
130
+
131
+ return {
132
+ primaryKeyword,
133
+ density: Math.round(density * 100) / 100,
134
+ occurrences,
135
+ wordCount,
136
+ inTitle,
137
+ inH1,
138
+ inFirstParagraph,
139
+ inLastParagraph,
140
+ inHeadings,
141
+ isKeywordStuffing,
142
+ isUnderOptimized,
143
+ lsiKeywords,
144
+ recommendations,
145
+ };
146
+ }
147
+
148
+ /**
149
+ * Find LSI (Latent Semantic Indexing) keywords in content
150
+ */
151
+ export function findLSIKeywords(primaryKeyword: string, content: string): LSIKeyword[] {
152
+ const keyword = primaryKeyword.toLowerCase();
153
+ const contentLower = content.toLowerCase();
154
+ const results: LSIKeyword[] = [];
155
+
156
+ // Get semantically related terms based on keyword components
157
+ const relatedTerms = new Set<string>();
158
+
159
+ // Check against known LSI patterns
160
+ for (const [pattern, related] of Object.entries(LSI_PATTERNS)) {
161
+ if (keyword.includes(pattern) || pattern.includes(keyword)) {
162
+ related.forEach(term => relatedTerms.add(term));
163
+ }
164
+ }
165
+
166
+ // Add common modifiers and variations
167
+ const modifiers = [
168
+ 'best', 'top', 'free', 'online', 'guide', 'tutorial', 'how to',
169
+ 'tips', 'examples', 'review', 'vs', 'alternative',
170
+ ];
171
+ modifiers.forEach(mod => {
172
+ relatedTerms.add(`${mod} ${keyword}`);
173
+ relatedTerms.add(`${keyword} ${mod}`);
174
+ });
175
+
176
+ // Add word variations
177
+ const keywordWords = keyword.split(/\s+/);
178
+ keywordWords.forEach(word => {
179
+ // Plural/singular variations
180
+ if (word.endsWith('s')) {
181
+ relatedTerms.add(word.slice(0, -1));
182
+ } else {
183
+ relatedTerms.add(word + 's');
184
+ }
185
+ // -ing/-ed variations
186
+ if (word.endsWith('ing')) {
187
+ relatedTerms.add(word.slice(0, -3));
188
+ relatedTerms.add(word.slice(0, -3) + 'ed');
189
+ }
190
+ });
191
+
192
+ // Analyze content for related terms
193
+ for (const term of relatedTerms) {
194
+ if (term === keyword) continue;
195
+
196
+ const regex = new RegExp(`\\b${escapeRegex(term)}\\b`, 'gi');
197
+ const matches = contentLower.match(regex);
198
+ const occurrences = matches ? matches.length : 0;
199
+
200
+ // Determine relevance
201
+ let relevance: 'high' | 'medium' | 'low' = 'low';
202
+ if (term.includes(keyword) || keyword.includes(term)) {
203
+ relevance = 'high';
204
+ } else if (term.split(/\s+/).some(w => keyword.includes(w))) {
205
+ relevance = 'medium';
206
+ }
207
+
208
+ results.push({ keyword: term, occurrences, relevance });
209
+ }
210
+
211
+ // Sort by relevance and occurrences
212
+ results.sort((a, b) => {
213
+ const relevanceOrder = { high: 0, medium: 1, low: 2 };
214
+ if (relevanceOrder[a.relevance] !== relevanceOrder[b.relevance]) {
215
+ return relevanceOrder[a.relevance] - relevanceOrder[b.relevance];
216
+ }
217
+ return b.occurrences - a.occurrences;
218
+ });
219
+
220
+ return results.slice(0, 20);
221
+ }
222
+
223
+ /**
224
+ * Calculate TF-IDF score for keywords in content
225
+ */
226
+ export function calculateTFIDF(
227
+ content: string,
228
+ documentCount: number = 100
229
+ ): Map<string, number> {
230
+ const words = content.toLowerCase().split(/\s+/).filter(w => w.length > 3);
231
+ const wordCount = words.length;
232
+ const termFrequency = new Map<string, number>();
233
+
234
+ // Count term frequency
235
+ for (const word of words) {
236
+ const clean = word.replace(/[^a-z]/g, '');
237
+ if (clean.length > 3 && !isStopWord(clean)) {
238
+ termFrequency.set(clean, (termFrequency.get(clean) || 0) + 1);
239
+ }
240
+ }
241
+
242
+ // Calculate TF-IDF (simplified - assume inverse document frequency)
243
+ const tfidf = new Map<string, number>();
244
+ for (const [term, freq] of termFrequency) {
245
+ const tf = freq / wordCount;
246
+ // Simplified IDF - assume rare terms have higher value
247
+ const idf = Math.log(documentCount / (1 + Math.min(freq, 10)));
248
+ tfidf.set(term, tf * idf);
249
+ }
250
+
251
+ return tfidf;
252
+ }
253
+
254
+ function isStopWord(word: string): boolean {
255
+ const stopWords = new Set([
256
+ 'the', 'a', 'an', 'and', 'or', 'but', 'in', 'on', 'at', 'to', 'for',
257
+ 'of', 'with', 'by', 'from', 'as', 'is', 'was', 'are', 'were', 'been',
258
+ 'be', 'have', 'has', 'had', 'do', 'does', 'did', 'will', 'would',
259
+ 'could', 'should', 'may', 'might', 'must', 'shall', 'can', 'need',
260
+ 'this', 'that', 'these', 'those', 'what', 'which', 'who', 'when',
261
+ 'where', 'why', 'how', 'all', 'each', 'every', 'both', 'few', 'more',
262
+ 'most', 'other', 'some', 'such', 'only', 'same', 'than', 'too', 'very',
263
+ 'just', 'also', 'now', 'here', 'there', 'then', 'once', 'your', 'their',
264
+ ]);
265
+ return stopWords.has(word);
266
+ }
267
+
268
+ function escapeRegex(str: string): string {
269
+ return str.replace(/[.*+?^${}()|[\]\\]/g, '\\$&');
270
+ }
271
+
272
+ /**
273
+ * Format keyword density report
274
+ */
275
+ export function formatKeywordDensityReport(analysis: KeywordDensityAnalysis): string {
276
+ const lines: string[] = [];
277
+
278
+ lines.push('');
279
+ lines.push('═'.repeat(60));
280
+ lines.push(' KEYWORD DENSITY ANALYSIS');
281
+ lines.push('═'.repeat(60));
282
+ lines.push('');
283
+
284
+ // Primary keyword stats
285
+ lines.push(`Primary Keyword: "${analysis.primaryKeyword}"`);
286
+ lines.push('');
287
+
288
+ // Density metrics
289
+ const densityIcon = analysis.isKeywordStuffing ? '🔴' :
290
+ analysis.isUnderOptimized ? '🟡' : '🟢';
291
+ lines.push(`${densityIcon} Density: ${analysis.density}%`);
292
+ lines.push(` Occurrences: ${analysis.occurrences} times in ${analysis.wordCount} words`);
293
+
294
+ if (analysis.isKeywordStuffing) {
295
+ lines.push(' ⚠️ WARNING: Possible keyword stuffing detected');
296
+ }
297
+ if (analysis.isUnderOptimized) {
298
+ lines.push(' ⚠️ WARNING: Keyword may be under-optimized');
299
+ }
300
+ lines.push('');
301
+
302
+ // Placement analysis
303
+ lines.push('📍 KEYWORD PLACEMENT');
304
+ lines.push('─'.repeat(60));
305
+ lines.push(` In Title: ${analysis.inTitle ? '✅ Yes' : '❌ No'}`);
306
+ lines.push(` In H1: ${analysis.inH1 ? '✅ Yes' : '❌ No'}`);
307
+ lines.push(` In First Paragraph: ${analysis.inFirstParagraph ? '✅ Yes' : '❌ No'}`);
308
+ lines.push(` In Subheadings: ${analysis.inHeadings > 0 ? `✅ ${analysis.inHeadings} times` : '❌ No'}`);
309
+ lines.push(` In Last Paragraph: ${analysis.inLastParagraph ? '✅ Yes' : '❌ No'}`);
310
+ lines.push('');
311
+
312
+ // LSI Keywords
313
+ const presentLSI = analysis.lsiKeywords.filter(l => l.occurrences > 0);
314
+ const missingLSI = analysis.lsiKeywords.filter(l => l.occurrences === 0 && l.relevance !== 'low');
315
+
316
+ if (presentLSI.length > 0) {
317
+ lines.push('🔗 LSI KEYWORDS FOUND');
318
+ lines.push('─'.repeat(60));
319
+ for (const lsi of presentLSI.slice(0, 8)) {
320
+ const relevanceIcon = lsi.relevance === 'high' ? '🟢' :
321
+ lsi.relevance === 'medium' ? '🟡' : '⚪';
322
+ lines.push(` ${relevanceIcon} "${lsi.keyword}" - ${lsi.occurrences} occurrences`);
323
+ }
324
+ lines.push('');
325
+ }
326
+
327
+ if (missingLSI.length > 0) {
328
+ lines.push('📝 SUGGESTED LSI KEYWORDS TO ADD');
329
+ lines.push('─'.repeat(60));
330
+ for (const lsi of missingLSI.slice(0, 5)) {
331
+ lines.push(` • ${lsi.keyword}`);
332
+ }
333
+ lines.push('');
334
+ }
335
+
336
+ // Recommendations
337
+ if (analysis.recommendations.length > 0) {
338
+ lines.push('💡 RECOMMENDATIONS');
339
+ lines.push('─'.repeat(60));
340
+ for (const rec of analysis.recommendations) {
341
+ lines.push(` • ${rec}`);
342
+ }
343
+ lines.push('');
344
+ }
345
+
346
+ lines.push('═'.repeat(60));
347
+
348
+ return lines.join('\n');
349
+ }
@@ -0,0 +1,262 @@
1
+ // Content Readability Analysis
2
+ // Implements Flesch Reading Ease, Flesch-Kincaid, and other readability metrics
3
+
4
+ export interface ReadabilityResult {
5
+ // Flesch Reading Ease (0-100, higher = easier)
6
+ fleschReadingEase: number;
7
+ fleschGrade: string;
8
+ // Flesch-Kincaid Grade Level (US school grade)
9
+ fleschKincaidGrade: number;
10
+ // Additional metrics
11
+ avgSentenceLength: number;
12
+ avgSyllablesPerWord: number;
13
+ avgWordsPerParagraph: number;
14
+ // Content stats
15
+ wordCount: number;
16
+ sentenceCount: number;
17
+ paragraphCount: number;
18
+ syllableCount: number;
19
+ // Quality indicators
20
+ passiveVoicePercentage: number;
21
+ complexWordPercentage: number; // Words with 3+ syllables
22
+ // Recommendations
23
+ recommendations: string[];
24
+ score: number; // 0-100 composite score
25
+ }
26
+
27
+ /**
28
+ * Analyze content readability
29
+ */
30
+ export function analyzeReadability(text: string): ReadabilityResult {
31
+ // Clean and normalize text
32
+ const cleanText = text.replace(/<[^>]*>/g, ' ').replace(/\s+/g, ' ').trim();
33
+
34
+ // Count elements
35
+ const words = getWords(cleanText);
36
+ const sentences = getSentences(cleanText);
37
+ const paragraphs = getParagraphs(cleanText);
38
+ const syllables = countTotalSyllables(words);
39
+
40
+ const wordCount = words.length;
41
+ const sentenceCount = Math.max(sentences.length, 1);
42
+ const paragraphCount = Math.max(paragraphs.length, 1);
43
+
44
+ // Calculate averages
45
+ const avgSentenceLength = wordCount / sentenceCount;
46
+ const avgSyllablesPerWord = syllables / Math.max(wordCount, 1);
47
+ const avgWordsPerParagraph = wordCount / paragraphCount;
48
+
49
+ // Flesch Reading Ease: 206.835 - 1.015 * (words/sentences) - 84.6 * (syllables/words)
50
+ const fleschReadingEase = Math.max(0, Math.min(100,
51
+ 206.835 - (1.015 * avgSentenceLength) - (84.6 * avgSyllablesPerWord)
52
+ ));
53
+
54
+ // Flesch-Kincaid Grade Level: 0.39 * (words/sentences) + 11.8 * (syllables/words) - 15.59
55
+ const fleschKincaidGrade = Math.max(0,
56
+ (0.39 * avgSentenceLength) + (11.8 * avgSyllablesPerWord) - 15.59
57
+ );
58
+
59
+ // Passive voice detection
60
+ const passiveVoiceCount = countPassiveVoice(sentences);
61
+ const passiveVoicePercentage = (passiveVoiceCount / sentenceCount) * 100;
62
+
63
+ // Complex words (3+ syllables)
64
+ const complexWords = words.filter(w => countSyllables(w) >= 3);
65
+ const complexWordPercentage = (complexWords.length / Math.max(wordCount, 1)) * 100;
66
+
67
+ // Generate recommendations
68
+ const recommendations: string[] = [];
69
+
70
+ if (avgSentenceLength > 20) {
71
+ recommendations.push(`Shorten sentences. Average is ${avgSentenceLength.toFixed(1)} words (target: <20)`);
72
+ }
73
+
74
+ if (fleschReadingEase < 60) {
75
+ recommendations.push('Simplify language. Content may be too difficult for general audiences');
76
+ }
77
+
78
+ if (passiveVoicePercentage > 15) {
79
+ recommendations.push(`Reduce passive voice (${passiveVoicePercentage.toFixed(1)}%). Use active voice for clarity`);
80
+ }
81
+
82
+ if (complexWordPercentage > 25) {
83
+ recommendations.push(`Reduce complex words (${complexWordPercentage.toFixed(1)}%). Use simpler alternatives`);
84
+ }
85
+
86
+ if (avgWordsPerParagraph > 150) {
87
+ recommendations.push('Break up long paragraphs. Target 50-150 words per paragraph');
88
+ }
89
+
90
+ // Composite score
91
+ const score = calculateReadabilityScore({
92
+ fleschReadingEase,
93
+ avgSentenceLength,
94
+ passiveVoicePercentage,
95
+ complexWordPercentage,
96
+ });
97
+
98
+ return {
99
+ fleschReadingEase: Math.round(fleschReadingEase * 10) / 10,
100
+ fleschGrade: getFleschGrade(fleschReadingEase),
101
+ fleschKincaidGrade: Math.round(fleschKincaidGrade * 10) / 10,
102
+ avgSentenceLength: Math.round(avgSentenceLength * 10) / 10,
103
+ avgSyllablesPerWord: Math.round(avgSyllablesPerWord * 100) / 100,
104
+ avgWordsPerParagraph: Math.round(avgWordsPerParagraph),
105
+ wordCount,
106
+ sentenceCount,
107
+ paragraphCount,
108
+ syllableCount: syllables,
109
+ passiveVoicePercentage: Math.round(passiveVoicePercentage * 10) / 10,
110
+ complexWordPercentage: Math.round(complexWordPercentage * 10) / 10,
111
+ recommendations,
112
+ score,
113
+ };
114
+ }
115
+
116
+ function getWords(text: string): string[] {
117
+ return text.split(/\s+/).filter(w => w.length > 0 && /[a-zA-Z]/.test(w));
118
+ }
119
+
120
+ function getSentences(text: string): string[] {
121
+ return text.split(/[.!?]+/).filter(s => s.trim().length > 0);
122
+ }
123
+
124
+ function getParagraphs(text: string): string[] {
125
+ return text.split(/\n\n+/).filter(p => p.trim().length > 0);
126
+ }
127
+
128
+ function countSyllables(word: string): number {
129
+ word = word.toLowerCase().replace(/[^a-z]/g, '');
130
+ if (word.length <= 3) return 1;
131
+
132
+ // Count vowel groups
133
+ const vowels = 'aeiouy';
134
+ let count = 0;
135
+ let prevWasVowel = false;
136
+
137
+ for (const char of word) {
138
+ const isVowel = vowels.includes(char);
139
+ if (isVowel && !prevWasVowel) {
140
+ count++;
141
+ }
142
+ prevWasVowel = isVowel;
143
+ }
144
+
145
+ // Adjust for silent e
146
+ if (word.endsWith('e') && count > 1) {
147
+ count--;
148
+ }
149
+
150
+ // Adjust for common endings
151
+ if (word.endsWith('le') && word.length > 2 && !vowels.includes(word[word.length - 3])) {
152
+ count++;
153
+ }
154
+
155
+ return Math.max(1, count);
156
+ }
157
+
158
+ function countTotalSyllables(words: string[]): number {
159
+ return words.reduce((sum, word) => sum + countSyllables(word), 0);
160
+ }
161
+
162
+ function countPassiveVoice(sentences: string[]): number {
163
+ const passivePatterns = [
164
+ /\b(am|is|are|was|were|been|being)\s+\w+ed\b/i,
165
+ /\b(am|is|are|was|were|been|being)\s+\w+en\b/i,
166
+ /\b(get|gets|got|gotten|getting)\s+\w+ed\b/i,
167
+ ];
168
+
169
+ return sentences.filter(sentence =>
170
+ passivePatterns.some(pattern => pattern.test(sentence))
171
+ ).length;
172
+ }
173
+
174
+ function getFleschGrade(score: number): string {
175
+ if (score >= 90) return 'Very Easy (5th grade)';
176
+ if (score >= 80) return 'Easy (6th grade)';
177
+ if (score >= 70) return 'Fairly Easy (7th grade)';
178
+ if (score >= 60) return 'Standard (8th-9th grade)';
179
+ if (score >= 50) return 'Fairly Difficult (10th-12th grade)';
180
+ if (score >= 30) return 'Difficult (College)';
181
+ return 'Very Difficult (College Graduate)';
182
+ }
183
+
184
+ function calculateReadabilityScore(metrics: {
185
+ fleschReadingEase: number;
186
+ avgSentenceLength: number;
187
+ passiveVoicePercentage: number;
188
+ complexWordPercentage: number;
189
+ }): number {
190
+ // Weight factors
191
+ const weights = {
192
+ flesch: 0.4,
193
+ sentenceLength: 0.25,
194
+ passiveVoice: 0.2,
195
+ complexity: 0.15,
196
+ };
197
+
198
+ // Score components (0-100)
199
+ const fleschScore = metrics.fleschReadingEase;
200
+ const sentenceLengthScore = Math.max(0, 100 - Math.abs(metrics.avgSentenceLength - 15) * 3);
201
+ const passiveVoiceScore = Math.max(0, 100 - metrics.passiveVoicePercentage * 5);
202
+ const complexityScore = Math.max(0, 100 - metrics.complexWordPercentage * 2);
203
+
204
+ const totalScore =
205
+ fleschScore * weights.flesch +
206
+ sentenceLengthScore * weights.sentenceLength +
207
+ passiveVoiceScore * weights.passiveVoice +
208
+ complexityScore * weights.complexity;
209
+
210
+ return Math.round(totalScore);
211
+ }
212
+
213
+ /**
214
+ * Format readability report
215
+ */
216
+ export function formatReadabilityReport(result: ReadabilityResult): string {
217
+ const lines: string[] = [];
218
+
219
+ lines.push('');
220
+ lines.push('═'.repeat(60));
221
+ lines.push(' READABILITY ANALYSIS');
222
+ lines.push('═'.repeat(60));
223
+ lines.push('');
224
+
225
+ // Score overview
226
+ const scoreIcon = result.score >= 70 ? '🟢' : result.score >= 50 ? '🟡' : '🔴';
227
+ lines.push(`${scoreIcon} Readability Score: ${result.score}/100`);
228
+ lines.push('');
229
+
230
+ // Key metrics
231
+ lines.push('📊 KEY METRICS');
232
+ lines.push('─'.repeat(60));
233
+ lines.push(` Flesch Reading Ease: ${result.fleschReadingEase} (${result.fleschGrade})`);
234
+ lines.push(` Flesch-Kincaid Grade: ${result.fleschKincaidGrade}`);
235
+ lines.push(` Avg Sentence Length: ${result.avgSentenceLength} words`);
236
+ lines.push(` Passive Voice: ${result.passiveVoicePercentage}%`);
237
+ lines.push(` Complex Words: ${result.complexWordPercentage}%`);
238
+ lines.push('');
239
+
240
+ // Content stats
241
+ lines.push('📝 CONTENT STATS');
242
+ lines.push('─'.repeat(60));
243
+ lines.push(` Words: ${result.wordCount}`);
244
+ lines.push(` Sentences: ${result.sentenceCount}`);
245
+ lines.push(` Paragraphs: ${result.paragraphCount}`);
246
+ lines.push(` Syllables: ${result.syllableCount}`);
247
+ lines.push('');
248
+
249
+ // Recommendations
250
+ if (result.recommendations.length > 0) {
251
+ lines.push('💡 RECOMMENDATIONS');
252
+ lines.push('─'.repeat(60));
253
+ for (const rec of result.recommendations) {
254
+ lines.push(` • ${rec}`);
255
+ }
256
+ lines.push('');
257
+ }
258
+
259
+ lines.push('═'.repeat(60));
260
+
261
+ return lines.join('\n');
262
+ }