UNPKG

2.33 kBJavaScriptView Raw
1'use strict';
2Object.defineProperty(exports, "__esModule", { value: true });
3exports.doOptimize = exports.init = exports.segment = exports.type = void 0;
4const const_1 = require("../mod/const");
5/** 模块类型 */
6exports.type = 'optimizer';
7/**
8 * 模块初始化
9 *
10 * @param {Segment} segment 分词接口
11 */
12function init(_segment) {
13 exports.segment = _segment;
14}
15exports.init = init;
16/**
17 * 日期时间优化
18 *
19 * @param {array} words 单词数组
20 * @param {bool} is_not_first 是否为管理器调用的
21 * @return {array}
22 */
23function doOptimize(words, is_not_first) {
24 if (typeof is_not_first == 'undefined') {
25 is_not_first = false;
26 }
27 // 合并相邻的能组成一个单词的两个词
28 const TABLE = exports.segment.getDict('TABLE');
29 const POSTAG = exports.segment.POSTAG;
30 let i = 0;
31 let ie = words.length - 1;
32 while (i < ie) {
33 let w1 = words[i];
34 let w2 = words[i + 1];
35 //debug(w1.w + ', ' + w2.w);
36 if ((w1.p & POSTAG.A_M) > 0) {
37 // =========================================
38 // 日期时间组合 数字 + 日期单位,如 “2005年"
39 if (w2.w in const_1.DATETIME) {
40 let nw = w1.w + w2.w;
41 let len = 2;
42 let ma = [w1, w2];
43 // 继续搜索后面连续的日期时间描述,必须符合 数字 + 日期单位
44 while (true) {
45 let w11 = words[i + len];
46 let w22 = words[i + len + 1];
47 if (w11 && w22 && (w11.p & POSTAG.A_M) > 0 && w22.w in const_1.DATETIME) {
48 len += 2;
49 nw += w11.w + w22.w;
50 ma.push(w11);
51 ma.push(w22);
52 }
53 else {
54 break;
55 }
56 }
57 words.splice(i, len, {
58 w: nw,
59 p: POSTAG.D_T,
60 m: ma,
61 });
62 ie -= len - 1;
63 continue;
64 }
65 // =========================================
66 }
67 // 移到下一个词
68 i++;
69 }
70 return words;
71}
72exports.doOptimize = doOptimize;
73//# sourceMappingURL=DatetimeOptimizer.js.map
\No newline at end of file