001package com.aispeech.dui.dds.agent; 002 003import org.json.JSONArray; 004import org.json.JSONException; 005import org.json.JSONObject; 006 007import java.util.ArrayList; 008import java.util.List; 009 010/** 011 * 词库操作类 012 * <p> 013 * 可以对词库的词条进行增删改。 014 * <p> 015 * Created by Jinrui on 18-10-11. 016 * 017 * @see <a href="https://www.dui.ai/docs/ct_common_Andriod_SDK">https://www.dui.ai/docs/ct_common_Andriod_SDK</a> 018 */ 019 020public class VocabIntent { 021 public static final int LOCAL = 1; 022 public static final int CLOUD = 2; 023 public static final int ALL = LOCAL + CLOUD; 024 /** 025 * 词库操作:添加词条 026 */ 027 public static final String ACTION_INSERT = "ACTION_INSERT"; 028 029 /** 030 * 词库操作:删除词条 031 */ 032 public static final String ACTION_REMOVE = "ACTION_REMOVE"; 033 034 /** 035 * 词库操作:清空所有词条后,添加词条 036 */ 037 public static final String ACTION_CLEAR_AND_INSERT = "ACTION_CLEAR_AND_INSERT"; 038 039 /** 040 * 词库操作:清空所有词条 041 */ 042 public static final String ACTION_CLEAR_ALL = "ACTION_CLEAR_ALL"; 043 044 private String action; 045 private String name; 046 private List<String> contents; 047 private boolean needSegment; 048 private boolean numberExtension; 049 050 /** 051 * 获取当前词库支持阿拉伯数字转换,默认不支持 052 * 053 * @return 是否需要分词 054 */ 055 public boolean isNumberExtension() { 056 return numberExtension; 057 } 058 059 /** 060 * 设置当前词库是否支持阿拉伯数字转换 061 * 062 * @param enableNumberExtension true or false 063 */ 064 public VocabIntent setNumberExtension(boolean enableNumberExtension) { 065 this.numberExtension = enableNumberExtension; 066 return this; 067 } 068 069 /** 070 * 获取当前词库是否需要分词,默认不需要 071 * 072 * @return 是否需要分词 073 */ 074 public boolean isNeedSegment() { 075 return needSegment; 076 } 077 078 /** 079 * 设置当前词库是否需要分词 080 * 081 * @param needSegment true or false 082 */ 083 public VocabIntent setNeedSegment(boolean needSegment) { 084 this.needSegment = needSegment; 085 return this; 086 } 087 088 /** 089 * 获取词库操作 090 * 091 * @return string 需要执行的词库操作 092 * @see #setAction(String) 093 */ 094 public String getAction() { 095 return action; 096 } 097 098 /** 099 * 设置需要执行的词库操作 100 * 101 * @param action 需要执行的词库操作 102 * @return 返回本VocabIntent对象,便于连续调用 103 * @see #getAction() 104 * @see #ACTION_INSERT 105 * @see #ACTION_REMOVE 106 * @see #ACTION_CLEAR_AND_INSERT 107 * @see #ACTION_CLEAR_ALL 108 */ 109 public VocabIntent setAction(String action) { 110 this.action = action; 111 return this; 112 } 113 114 /** 115 * 获取词库名称 116 * 117 * @return 词库名称 118 * @see #setName(String) 119 */ 120 public String getName() { 121 return name; 122 } 123 124 /** 125 * 设置词库名称 126 * <p> 127 * 可以是自定义词库,比如“我的应用”。<br> 128 * 可以是系统词库,比如“sys.联系人”。 129 * 130 * @param name 词库名称 131 * @return 返回本VocabIntent对象,便于连续调用 132 * @see #getName() 133 */ 134 public VocabIntent setName(String name) { 135 this.name = name; 136 return this; 137 } 138 139 /** 140 * 获取词条列表 141 * 142 * @return 词条列表 143 * @see #setContents(List) 144 * @see #addContent(String) 145 */ 146 public List<String> getContents() { 147 return contents; 148 } 149 150 /** 151 * 设置需要操作的词条列表 152 * <p> 153 * 若需要上传带同义词的词条,格式如下:"${词条取值}:${同义词1}[,${同义词2}]"。<br> 154 * 比如:"电灯:电灯泡,灯泡","支付宝:支护宝"。 155 * 156 * @param contents 词条列表 157 * @return 返回本VocabIntent对象,便于连续调用 158 * @see #addContent(String) 159 * @see #getContents() 160 */ 161 public VocabIntent setContents(List<String> contents) { 162 this.contents = contents; 163 return this; 164 } 165 166 /** 167 * 向词条列表中添加词条 168 * 169 * @param content 词条 170 * @return 返回本VocabIntent对象,便于连续调用 171 * @see #setContents(List) 172 * @see #getContents() 173 */ 174 public VocabIntent addContent(String content) { 175 if (null == this.contents) { 176 this.contents = new ArrayList<>(); 177 } 178 179 this.contents.add(content); 180 return this; 181 } 182 183 private void checkParams() { 184 if (null == this.name) { 185 throw new IllegalArgumentException("Illegal Argument: null name"); 186 } 187 188 if (null == this.action) { 189 throw new IllegalArgumentException("Illegal Argument: null action"); 190 } 191 192 if ((null == this.contents || 0 == this.contents.size()) && ACTION_INSERT.equals(this.getAction())) { 193 throw new IllegalArgumentException("Illegal Argument: ACTION_INSERT without contents"); 194 } 195 196 if ((null == this.contents || 0 == this.contents.size()) && ACTION_REMOVE.equals(this.getAction())) { 197 throw new IllegalArgumentException("Illegal Argument: ACTION_REMOVE without contents"); 198 } 199 200 if ((null == this.contents || 0 == this.contents.size()) && ACTION_CLEAR_AND_INSERT.equals(this.getAction())) { 201 throw new IllegalArgumentException("Illegal Argument: ACTION_CLEAR_AND_INSERT without contents"); 202 } 203 } 204 205 public String toJson() { 206 checkParams(); 207 208 JSONObject obj = new JSONObject(); 209 try { 210 obj.put("name", this.name); 211 obj.put("action", this.action); 212 obj.put("needSegment", this.needSegment); 213 obj.put("numberExtension", this.numberExtension); 214 215 if (null != this.contents && 0 != this.contents.size()) { 216 JSONArray arr = new JSONArray(); 217 for (String s : this.contents) { 218 arr.put(s); 219 } 220 obj.put("contents", arr); 221 } 222 } catch (JSONException e) { 223 com.aispeech.dui.manager.AIJavaException.printException(e); 224 } 225 226 return obj.toString(); 227 } 228 229 @Override 230 public String toString() { 231 return "VocabIntent{" + 232 "action='" + action + '\'' + 233 ", name='" + name + '\'' + 234 ", contents=" + contents + 235 ", needSegment=" + needSegment + 236 ", numberExtension=" + numberExtension + 237 '}'; 238 } 239}