-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathbackground.js
More file actions
60 lines (50 loc) · 1.5 KB
/
background.js
File metadata and controls
60 lines (50 loc) · 1.5 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
let jiebaReady = false;
let jiebaInstance = null;
// 初始化jieba
initJieba();
function initJieba() {
try {
importScripts('./lib/jieba.js');
Jieba().then(instance => {
jiebaInstance = instance;
jiebaReady = true;
console.log('Jieba initialized successfully');
});
} catch (error) {
console.error('Failed to initialize Jieba:', error);
}
}
chrome.runtime.onMessage.addListener((request, sender, sendResponse) => {
if (request.action === "processChinese") {
processChinese(request.text).then(sendResponse);
return true;
}
});
async function processChinese(text) {
if (!jiebaReady) {
console.warn('Jieba is not ready yet');
return simpleProcessChinese(text);
}
try {
const words = jiebaInstance.tag(text);
return identifySVO(words);
} catch (error) {
console.error('Error processing Chinese text with Jieba:', error);
return simpleProcessChinese(text);
}
}
function simpleProcessChinese(text) {
// 简单的分词
const words = text.split(/([,。!?;:、\s])/);
return words.map(word => ({ text: word, important: word.length > 1 }));
}
function identifySVO(words) {
let result = [];
const importantTags = ['n', 'v', 'a', 'r', 'd']; // 名词、动词、形容词、代词、副词
for (let i = 0; i < words.length; i++) {
const { word, tag } = words[i];
let isImportant = importantTags.some(t => tag.startsWith(t)) && word.length > 1;
result.push({ text: word, important: isImportant });
}
return result;
}