-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtermExtractor.js
More file actions
78 lines (65 loc) · 2.38 KB
/
termExtractor.js
File metadata and controls
78 lines (65 loc) · 2.38 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
export class TermExtractor {
constructor(wordposInstance, maxTerms = 5) {
this.wordpos = wordposInstance;
this.maxTerms = maxTerms;
}
async getAdjs(source) {
try {
const adjectives = await this.wordpos.getAdjectives(source);
// Filter out numerical values and empty strings
const filtered = adjectives
.filter(item => isNaN(item))
.filter(item => item.trim() !== '');
if (filtered.length === 0) {
return [];
}
// Choose random starting index
const maxStartIndex = Math.max(0, filtered.length - this.maxTerms);
const startIndex = Math.floor(Math.random() * (maxStartIndex + 1));
// Return a slice of terms up to maxTerms
return filtered.slice(startIndex, startIndex + this.maxTerms);
} catch (error) {
console.error('Error extracting adjectives:', error);
return [];
}
}
async getNouns(source) {
try {
const nouns = await this.wordpos.getNouns(source);
return this.filterAndRandomize(nouns);
} catch (error) {
console.error('Error extracting nouns:', error);
return [];
}
}
async getVerbs(source) {
try {
const verbs = await this.wordpos.getVerbs(source);
return this.filterAndRandomize(verbs);
} catch (error) {
console.error('Error extracting verbs:', error);
return [];
}
}
async getAdverbs(source) {
try {
const adverbs = await this.wordpos.getAdverbs(source);
return this.filterAndRandomize(adverbs);
} catch (error) {
console.error('Error extracting adverbs:', error);
return [];
}
}
// Helper method to filter and randomize results
filterAndRandomize(terms) {
const filtered = terms
.filter(item => isNaN(item))
.filter(item => item.trim() !== '');
if (filtered.length === 0) {
return [];
}
const maxStartIndex = Math.max(0, filtered.length - this.maxTerms);
const startIndex = Math.floor(Math.random() * (maxStartIndex + 1));
return filtered.slice(startIndex, startIndex + this.maxTerms);
}
}