shikeying
2024-01-11 3b67e947e36133e2a40eb2737b15ea375e157ea0
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
package com.walker.semantics;
 
import com.walker.semantics.util.SemanticsUtils;
import org.ansj.domain.Result;
import org.ansj.domain.Term;
import org.ansj.splitWord.analysis.DicAnalysis;
 
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
 
/**
 * 描述:用户输入指令语句对象
 * @author 时克英
 * @date 2020年11月20日 上午9:22:23
 */
 
public class InputWord {
 
    private String srcText;
    private List<WordMeta> wordList = new ArrayList<>(8);
    
    private int total = 0;    // 总分词数量(包含重复的)
    
//    // 以下为分析语义使用的属性
//    // 是否存在时间词,据此可决定是否解析时间
//    private boolean hasWordTime = false;
    
    // 添加属性,用于根据词的名称查找元数据,2020-12-23
    private Map<String, WordMeta> wordMetaCache = new HashMap<>(16);
    
    private int sceneContextId = 0;
    private String user;
    
    public InputWord(String srcWords){
        if(SemanticsUtils.isEmpty(srcWords)){
            throw new IllegalArgumentException("srcWords is required!");
        }
        this.srcText = srcWords;
 
        // 分词填充
        Result result = DicAnalysis.parse(this.srcText);
        SpeechPart sp = null;
        for(Term t : result.getTerms()){
            sp = SpeechPart.toSpeechPart(t.getNatureStr());
            if(sp.isFocus()){
                this.addWordMeta(new WordMeta(t.getName(), sp));
            }
        }
    }
 
    public void addWordMeta(WordMeta wm){
        this.wordMetaCache.put(wm.getText(), wm);
        // 这里允许词重复,做标记
        if(this.wordList.contains(wm)){
            wm.setDuplication(true);
        }
        wm.setIndex(total);
        this.wordList.add(wm);
        this.total++;
    }
    
    public String getSrcText(){
        return this.srcText;
    }
    
    public List<WordMeta> getWordMetaList(){
        return this.wordList;
    }
 
    /**
     * 共包含多少个词元(可能有重复的)
     * @return
     */
    public int getTotal() {
        return total;
    }
    
    /**
     * 返回词元的map对象,key = 单词
     * @return
     */
    public Map<String, WordMeta> getWordMetaMap(){
        return this.wordMetaCache;
    }
    
    /**
     * 返回语句的长度
     * @return
     */
    public int getTextLength(){
        return this.srcText.length();
    }
    
    /**
     * 根据索引返回单词词元
     * @param index
     * @return
     */
    public WordMeta getWordMeta(int index){
        if(index >= this.total){
            throw new IllegalArgumentException("超过索引,无法返回WordMeta:" + index + ", total=" + this.total);
        }
        return this.wordList.get(index);
    }
    
    @Override
    public String toString(){
        return this.wordList.toString();
    }
}