-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathscript.js
More file actions
84 lines (73 loc) · 1.89 KB
/
script.js
File metadata and controls
84 lines (73 loc) · 1.89 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
function swindow(list, size) {
return _.reduce(
_.range(list.length - size + 1),
(current, index) => {
return _.concat(current, _.join(_.slice(list, index, index + size), ""));
},
[]
);
}
function posting_build(postings, token_list) {
return _.reduce(
_.filter(token_list, (token) => token.length > 0),
(current, incoming) => _.update(current, incoming, (n) => (n ? n + 1 : 1)),
postings
);
}
function ngram_breaker(n, content_list) {
return _.reduce(
content_list,
(current, incoming) => posting_build(current, swindow(incoming, n)),
{}
);
}
function phrase_breaker(content) {
return _.reduce(
_.trim(content).split(/\r?\n/),
(current, incoming) => {
return _.concat(
current,
_.filter(
_.trim(incoming).split(/[,。「」?!;:『』()——[]……\s]/),
(phrase) => _.trim(phrase).length > 0
)
);
},
[]
);
}
function populate(elements, container) {
container.empty().append(elements);
}
function tokens_build(tokens) {
let [min, max] = _.over([Math.min, Math.max]).apply({}, _.values(tokens)),
step = (max - min) / 6;
return _.map(tokens, (count, token) => {
return $($("#badge-template").html())
.addClass(`fs-${7 - Math.floor((count - min) / step)}`)
.prepend(`${token} `)
.find("span.bg-secondary")
.append(count)
.end();
});
}
function filter_n(n) {
return (x) => x > $(`#${n}gram`).parent().find("select").val();
}
$(() => {
$("#content").keyup((e) => {
let phrases = phrase_breaker(e.target.value);
_.forEach(_.range(1, 5), (n) => {
populate(
tokens_build(_.pickBy(ngram_breaker(n, phrases), filter_n(n))),
$(`#${n}gram`)
);
});
});
$(".card select").change(() => {
$("#content").keyup();
});
$("#refresh").click(() => {
$("#content").keyup();
});
});