001package com.aispeech.dui.dds.agent.wakeup.word; 002 003import android.text.TextUtils; 004 005import org.json.JSONArray; 006import org.json.JSONException; 007import org.json.JSONObject; 008 009public class HotWords { 010 private JSONObject mSlotsObj = new JSONObject(); 011 private JSONArray mSlotsArr = new JSONArray(); 012 013 // 设置场景 014 public HotWords setScene(String scene) { 015 try { 016 mSlotsObj.put("scene", scene); 017 } catch (JSONException e) { 018 e.printStackTrace(); 019 } 020 return this; 021 } 022 023 // 添加热词词库 024 public HotWords addSlots(Slots slots) { 025 JSONObject slotObj = slots.getSlotsObj(); 026 mSlotsArr.put(slotObj); 027 return this; 028 } 029 030 public JSONObject getHotWordsObj() { 031 try { 032 mSlotsObj.put("hotWords", mSlotsArr); 033 } catch (JSONException e) { 034 e.printStackTrace(); 035 } 036 return mSlotsObj; 037 } 038 039 040 public static class Slots{ 041 042 private JSONObject mSlotsObj = new JSONObject(); 043 private JSONArray mSlotsArry = new JSONArray(); 044 private String slot; 045 046 // 添加热词词库 047 public Slots addHotWord(HotWord hotWord) { 048 JSONObject hotWordObj = hotWord.getHotWordObj(); 049 mSlotsArry.put(hotWordObj); 050 return this; 051 } 052 053 public JSONObject getSlotsObj() { 054 try { 055 if(!TextUtils.isEmpty(slot)){ 056 mSlotsObj.put(slot, mSlotsArry); 057 } 058 } catch (Exception e) { 059 e.printStackTrace(); 060 } 061 return mSlotsObj; 062 } 063 064 public Slots setSlot(String slot) { 065 this.slot = slot; 066 return this; 067 } 068 } 069 070 public static class HotWord { 071 private JSONObject mHotWordObj = new JSONObject(); 072 private JSONArray mSegmentArr; 073 074 public HotWord setOrigin(String origin) { 075 try { 076 mHotWordObj.put("origin", origin); 077 } catch (JSONException e) { 078 e.printStackTrace(); 079 } 080 return this; 081 } 082 083 public HotWord setThreshold(float threshold) { 084 try { 085 mHotWordObj.put("threshold", threshold); 086 } catch (JSONException e) { 087 e.printStackTrace(); 088 } 089 return this; 090 } 091 092 public HotWord setAction(String action) { 093 try { 094 mHotWordObj.put("action", action); 095 } catch (JSONException e) { 096 e.printStackTrace(); 097 } 098 return this; 099 } 100 101 public HotWord addSegment(String segment) { 102 if (mSegmentArr == null) { 103 mSegmentArr = new JSONArray(); 104 } 105 mSegmentArr.put(segment); 106 return this; 107 } 108 109 public HotWord addCustom(String key, String value) { 110 try { 111 mHotWordObj.put(key, value); 112 } catch (JSONException e) { 113 e.printStackTrace(); 114 } 115 return this; 116 } 117 118 private JSONObject getHotWordObj() { 119 if (mSegmentArr != null) { 120 try { 121 mHotWordObj.put("segment", mSegmentArr); 122 } catch (Exception e) { 123 e.printStackTrace(); 124 } 125 } 126 return mHotWordObj; 127 } 128 } 129 130}