forked from BlueDome77/Turbowarp-Extension-List
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Text Analyser.js
98 lines (91 loc) · 3.05 KB
/
Text Analyser.js
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
class TextAnalyzer {
getInfo() {
return {
id: 'textanalyzer',
name: 'Text Analyzer',
blocks: [
{
opcode: 'wordCount',
blockType: 'reporter',
text: 'word count of [text]',
arguments: {
text: {
type: 'string',
defaultValue: 'Hello world'
}
}
},
{
opcode: 'characterCount',
blockType: 'reporter',
text: 'character count of [text]',
arguments: {
text: {
type: 'string',
defaultValue: 'Hello world'
}
}
},
{
opcode: 'averageWordLength',
blockType: 'reporter',
text: 'average word length of [text]',
arguments: {
text: {
type: 'string',
defaultValue: 'Hello world'
}
}
},
{
opcode: 'mostFrequentWords',
blockType: 'reporter',
text: 'most frequent words of [text] (limit: [limit])',
arguments: {
text: {
type: 'string',
defaultValue: 'Hello world'
},
limit: {
type: 'number',
defaultValue: 5
}
}
}
]
};
}
wordCount(args) {
const text = args.text;
const words = text.split(' ');
return words.length;
}
characterCount(args) {
const text = args.text;
return text.length;
}
averageWordLength(args) {
const text = args.text;
const words = text.split(' ');
const totalLength = words.reduce((sum, word) => sum + word.length, 0);
return totalLength / words.length;
}
mostFrequentWords(args) {
const text = args.text;
const words = text.toLowerCase().match(/\b\w+\b/g);
const limit = Math.max(1, Math.min(args.limit, words.length));
const wordCountMap = {};
for (let i = 0; i < words.length; i++) {
const word = words[i];
if (wordCountMap[word]) {
wordCountMap[word]++;
} else {
wordCountMap[word] = 1;
}
}
const sortedWords = Object.keys(wordCountMap).sort((a, b) => wordCountMap[b] - wordCountMap[a]);
const mostFrequent = sortedWords.slice(0, limit);
return mostFrequent.join(', ');
}
}
Scratch.extensions.register(new TextAnalyzer());