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
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
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
package com.walker.semantics.support;
 
import com.walker.semantics.SemanticsException;
import com.walker.semantics.SemanticsManager;
import com.walker.semantics.SpeechPart;
import org.ansj.domain.Result;
import org.ansj.library.DicLibrary;
import org.ansj.splitWord.analysis.DicAnalysis;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
 
/**
 * 描述:默认语义管理器实现
 * @author 时克英
 * @date 2020年11月10日 上午9:52:13
 */
 
public class DefaultSemanticsManager implements SemanticsManager {
 
    protected final transient Logger logger = LoggerFactory.getLogger(getClass());
 
    @Override
    public void startup() {
        // 内置一些特殊的指令,对于系统管理来说这些指令很难维护,所以直接集成
//        DicLibrary.insert(DicLibrary.DEFAULT, "在哪", SpeechPart.INDEX_MY_V, 1000);
        this.loadKeywordFromPersistence();
    }
 
    @Override
    public void registerKeyWord(int sceneContextId, String word, SpeechPart speechPart) throws SemanticsException {
        Result result = DicAnalysis.parse(word);
        int wordSize = result.getTerms().size();
        if(wordSize == 0){
            throw new SemanticsException("注册的关键词为空:" + word);
        }
//        logger.debug("====== " + result);
//
//        WordSet wordSet = new WordSet(result.getTerms());
//        boolean syncToVoice = false;
//
//        if(speechPart == SpeechPart.MY_V){
//            syncToVoice = this.doRegisterInstruction(wordSet);
//            logger.info("注册了指令词:" + wordSet);
//
//        } else if(speechPart == SpeechPart.MY_N){
//            syncToVoice = this.doRegisterResourceObject(wordSet);
//            logger.info("注册了资源词:" + wordSet);
//
//        } else if(speechPart == SpeechPart.MY_PLACE){
//            syncToVoice = this.doRegisterResourcePlace(wordSet);
//            logger.info("注册了地点词:" + wordSet);
//
//        } else {
//            throw new InteracupException("注册关键词未支持类型:" + speechPart.name());
//        }
 
        boolean syncToVoice = false;
 
        if(speechPart == SpeechPart.MY_V){
//            if(wordSet.isInstruction()){
//                throw new InteracupException("已经是自定义指令词了,无需重复定义:" + wordSet);
//            }
            DicLibrary.insert(DicLibrary.DEFAULT, word, SpeechPart.INDEX_MY_V, 1000);
 
        } else if(speechPart == SpeechPart.MY_N){
//            if(wordSet.isResource()){
//                throw new InteracupException("当前词已经是资源词,无需在重复定义:" + wordSet);
//            }
            DicLibrary.insert(DicLibrary.DEFAULT, word, SpeechPart.INDEX_MY_N, 1000);
 
        } else if(speechPart == SpeechPart.MY_PLACE){
//            if(wordSet.isPlace()){
//                throw new InteracupException("当前词已经是地点词,无需在重复定义:" + wordSet);
//            }
            DicLibrary.insert(DicLibrary.DEFAULT, word, SpeechPart.INDEX_MY_PLACE, 100);
 
        } else if(speechPart == SpeechPart.MY_V_AUX){
            // 辅助指令词
            DicLibrary.insert(DicLibrary.DEFAULT, word, SpeechPart.INDEX_MY_V_AUX, 1000);
 
        } else if(speechPart == SpeechPart.MY_ROAD){
            // 辅助指令词
            DicLibrary.insert(DicLibrary.DEFAULT, word, SpeechPart.INDEX_MY_ROAD, 200);
 
        } else {
            throw new SemanticsException("注册关键词未支持类型:" + speechPart.name());
        }
//        logger.info("注册了关键词:" + word);
 
        // 业务回调
        this.afterRegisterKeyWord(word, speechPart);
 
        // 同步到语音库
        if(syncToVoice){
            logger.info("已同步到语音库:" + word);
        }
    }
 
    @Override
    public void removeKeyWord(int sceneContextId, String word) {
        DicLibrary.delete(DicLibrary.DEFAULT, word);
    }
 
//    /**
//     * 注册自定义指令关键词
//     * @param wordSet
//     * @return
//     * @throws InteracupException
//     */
//    private boolean doRegisterInstruction(WordSet wordSet) throws InteracupException{
//        if(wordSet.size() > 1){
//            throw new InteracupException("指令关键词一次只能添加一个,当前:" + wordSet.size() + "个");
//        }
//        if(wordSet.isInstruction()){
////            throw new InteracupException("已经是自定义指令词了,无需重复定义:" + wordSet);
//            logger.warn("已经是自定义指令词了,无需重复定义:" + wordSet);
//            return false;
//        }
//        if(!wordSet.hasAvailableInstruction()){
//            throw new InteracupException("不存在可用的指令词!指令词只能是动词或名词,请重新输入:" + wordSet);
//        }
//        DicLibrary.insert(DicLibrary.DEFAULT, wordSet.getFirstText(), SpeechPart.INDEX_MY_V, 1000);
//        return false;
//    }
//
//    /**
//     * 注册自定义活动资源名称,非地点类,如:车辆、行人、道路
//     * @param wordSet
//     * @return
//     */
//    private boolean doRegisterResourceObject(WordSet wordSet) throws InteracupException{
//        if(wordSet.isResource()){
//            throw new InteracupException("当前词已经是资源词,无需在重复定义:" + wordSet);
//        }
//        boolean syncToVoice = false;
//        if(wordSet.size() > 1){
//            // 如果是多个词联合,需要同步到语音输入库中
//            syncToVoice = true;
//        }
//        DicLibrary.insert(DicLibrary.DEFAULT, wordSet.getText(), SpeechPart.INDEX_MY_N, 1000);
//        return syncToVoice;
//    }
//
//    private boolean doRegisterResourcePlace(WordSet wordSet) throws InteracupException{
//        if(wordSet.isPlace()){
//            throw new InteracupException("当前词已经是地点词,无需在重复定义:" + wordSet);
//        }
//        boolean syncToVoice = false;
//        if(wordSet.size() > 1){
//            // 如果是多个词联合,需要同步到语音输入库中
//            syncToVoice = true;
//        }
//        DicLibrary.insert(DicLibrary.DEFAULT, wordSet.getText(), SpeechPart.INDEX_MY_PLACE, 1000);
//        return syncToVoice;
//    }
 
    /**
     * 从持久化存储中,加载已有的自定义关键词
     */
    protected void loadKeywordFromPersistence(){
        DicLibrary.insert(DicLibrary.DEFAULT, "盖亚", SpeechPart.INDEX_MY_N, 1000);
        // 时间词
        DicLibrary.insert(DicLibrary.DEFAULT, "前天", SpeechPart.INDEX_T_TIME, 1000);
        DicLibrary.insert(DicLibrary.DEFAULT, "后天", SpeechPart.INDEX_T_TIME, 1000);
        DicLibrary.insert(DicLibrary.DEFAULT, "大后天", SpeechPart.INDEX_T_TIME, 1000);
    }
 
    /**
     * 注册自定义关键词之后,业务可回调
     * @param text
     * @param speechPart
     */
    protected void afterRegisterKeyWord(String text, SpeechPart speechPart){
 
    }
 
    /**
     * 删除自定义关键词之后,业务可回调
     * @param text
     */
    protected void afterRemoveKeyWord(String text){
 
    }
 
    /**
    private class WordSet{
        private List<WordMeta> textList = new ArrayList<>(4);
        public WordSet(List<Term> terms){
            SpeechPart speechPart = null;
            WordMeta wordMeta = null;
            for(Term t : terms){
                speechPart = SpeechPart.toSpeechPart(t.getNatureStr());
                // 只处理关注的词性
//                if(speechPart.isFocus()){
                    wordMeta = new WordMeta(t.getName(), speechPart);
                    if(!this.textList.contains(wordMeta)){
                        this.textList.add(wordMeta);
                    }
//                }
            }
        }
 
        public int size(){
            return this.textList.size();
        }
 
        public boolean isInstruction(){
            if(this.size() == 1 && this.textList.get(0).isInstruction()){
                return true;
            }
            return false;
        }
 
        public boolean isPlace(){
            if(this.size() == 1 && this.textList.get(0).isPlace()){
                return true;
            }
            return false;
        }
 
        public boolean isResource(){
            if(this.size() == 1 && this.textList.get(0).isResource()){
                return true;
            }
            return false;
        }
 
        @Override
        public String toString(){
            StringBuilder sb = new StringBuilder("[");
            for(WordMeta wm : this.textList){
                sb.append(wm.getText()).append(StringUtils.DEFAULT_SPLIT_SEPARATOR);
            }
            sb.append("]");
            return sb.toString();
        }
    }
*/
}