generated from obsidianmd/obsidian-sample-plugin
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.ts
640 lines (549 loc) · 17.3 KB
/
main.ts
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
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
import {
App,
SuggestModal,
Plugin,
PluginSettingTab, Setting, Editor, Notice
} from 'obsidian';
import OpenAI from 'openai';
// Load wink-nlp package.
import WinkNLP from 'wink-nlp';
import {ItemToken} from 'wink-nlp';
// Load english language model.
import model from 'wink-eng-lite-web-model';
// Instantiate winkNLP.
const winkNLP = WinkNLP;
const nlp = winkNLP(model);
// Obtain "its" helper to extract item properties.
const its = nlp.its;
// ===============================================================
function NaiveBayesianClassifier() {
if (!(this instanceof NaiveBayesianClassifier)) {
// @ts-ignore
return new NaiveBayesianClassifier()
}
this.docs = [];
// set
this.classes = new Set([]);
this.loglikelihood = {};
this.logprior = {};
this.vocabulary = new Set([]);
return this;
}
NaiveBayesianClassifier.prototype.tokenize = function (text: string) {
// let words = text.split(/[^A-Za-z/ ]/)
// words = words.filter((w) => w.length > 0)
const doc = nlp.readDoc(text)
// console.log(doc.tokens().out(its.type))
let words = doc.tokens().filter((t: ItemToken) => t.out(its.type) === 'word').out(its.normal);
// handle url
const urls = doc.tokens().filter((t: ItemToken) => t.out(its.type) === 'url').out(its.normal);
const url_stop_list = ["http", "https", "www", "com", "org", "co", "net", "gov", "edu", "uk", "au", "ca", "us", "in", "io", "info", "biz", "name", "blog", "app", "appspot", "appspot.com", "code"]
urls.forEach((url: string) => {
let url_words = url.split(/[^A-Za-z0-9 ]/)
url_words = url_words.filter((w) => w.length > 0 && !url_stop_list.includes(w))
words = words.concat(url_words)
})
return words
}
NaiveBayesianClassifier.prototype.addDocument = function (doc: string | string[], target: string) {
// add target to classes if target not in classes
if (!this.classes.has(target)) {
this.classes.add(target)
}
let words = doc
if (typeof (doc) == "string") {
words = this.tokenize(doc)
}
if (words.length == 0) {
// console.log("empty doc " + target + " " + doc)
return
}
// add doc to docs
this.docs.push({
value: words,
label: target
})
}
NaiveBayesianClassifier.prototype.train = function () {
const classprior = {}
// classwise words
const bigDoc: { [key: string]: string[] } = {};
this.docs.forEach((doc: { label: string | number; value: string[]; }) => {
// count documents per class
// @ts-ignore
classprior[doc.label] = classprior[doc.label] || 0
// @ts-ignore
classprior[doc.label] += 1
// collect words into class
//@ts-ignore
bigDoc[doc.label] = bigDoc[doc.label] || []
// @ts-ignore
bigDoc[doc.label] = bigDoc[doc.label].concat(doc.value)
// collect words into vocabulary
doc.value.forEach(this.vocabulary.add, this.vocabulary);
})
// normalize
for (const [class_, value] of Object.entries(classprior)) {
// @ts-ignore
this.logprior[class_] = Math.log(value / this.docs.length)
}
// console.log(this.logprior)
// loop over vocabulary
for (const [class_] of Object.entries(classprior)) {
const class_counter: { [key: string]: number } = {}
bigDoc[class_].forEach((item: string) => {
class_counter[item] = (class_counter[item] || 0) + 1
});
let denominator = 0
// console.log(class_counter)
this.vocabulary.forEach((word: string) => {
denominator += (class_counter[word] || 0) + 1
})
// @ts-ignore
for (const word of this.vocabulary) {
this.loglikelihood[word] = this.loglikelihood[word] || {}
const count_w_c = class_counter[word] || 0
// @ts-ignore
this.loglikelihood[word][class_] = Math.log((count_w_c + 1) / denominator);
}
}
// console.log("training.")
}
NaiveBayesianClassifier.prototype.classify = function (doc: string | string[]) {
const class_scores = {}
if (typeof (doc) == "string") {
doc = this.tokenize(doc)
}
for (const [class_, value] of Object.entries(this.logprior)) {
// @ts-ignore
class_scores[class_] = value
for (const word of doc) {
if (this.vocabulary.has(word)) {
// @ts-ignore
class_scores[class_] += this.loglikelihood[word][class_]
}
}
}
const classes: [string, number][] = Object.entries(class_scores)
classes.sort((a: [string, number], b: [string, number]) => b[1] - a[1])
const sorted_classes = classes.map((item) => item[0])
return sorted_classes
}
NaiveBayesianClassifier.prototype.addDocuments = function (texts: string[], target: string) {
for (const text in texts) {
this.docs.push({
text: text,
target: target
})
}
}
// let nbc = NaiveBayesianClassifier()
// ===============================================================
interface TextMoverPluginSettings {
mySetting: string;
}
interface TextMoverPluginSettings {
mySetting: string;
classifier: string;
openAIapiKey: string;
modelName: string;
maxTokens: number;
}
const DEFAULT_SETTINGS: TextMoverPluginSettings = {
mySetting: "default",
classifier: "",
openAIapiKey: "",
modelName: "gpt-3.5-turbo",
maxTokens: 20,
};
interface Heading {
heading: string;
level: number;
position: object;
}
export class HeadingSuggestionModal extends SuggestModal<Heading> {
// Returns all available suggestions.
headings: Heading[];
result: object
onSubmit: (result: object) => void;
constructor(app: App, headings: Heading[], onSubmit: (result: object) => void) {
super(app);
this.headings = headings;
this.onSubmit = onSubmit;
}
onOpen() {
// console.log("inside onOpen");
super.onOpen();
}
getSuggestions(query: string): Heading[] {
return this.headings.filter((item) =>
item.heading.toLowerCase().includes(query.toLowerCase())
);
}
// Renders each suggestion item.
renderSuggestion(heading: Heading, el: HTMLElement) {
el.createEl("div", {text: heading.heading});
}
// Perform action on the selected suggestion.
onChooseSuggestion(heading: Heading, evt: MouseEvent | KeyboardEvent) {
this.onSubmit(heading);
}
}
export default class TextMoverPlugin extends Plugin {
settings: TextMoverPluginSettings;
llm_client: OpenAI;
get_file_headings(file: any) {
const filecache = this.app.metadataCache.getFileCache(file)
let headings: Heading[] = [];
if (filecache && filecache.headings) {
headings = filecache.headings.map(headingCache => {
return {
heading: headingCache.heading,
level: headingCache.level,
position: headingCache.position
};
});
}
// sort headings alphabetically
headings.sort((a, b) => a.heading.localeCompare(b.heading))
return headings
}
process_headings_from_llm_response(llm_response: string | null) {
// alert notice the class
// split classes by ", " or newline
let chosen_classses: string[] = []
if (llm_response == null || llm_response.length == 0) {
chosen_classses = []
} else if (llm_response.includes("\n")) {
chosen_classses = llm_response.split("\n")
} else if (llm_response.includes(", ")) {
chosen_classses = llm_response.split(", ")
} else if (llm_response.length > 0) {
chosen_classses = llm_response.split(", ")
}
// strip non alphabetic characters
chosen_classses = chosen_classses.map((item: string) => item.trim().replace(/[^a-zA-Z /]/g, '').trim())
console.log(chosen_classses);
return chosen_classses
}
async sort_headings_via_llm(headings: Heading[], selection = "", editor: Editor, callback: {
(): void;
(arg0: Heading[], arg1: string, arg2: Editor): void;
}) {
if (!this.settings.openAIapiKey) {
new Notice("OpenAI API key not set in the plugin settings. Returning default headings.")
callback(headings, selection, editor)
return
}
const heading_str = headings.map(heading => heading.heading).join(", ")
const prompt = "For the text below, suggest top3 classes from one of the following classes(no fluff, no explaination, no numbering just the classes): \n" +
"```\n " +
"classes:" + heading_str + "\n" +
"text: " + selection
// api post call to openai
await this.llm_client.chat.completions.create({
messages: [{role: 'user', content: prompt}],
model: this.settings.modelName,
}).then(
(response) => {
const llm_response = response.choices[0].message.content
const chosen_classses = this.process_headings_from_llm_response(llm_response)
headings.sort((a, b) => {
return -(chosen_classses.indexOf(a.heading) - chosen_classses.indexOf(b.heading));
})
// return headings
callback(headings, selection, editor)
},
(error) => {
// console.log("c2")
new Notice("Error: " + error.message)
callback(headings, selection, editor)
}
);
}
async sort_headings_via_bayesian(headings: Heading[], training_instances: object[], selection = "") {
const nbc = NaiveBayesianClassifier();
// loop over training instances
for (const instance of training_instances) {
// @ts-ignore
nbc.addDocument(instance.input, instance.label)
}
nbc.train()
const sorted_classes = nbc.classify(selection)
console.log(sorted_classes);
// add missing headings to sorted classes
headings.forEach((heading) => {
if (!sorted_classes.includes(heading.heading)) {
sorted_classes.push(heading.heading)
}
})
headings.sort((a, b) => {
return (sorted_classes.indexOf(a.heading) - sorted_classes.indexOf(b.heading))
})
return headings
}
get_action_line(editor: Editor) {
const selection_detail = editor.listSelections()
// console.log(selection_detail)
let line_no = null
selection_detail.forEach(es => {
// loop through selected lines
const min = Math.min(es.anchor.line, es.head.line), max = Math.max(es.anchor.line, es.head.line)
for (let i = min; i <= max; i++) {
const line_text = editor.getLine(i).trim()
if (line_text == "" || line_text.startsWith("#")) {
// skip empty lines and headings
// console.log("skipping line: " + line_text)
continue
} else {
line_no = i
break
}
}
})
return line_no
}
modal_submit_callback(result: object, editor: Editor) {
// choose first valid line as actionable line
// idle behaviour, act on all lines
const action_line = this.get_action_line(editor)
if (action_line == null) {
return
}
const selection = editor.getLine(action_line)
// concatenate all headings with ,
// @ts-ignore
const source_start = {"line": action_line, "ch": 0}
const source_end = {"line": action_line, "ch": selection.length}
// @ts-ignore
const targetPosition = {"line": result.position.end.line + 1, "ch": 0}
if (source_start.line > targetPosition.line) {
editor.replaceRange("", source_start, source_end)
editor.replaceRange(`${selection}\n`, targetPosition)
} else {
editor.replaceRange(`${selection}\n`, targetPosition)
editor.replaceRange("", source_start, source_end)
}
// @ts-ignore
new Notice("Moved text to heading: " + result.heading)
}
getTrainingInstancesFromFile(editor: Editor, file: any) {
const filecache = this.app.metadataCache.getFileCache(file)
let headings: Heading[] = [];
if (filecache && filecache.headings) {
headings = filecache.headings.map(headingCache => {
return {
heading: headingCache.heading,
level: headingCache.level,
position: headingCache.position
};
});
}
if (typeof (headings) == "undefined" || headings == null || headings.length == 0) {
return []
}
// @ts-ignore
const lastLine: number = headings.last().position.end.line
// generate difference array
const line_heading_map: number[] = Array.apply(0, Array(lastLine)).map(Number.prototype.valueOf, 0)
// console.log(lastLine)
for (let i = 1, prev_heading = 0; i < headings.length; i++) {
// @ts-ignore
line_heading_map[headings[i].position.end.line] = headings[i].position.end.line - prev_heading
// @ts-ignore
prev_heading = headings[i].position.end.line
}
// generate actual array from difference array
const actual_line_heading_map: number[] = []
for (let i = 0; i < line_heading_map.length; i++) {
if (i == 0) {
actual_line_heading_map[i] = line_heading_map[i]
} else {
actual_line_heading_map[i] = actual_line_heading_map[i - 1] + line_heading_map[i]
}
}
const training_instances: object[] = []
actual_line_heading_map.map((ele, idx: number) => {
let input = editor.getLine(idx)
// replace numerical prefixes from input with ""
input = input.replace(/\d+\./g, "")
input = input.replace(/#+ /g, "")
// remove trailing whitespace
input = input.trim()
let label = editor.getLine(ele)
// replace preceeding # and spaces with ""
label = label.replace(/#+ /g, "")
if (input != "" && label != "" && input != label) {
training_instances.push({
"input": input,
"label": label
})
}
})
// filter training examples with empty input or target
// @ts-ignore
// training_instances = training_instances.filter(ele => ele.input != "" && ele.target != "")
// console.log(training_instances)
return training_instances
}
async editorcallback() {
const editor = this.app.workspace.activeEditor?.editor
if (!editor) {
return
}
const action_line = this.get_action_line(editor)
if (action_line == null) {
return
}
const selection = editor.getLine(action_line)
const file = this.app.workspace.getActiveFile()
// get headings from file
if (!file) {
return
}
let headings = this.get_file_headings(file)
// sort headings via LLM call
if (this.settings.classifier == "llm") {
await this.sort_headings_via_llm(headings, selection, editor, () => {
const hmodal = new HeadingSuggestionModal(this.app, headings, (result) => {
this.modal_submit_callback(result, editor)
}
);
hmodal.setPlaceholder(selection);
hmodal.open()
})
return
} else if (this.settings.classifier == "nbc") {
const training_instances = this.getTrainingInstancesFromFile(editor, file)
headings = await this.sort_headings_via_bayesian(headings, training_instances, selection)
}
const hmodal = new HeadingSuggestionModal(this.app, headings, (result) => {
this.modal_submit_callback(result, editor)
}
);
hmodal.setPlaceholder(selection);
hmodal.open()
}
async onload() {
await this.loadSettings();
await this.build_api()
this.addCommand(
{
"id": "move-text-to-heading",
"name": "Move text to heading",
editorCallback: (editor: Editor) => {
// get activeEditor
this.editorcallback()
}
}
)
this.registerEvent(
this.app.workspace.on("editor-menu", (menu, editor, view) => {
menu.addItem((item) => {
item
.setTitle("Move text to heading")
.setIcon("document")
.onClick(async () => {
this.editorcallback();
});
})
}))
// for debugging
// This creates an icon in the left ribbon.
// this.addRibbonIcon('dice', 'Log training examples', (evt: MouseEvent) => {
// const editor = this.app.workspace.getActiveViewOfType(MarkdownView)?.editor
// if (editor == null) {
// return
// }
// const file = this.app.workspace.getActiveFile()
// // get headings from file
// if (!file) {
// return
// }
//
// let training_instances = this.getTrainingInstancesFromFile(editor, file)
// // console.log(training_instances)
// });
//
this.addSettingTab(new TextMoverSettingTab(this.app, this));
}
onunload() {
}
async loadSettings() {
this.settings = Object.assign({}, DEFAULT_SETTINGS, await this.loadData());
}
async saveSettings() {
await this.saveData(this.settings);
}
async build_api() {
// console.log("build api here.")
if (this.settings.classifier == "llm") {
this.llm_client = new OpenAI({
apiKey: this.settings.openAIapiKey, // This is the default and can be omitted
dangerouslyAllowBrowser: true,
});
// console.log(this.llm_client)
}
}
}
class TextMoverSettingTab extends PluginSettingTab {
plugin: TextMoverPlugin;
constructor(app: App, plugin: TextMoverPlugin) {
super(app, plugin);
this.plugin = plugin;
}
display(): void {
const {containerEl} = this;
containerEl.empty();
new Setting(containerEl)
.setName("Classifier")
.setDesc("Select classifier to get smart suggestions")
.addDropdown((dropdown) => dropdown.addOptions({
"": "No classifier",
"nbc": "Bayesian",
"llm": "LLM",
}).setValue(this.plugin.settings.classifier)
.onChange(async (value) => {
this.plugin.settings.classifier = value;
await this.plugin.saveSettings();
if (value == "llm") {
this.plugin.build_api();
}
api_key_setting.setDisabled(!(value == "llm"));
model_name_setting.setDisabled(!(value == "llm"));
}));
new Setting(containerEl).setName('LLM').setHeading();
const api_key_setting = new Setting(containerEl).setName("OpenAI API Key").addText((text) =>
text
.setPlaceholder("Enter OpenAI key here")
.setValue(this.plugin.settings.openAIapiKey)
.setDisabled(this.plugin.settings.classifier != "llm")
.onChange(async (value) => {
this.plugin.settings.openAIapiKey = value;
await this.plugin.saveSettings();
if (this.plugin.settings.classifier == "llm") {
this.plugin.build_api();
}
}),
);
const model_name_setting = new Setting(containerEl)
.setName("Model name")
.setDesc("Select your model")
.addDropdown((dropdown) =>
dropdown
.addOptions({
"gpt-3.5-turbo": "gpt-3.5-turbo",
"gpt-4": "gpt-4",
})
.setValue(this.plugin.settings.modelName)
.setDisabled(this.plugin.settings.classifier != "llm")
.onChange(async (value) => {
this.plugin.settings.modelName = value;
await this.plugin.saveSettings();
if (this.plugin.settings.classifier == "llm") {
this.plugin.build_api();
}
})
);
}
}