-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathsummarizer.js
More file actions
140 lines (102 loc) · 2.75 KB
/
summarizer.js
File metadata and controls
140 lines (102 loc) · 2.75 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
// summarizer.js
process.env.ORT_LOG_LEVEL = "error";
const { pipeline } = require('@xenova/transformers');
// CONFIG
const MODEL = 'Xenova/t5-small';
const MAX_CHUNK = 800; // characters per chunk
// STOPWORDS
const STOPWORDS = new Set([
"a","an","the","and","or","but","if","while","of","at","by","for","with",
"about","against","between","into","through","during","before","after",
"above","below","to","from","up","down","in","out","on","off","over",
"under","again","further","then","once","here","there","when","where",
"why","how","all","any","both","each","few","more","most","other","some",
"such","no","nor","not","only","own","same","so","than","too","very"
]);
// TEXT CLEANING
function stripHtml(html = "") {
return html.replace(/<[^>]*>/g, '');
}
function normalizeText(text) {
return stripHtml(text)
.replace(/\s+/g, ' ')
.trim();
}
// CHUNK LONG TEXT
function chunkText(text) {
const chunks = [];
let i = 0;
while (i < text.length) {
chunks.push(text.slice(i, i + MAX_CHUNK));
i += MAX_CHUNK;
}
return chunks;
}
// TITLE GENERATION
function generateTitle(summary) {
const words = summary
.toLowerCase()
.split(/\W+/)
.filter(w => w.length > 3 && !STOPWORDS.has(w));
const freq = {};
for (let w of words)
freq[w] = (freq[w] || 0) + 1;
const top = Object.entries(freq)
.sort((a,b)=>b[1]-a[1])
.slice(0,4)
.map(([w]) => w.charAt(0).toUpperCase()+w.slice(1));
return top.join(" ");
}
// SUMMARIZER CLASS
class Summarizer {
constructor(){
this.model = null;
this.loading = null;
}
async loadModel(){
if(this.model) return this.model;
if(!this.loading){
console.log("Loading news summarization model...");
this.loading = pipeline('summarization', MODEL);
}
this.model = await this.loading;
console.log("Summarizer ready.");
return this.model;
}
releaseModel(){
this.model = null;
this.loading = null;
}
// MAIN SUMMARIZE FUNCTION
async summarizeText(text){
await this.loadModel();
const clean = normalizeText(text);
// Skip model summary generation if text already short
if(clean.length < 250){
return {
title: generateTitle(clean),
summary: clean
};
}
const chunks = chunkText(clean);
const partial = [];
for(const chunk of chunks){
const result = await this.model(chunk,{
max_length:80,
min_length:25
});
partial.push(result[0].summary_text);
}
const combined = partial.join(" ");
const final = await this.model(combined,{
max_length:90,
min_length:35
});
const summary = final[0].summary_text;
return {
title: generateTitle(summary),
summary
};
}
}
module.exports = Summarizer;