Authored by Gino Zhang

consumer支持对同义词的增删改操作

1 package com.yoho.search.consumer.dynwords; 1 package com.yoho.search.consumer.dynwords;
2 2
3 -import java.util.ArrayList;  
4 -import java.util.HashMap;  
5 -import java.util.HashSet;  
6 -import java.util.LinkedHashMap;  
7 -import java.util.List;  
8 -import java.util.Map;  
9 -import java.util.Set;  
10 -  
11 -import javax.servlet.http.HttpServletRequest;  
12 -  
13 import org.slf4j.Logger; 3 import org.slf4j.Logger;
14 import org.slf4j.LoggerFactory; 4 import org.slf4j.LoggerFactory;
15 import org.springframework.beans.factory.annotation.Autowired; 5 import org.springframework.beans.factory.annotation.Autowired;
16 import org.springframework.stereotype.Controller; 6 import org.springframework.stereotype.Controller;
  7 +import org.springframework.util.Assert;
17 import org.springframework.web.bind.annotation.RequestMapping; 8 import org.springframework.web.bind.annotation.RequestMapping;
18 import org.springframework.web.bind.annotation.ResponseBody; 9 import org.springframework.web.bind.annotation.ResponseBody;
19 10
  11 +import javax.servlet.http.HttpServletRequest;
  12 +import java.util.*;
  13 +
20 @Controller 14 @Controller
21 public class DynSegWordsToolsController { 15 public class DynSegWordsToolsController {
22 16
@@ -32,6 +26,7 @@ public class DynSegWordsToolsController { @@ -32,6 +26,7 @@ public class DynSegWordsToolsController {
32 private ESWordsService esWordsService; 26 private ESWordsService esWordsService;
33 27
34 private static Map<String, Integer> allDictFiles = new LinkedHashMap<String, Integer>(); 28 private static Map<String, Integer> allDictFiles = new LinkedHashMap<String, Integer>();
  29 +
35 static { 30 static {
36 // 词的类型 1为主词典的词 2为停用词 3为姓氏 4为量词 5为后缀词 6为介词 31 // 词的类型 1为主词典的词 2为停用词 3为姓氏 4为量词 5为后缀词 6为介词
37 allDictFiles.put("main.dic", Integer.valueOf(1)); // IK主词典 32 allDictFiles.put("main.dic", Integer.valueOf(1)); // IK主词典
@@ -147,27 +142,23 @@ public class DynSegWordsToolsController { @@ -147,27 +142,23 @@ public class DynSegWordsToolsController {
147 if (request.getParameter("words") != null && request.getParameter("words").trim().length() > 0) { 142 if (request.getParameter("words") != null && request.getParameter("words").trim().length() > 0) {
148 words = request.getParameter("words").trim(); 143 words = request.getParameter("words").trim();
149 } 144 }
  145 + Assert.isTrue(words != null && !words.trim().isEmpty(), "Words cannot be null.");
150 146
151 String wordType = "1"; 147 String wordType = "1";
152 if (request.getParameter("wordType") != null && request.getParameter("wordType").trim().length() > 0) { 148 if (request.getParameter("wordType") != null && request.getParameter("wordType").trim().length() > 0) {
153 wordType = request.getParameter("wordType").trim(); 149 wordType = request.getParameter("wordType").trim();
154 } 150 }
155 151
156 - if ("7".equals(wordType)) {  
157 - // 调用/dictTools/addNewSynonymRule添加同义词  
158 - rtnMap.put("code", "404");  
159 - rtnMap.put("msg", "invalid word type");  
160 - logger.info("[DynSegWords]DynSegWordsToolsController.addNewWords end. cost: {}",  
161 - System.currentTimeMillis() - begin);  
162 - return rtnMap;  
163 - }  
164 -  
165 Set<String> wordSet = new HashSet<String>(); 152 Set<String> wordSet = new HashSet<String>();
  153 + if ("7".equals(wordType)) {
  154 + wordSet.add(words.trim());
  155 + } else {
166 for (String word : words.split(",")) { 156 for (String word : words.split(",")) {
167 if (word != null && word.trim().length() > 0) { 157 if (word != null && word.trim().length() > 0) {
168 wordSet.add(word.trim()); 158 wordSet.add(word.trim());
169 } 159 }
170 } 160 }
  161 + }
171 162
172 Set<String> addedWords = esWordsService.addNewWords(wordSet, Integer.valueOf(wordType)); 163 Set<String> addedWords = esWordsService.addNewWords(wordSet, Integer.valueOf(wordType));
173 rtnMap.put("code", "200"); 164 rtnMap.put("code", "200");
@@ -189,27 +180,23 @@ public class DynSegWordsToolsController { @@ -189,27 +180,23 @@ public class DynSegWordsToolsController {
189 if (request.getParameter("words") != null && request.getParameter("words").trim().length() > 0) { 180 if (request.getParameter("words") != null && request.getParameter("words").trim().length() > 0) {
190 words = request.getParameter("words").trim(); 181 words = request.getParameter("words").trim();
191 } 182 }
  183 + Assert.isTrue(words != null && !words.trim().isEmpty(), "Words cannot be null.");
192 184
193 String wordType = "1"; 185 String wordType = "1";
194 if (request.getParameter("wordType") != null && request.getParameter("wordType").trim().length() > 0) { 186 if (request.getParameter("wordType") != null && request.getParameter("wordType").trim().length() > 0) {
195 wordType = request.getParameter("wordType").trim(); 187 wordType = request.getParameter("wordType").trim();
196 } 188 }
197 189
198 - if ("7".equals(wordType)) {  
199 - // 调用/dictTools/addNewSynonymRule添加同义词  
200 - rtnMap.put("code", "404");  
201 - rtnMap.put("msg", "invalid word type");  
202 - logger.info("[DynSegWords]DynSegWordsToolsController.reactiveWords end. cost: {}",  
203 - System.currentTimeMillis() - begin);  
204 - return rtnMap;  
205 - }  
206 -  
207 Set<String> wordSet = new HashSet<String>(); 190 Set<String> wordSet = new HashSet<String>();
  191 + if ("7".equals(wordType)) {
  192 + wordSet.add(words.trim());
  193 + } else {
208 for (String word : words.split(",")) { 194 for (String word : words.split(",")) {
209 if (word != null && word.trim().length() > 0) { 195 if (word != null && word.trim().length() > 0) {
210 wordSet.add(word.trim()); 196 wordSet.add(word.trim());
211 } 197 }
212 } 198 }
  199 + }
213 200
214 esWordsService.reactiveWords(wordSet, Integer.valueOf(wordType)); 201 esWordsService.reactiveWords(wordSet, Integer.valueOf(wordType));
215 rtnMap.put("code", "200"); 202 rtnMap.put("code", "200");
@@ -231,12 +218,23 @@ public class DynSegWordsToolsController { @@ -231,12 +218,23 @@ public class DynSegWordsToolsController {
231 words = request.getParameter("words").trim(); 218 words = request.getParameter("words").trim();
232 } 219 }
233 220
  221 + Assert.isTrue(words != null && !words.trim().isEmpty(), "Words cannot be null.");
  222 +
  223 + String wordType = "1";
  224 + if (request.getParameter("wordType") != null && request.getParameter("wordType").trim().length() > 0) {
  225 + wordType = request.getParameter("wordType").trim();
  226 + }
  227 +
234 List<String> list = new ArrayList<String>(); 228 List<String> list = new ArrayList<String>();
  229 + if ("7".equals(wordType)) {
  230 + list.add(words.trim());
  231 + } else {
235 for (String word : words.split(",")) { 232 for (String word : words.split(",")) {
236 if (word != null && word.trim().length() > 0) { 233 if (word != null && word.trim().length() > 0) {
237 list.add(word.trim()); 234 list.add(word.trim());
238 } 235 }
239 } 236 }
  237 + }
240 238
241 esWordsService.deleteWords(list); 239 esWordsService.deleteWords(list);
242 rtnMap.put("code", "200"); 240 rtnMap.put("code", "200");