forked from mihakralj/obsidian-textanalysis
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathAnalysisGenerator.ts
More file actions
executable file
·399 lines (346 loc) · 19.2 KB
/
Copy pathAnalysisGenerator.ts
File metadata and controls
executable file
·399 lines (346 loc) · 19.2 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
import { MarkdownView } from 'obsidian';
import TextAnalysisPlugin from './main';
import readability from 'text-readability';
import { syllable as syllableCount } from 'syllable';
export interface AnalysisMetric {
id: string;
active: boolean;
label: string;
value: string;
}
interface TextMetrics {
wordCount: number;
sentenceCount: number;
words: string[];
}
// Regex patterns as constants to avoid object comparison issues
const LINK_PATTERN = /\[([^[\]]+)\]\(([^()]+)\)/g;
const ITALIC_PATTERN = /([*_])([^*_]*?)\1/g;
const TABLE_PATTERN = /\|\s*(.*?)\s*\|/g;
export class AnalysisGenerator {
private readonly plugin: TextAnalysisPlugin;
private readonly _analysisMetrics: AnalysisMetric[];
constructor(plugin: TextAnalysisPlugin) {
this.plugin = plugin;
this._analysisMetrics = [
{ id: 'Char', active: true, label: 'Character Count', value: '0' },
{ id: 'Lettr', active: true, label: 'Letter Count', value: '0' },
{ id: 'Word', active: true, label: 'Word Count', value: '0' },
{ id: 'Sent', active: true, label: 'Sentence Count', value: '0' },
{ id: 'Para', active: true, label: 'Paragraph Count', value: '0' },
{ id: 'Syll', active: true, label: 'Syllable Count', value: '0' },
{ id: 'ASen', active: true, label: 'Average Words per Sentence', value: '0' },
{ id: 'ASyl', active: true, label: 'Average Syllables per Word', value: '0' },
{ id: 'AChr', active: true, label: 'Average Characters per Word', value: '0' },
{ id: 'Diff', active: true, label: '% of Difficult Words', value: '0' },
{ id: 'SenC', active: true, label: 'Sentence Complexity', value: '0' },
{ id: 'LexD', active: true, label: 'Lexical Diversity', value: '0' },
{ id: 'FREs', active: true, label: 'Flesch Reading Ease Score', value: '0' },
{ id: 'FRDf', active: true, label: 'Flesch Reading Difficulty', value: '0' },
{ id: 'FKGL', active: true, label: 'Flesch-Kincaid Grade Level', value: '0' },
{ id: 'GFog', active: true, label: 'Gunning Fog Index', value: '0' },
{ id: 'SMOG', active: true, label: 'SMOG Index', value: '0' },
{ id: 'FCST', active: true, label: 'FORCAST Grade Level', value: '0' },
{ id: 'ARI', active: true, label: 'Automated Readability Index', value: '0' },
{ id: 'CLI', active: true, label: 'Coleman-Liau Index', value: '0' },
{ id: 'LWri', active: true, label: 'Linsear Write', value: '0' },
{ id: 'NDCh', active: true, label: 'New Dale-Chall Score', value: '0' },
{ id: 'PSK', active: true, label: 'Powers Sumner Kearl Grade', value: '0' },
{ id: 'RIX', active: true, label: 'Rix Readability ', value: '0' },
{ id: 'RIXD', active: true, label: 'Rix Difficulty ', value: '0' },
{ id: 'LIX', active: true, label: 'Lix Readability ', value: '0' },
{ id: 'LIXD', active: true, label: 'Lix Difficulty ', value: '0' },
{ id: 'GrdM', active: true, label: 'Grade level (consensus)', value: '0' },
{ id: 'Rdbl', active: true, label: 'Readability Rating', value: '0' },
{ id: 'GrdL', active: true, label: 'Reading level', value: '0' },
{ id: 'RdTm', active: true, label: 'Reading time', value: '0:00' },
{ id: 'SpkT', active: true, label: 'Speaking time', value: '0:00' },
];
}
// Public getter for analysisMetrics
get analysisMetrics(): AnalysisMetric[] {
return this._analysisMetrics;
}
displayAnalysis(): HTMLElement {
const container = document.createElement('div');
container.style.cssText = 'margin-left: 0; margin-right: 0; overflow-y: auto; max-height: 800px;';
const table = document.createElement('table');
table.classList.add('nav-folder-title');
table.style.cssText = 'width: 100%; border-collapse: collapse;';
const tbody = document.createElement('tbody');
table.appendChild(tbody);
const color = window.getComputedStyle(document.body).color.match(/\d+/g);
const borderColor = color ? `rgba(${color[0]}, ${color[1]}, ${color[2]}, 0.05)` : 'rgba(0, 0, 0, 0.05)';
this._analysisMetrics
.filter(item => item.active)
.forEach(item => {
const row = tbody.insertRow();
row.style.cursor = 'pointer';
row.addEventListener('click', () => {
// Open settings when a row is clicked
const app = (this.plugin.app as any);
const settingsTab = app.setting.settingTabs.find((tab: any) => tab.id === 'textanalysis');
if (settingsTab) {
app.setting.open();
app.setting.openTabById('textanalysis');
}
});
const [labelCell, valueCell] = [row.insertCell(), row.insertCell()];
labelCell.textContent = item.label;
labelCell.style.cssText = `text-align: left; padding: 2px 10px 2px 0px; width: 100%; white-space: nowrap; border-bottom: 1px solid ${borderColor}`;
valueCell.textContent = item.value;
valueCell.dataset.id = item.id;
valueCell.style.cssText = `text-align: right; padding: 2px 10px 2px 0px; width: 100%; white-space: nowrap; border-bottom: 1px solid ${borderColor}`;
});
container.appendChild(table);
container.appendChild(this.createSpacer());
return container;
}
private createSpacer(): HTMLElement {
const spacer = document.createElement('div');
spacer.style.height = '30px';
return spacer;
}
removeMarkdownFormatting(text: string): string {
// First, normalize line endings and ensure proper spacing
text = text.replace(/\r\n/g, '\n')
.replace(/\n\n+/g, '\n\n') // Normalize multiple newlines to double newlines
.replace(/([.!?])\s*/g, '$1 '); // Ensure space after punctuation
const markdownPatterns = [
{ pattern: /^#+\s+/gm, replacement: '' }, // Headers
{ pattern: /(\*\*|__)(.*?)\1/g, replacement: '$2' }, // Bold
{ pattern: /([*_])([^*_]*?)\1/g, replacement: '$2' }, // Italic
{ pattern: /\[([^[\]]+)\]\(([^()]+)\)/g, replacement: '$1' }, // Links
{ pattern: /!\[([^\]]*)\]\(([^)]+)\)/g, replacement: '' }, // Images
{ pattern: /^>\s+/gm, replacement: '' }, // Blockquotes
{ pattern: /^---[\r\n][\s\S]*?---[\r\n]/gm, replacement: '' }, // Front matter
{ pattern: /^[-*_]{3,}\s*$/gm, replacement: '' }, // Horizontal rules
{ pattern: /`([^`]*)`/g, replacement: '$1' }, // Inline code
{ pattern: /```([\s\S]*?)```/g, replacement: '' }, // Code blocks
{ pattern: /^\s*[*\-+]\s+/gm, replacement: '' }, // Unordered lists
{ pattern: /^\s*\d+\.\s+/gm, replacement: '' }, // Ordered lists
{ pattern: /^\s*\[([^]]+)\]:\s*(.+)$/gm, replacement: '' }, // Reference links
{ pattern: /~~(.*?)~~/g, replacement: '$1' }, // Strikethrough
{ pattern: /^\[\^([^\]]+)\]:\s*(.+)$/gm, replacement: '' }, // Footnotes
{ pattern: /\|\s*(.*?)\s*\|/g, replacement: '$1' }, // Tables
{ pattern: /^\|?[-:]+\|[-:| ]+\s*$/gm, replacement: '' }, // Table formatting
];
// Apply each pattern while preserving spaces
let cleanText = text;
for (const { pattern, replacement } of markdownPatterns) {
cleanText = cleanText.replace(pattern, replacement);
}
// Clean up any remaining multiple spaces while preserving single spaces
cleanText = cleanText.replace(/\s+/g, ' ').trim();
// console.log('DEBUG - Text after markdown removal:', cleanText);
return cleanText;
}
private getBaseTextMetrics(text: string): TextMetrics {
const words = text.match(/\b\p{L}(['\-\p{L}\p{N}]*\p{L})?\b/gu) ?? [];
const sentenceCount = (text.match(/[.!?]+/g) || []).length;
// console.log('DEBUG - Words found:', words);
// console.log('DEBUG - Sentence count:', sentenceCount);
return {
words,
wordCount: words.length,
sentenceCount
};
}
async calculateAndUpdate(id: string, calculation: () => string): Promise<void> {
try {
const result = await Promise.resolve(calculation());
const valueCell = document.querySelector(`td[data-id="${id}"]`);
if (valueCell) {
valueCell.textContent = result;
}
const metric = this._analysisMetrics.find(metric => metric.id === id);
if (metric) {
metric.value = result;
}
// if (id === 'FREs') {
// console.log('DEBUG - Flesch Reading Ease Score:', result);
// }
} catch (error) {
console.error(`Error calculating metric ${id}:`, error);
}
}
updateAnalysisValues(): void {
const text = this.getActiveText();
if (!text) return;
// console.log('DEBUG - Active text:', text);
const metrics = this.getBaseTextMetrics(text);
this.updateBasicMetrics(text, metrics);
this.updateAdvancedMetrics(text, metrics);
this.updateReadabilityMetrics(text, metrics);
this.updateTimeMetrics(text);
}
private getActiveText(): string {
const selection = window.getSelection();
if (selection && selection.toString().length > 0) {
return this.removeMarkdownFormatting(selection.toString());
}
const activeView = this.plugin.app.workspace.getActiveViewOfType(MarkdownView);
if (!activeView) return '';
return this.removeMarkdownFormatting(activeView.editor.getValue());
}
private updateBasicMetrics(text: string, metrics: TextMetrics): void {
this.calculateAndUpdate('Char', () => readability.charCount(text, false).toString());
this.calculateAndUpdate('Lettr', () => readability.letterCount(text).toString());
this.calculateAndUpdate('Word', () => metrics.wordCount.toString());
this.calculateAndUpdate('Sent', () => metrics.sentenceCount.toString());
this.calculateAndUpdate('Para', () => (text.match(/[^\n]+\s*/g)?.length || 0).toString());
this.calculateAndUpdate('Syll', () => readability.syllableCount(text).toString());
}
private updateAdvancedMetrics(text: string, metrics: TextMetrics): void {
this.calculateAndUpdate('ASen', () => readability.averageSentenceLength(text).toString());
this.calculateAndUpdate('ASyl', () => readability.averageSyllablePerWord(text).toString());
this.calculateAndUpdate('AChr', () => readability.averageCharacterPerWord(text).toString());
this.calculateDifficultWords(metrics);
this.calculateSentenceComplexity(text, metrics);
this.calculateAndUpdate('LexD', () => readability.lexiconCount(text).toString());
}
private updateReadabilityMetrics(text: string, metrics: TextMetrics): void {
this.calculateAndUpdate('GrdL', () => readability.textStandard(text));
this.calculateAndUpdate('GrdM', () => readability.textMedian(text));
this.calculateFleschMetrics(text);
this.calculateOtherReadabilityIndices(text);
this.calculateCustomReadabilityMetrics(text, metrics);
}
private updateTimeMetrics(text: string): void {
this.calculateAndUpdate('RdTm', () => TextUtils.getReadingTimes(text));
this.calculateAndUpdate('SpkT', () => TextUtils.getSpeakingTimes(text));
}
private calculateDifficultWords(metrics: TextMetrics): void {
this.calculateAndUpdate('Diff', () => {
const difficult = readability.difficultWords(metrics.words.join(' '), 3);
const result = (difficult / metrics.wordCount) * 100;
return result === 0 ? '0%' : `${parseFloat(result.toFixed(2))}%`;
});
}
private calculateSentenceComplexity(text: string, metrics: TextMetrics): void {
this.calculateAndUpdate('SenC', () => {
if (metrics.sentenceCount === 0) return '0';
const clauseMarkers = /,|;|and|or|but|although|because/g;
const totalClauses = text.split(/[.!?]+/)
.filter(Boolean)
.reduce((total, sentence) => total + (sentence.match(clauseMarkers)?.length || 0) + 1, 0);
return (totalClauses / metrics.sentenceCount).toFixed(1);
});
}
private calculateFleschMetrics(text: string): void {
// console.log('DEBUG - Calculating Flesch metrics for text:', text);
const fleschScore = readability.fleschReadingEase(text);
// console.log('DEBUG - Raw Flesch score:', fleschScore);
this.calculateAndUpdate('FREs', () => fleschScore.toFixed(1));
this.calculateAndUpdate('FRDf', () => TextUtils.getDifficultyFromScore(fleschScore));
this.calculateAndUpdate('FKGL', () => readability.fleschKincaidGrade(text).toString());
}
private calculateOtherReadabilityIndices(text: string): void {
this.calculateAndUpdate('GFog', () => readability.gunningFog(text).toFixed(1));
this.calculateAndUpdate('SMOG', () => readability.smogIndex(text).toFixed(1));
this.calculateAndUpdate('FCST', () => TextUtils.getFORCAST(text).toFixed(1));
this.calculateAndUpdate('ARI', () => readability.automatedReadabilityIndex(text).toString());
this.calculateAndUpdate('CLI', () => readability.colemanLiauIndex(text).toString());
this.calculateAndUpdate('LWri', () => readability.linsearWriteFormula(text).toString());
this.calculateAndUpdate('NDCh', () => readability.daleChallReadabilityScore(text).toString());
}
private calculateCustomReadabilityMetrics(text: string, metrics: TextMetrics): void {
this.calculatePSKGrade(metrics);
this.calculateRixMetrics(text, metrics);
this.calculateLixMetrics(text, metrics);
this.calculateReadabilityRating(text);
}
private calculatePSKGrade(metrics: TextMetrics): void {
this.calculateAndUpdate('PSK', () => {
if (metrics.sentenceCount === 0) return '0';
const avgSentenceLength = metrics.wordCount / metrics.sentenceCount;
const avgSyllables = metrics.words.reduce((acc, word) => acc + syllableCount(word), 0) / metrics.wordCount;
return ((0.0778 * avgSentenceLength) + (0.0455 * avgSyllables) + 2.797).toFixed(2);
});
}
private calculateRixMetrics(text: string, metrics: TextMetrics): void {
const longWords = text.match(/\b\w{6,}\b/g) ?? [];
const rixScore = metrics.sentenceCount === 0 ? 0 : longWords.length / metrics.sentenceCount;
this.calculateAndUpdate('RIX', () => rixScore.toFixed(1));
this.calculateAndUpdate('RIXD', () => TextUtils.getRixDifficulty(rixScore));
}
private calculateLixMetrics(text: string, metrics: TextMetrics): void {
if (metrics.sentenceCount === 0 || metrics.wordCount === 0) {
this.calculateAndUpdate('LIX', () => '0');
this.calculateAndUpdate('LIXD', () => '');
return;
}
const longWords = metrics.words.filter(word => word.length > 6);
const lixScore = (metrics.wordCount / metrics.sentenceCount) + ((longWords.length / metrics.wordCount) * 100);
this.calculateAndUpdate('LIX', () => lixScore.toFixed(1));
this.calculateAndUpdate('LIXD', () => TextUtils.getLixDifficulty(lixScore));
}
private calculateReadabilityRating(text: string): void {
this.calculateAndUpdate('Rdbl', () => {
const score = parseFloat(readability.textMedian(text));
return TextUtils.getReadabilityGrade(score);
});
}
}
class TextUtils {
static getDifficultyFromScore(score: number): string {
if (score >= 90) return "Very Easy";
if (score >= 80) return "Easy";
if (score >= 70) return "Fairly Easy";
if (score >= 60) return "Standard";
if (score >= 50) return "Fairly Difficult";
if (score >= 30) return "Difficult";
return "Very Confusing";
}
static getFORCAST(text: string): number {
const words = text.match(/\b\p{L}(['\-\p{L}\p{N}]*\p{L})?\b/gu) ?? [];
const oneSyllableCount = words.filter(word => syllableCount(word) === 1).length;
const scaledOneSyllableCount = (oneSyllableCount / words.length) * 150;
return 20 - (scaledOneSyllableCount / 10);
}
static getReadingTimes(text: string): string {
const wordCount = (text.match(/\b\p{L}(['\-\p{L}\p{N}]*\p{L})?\b/gu) ?? []).length;
return `${this.convertToHMS(wordCount / 250)} - ${this.convertToHMS(wordCount / 200)}`;
}
static getSpeakingTimes(text: string): string {
const wordCount = (text.match(/\b\p{L}(['\-\p{L}\p{N}]*\p{L})?\b/gu) ?? []).length;
return `${this.convertToHMS(wordCount / 150)} - ${this.convertToHMS(wordCount / 125)}`;
}
static convertToHMS(minutes: number): string {
const totalSeconds = Math.round(minutes * 60);
const hours = Math.floor(totalSeconds / 3600);
const mins = Math.floor((totalSeconds % 3600) / 60);
const secs = totalSeconds % 60;
if (hours > 0) {
return `${hours}:${mins.toString().padStart(2, '0')}:${secs.toString().padStart(2, '0')}`;
}
if (mins > 0) {
return `${mins}:${secs.toString().padStart(2, '0')}`;
}
return `${secs}s`;
}
static getRixDifficulty(score: number): string {
if (score < 4) return 'Easy';
if (score < 6) return 'Moderate';
return 'Complex';
}
static getLixDifficulty(score: number): string {
if (score < 30) return 'Very easy';
if (score < 40) return 'Easy';
if (score < 50) return 'Medium';
if (score < 60) return 'Difficult';
return 'Very difficult';
}
static getReadabilityGrade(score: number): string {
if (score >= 7 && score <= 8) return 'A';
if ((score >= 6 && score < 7) || (score >= 8.1 && score < 9)) return 'A-';
if ((score >= 5 && score < 6) || (score >= 9 && score < 10)) return 'B+';
if ((score >= 4 && score < 5) || (score >= 10 && score < 11)) return 'B';
if ((score >= 3 && score < 4) || (score >= 11 && score < 12)) return 'B-';
if ((score >= 2 && score < 3) || (score >= 12 && score < 13)) return 'C+';
if ((score >= 1 && score < 2) || (score >= 13 && score < 14)) return 'C';
if (score === 1 || (score >= 14 && score < 15)) return 'C-';
if (score >= 15) return 'D';
return 'Invalid Score';
}
}