Commit f8cd65445956b97a327c8e063bc7e9e5e1b92237
1 parent
a2bb404f
1 拆分小家音箱拼音库
Showing
10 changed files
with
4 additions
and
544 deletions
build.gradle
config.gradle
module_xiaojiaSoundBox/build.gradle
module_xiaojiaSoundBox/libs/pinyin4j-2.5.0.jar deleted
100644 → 0
No preview for this file type
module_xiaojiaSoundBox/src/main/java/com/cnlive/strike/xiaojiaspeaker/model/IMFriendHasLetterInfo.java
| ... | ... | @@ -3,7 +3,7 @@ package com.cnlive.strike.xiaojiaspeaker.model; |
| 3 | 3 | import android.text.TextUtils; |
| 4 | 4 | |
| 5 | 5 | import com.cnlive.strike.xiaojiaspeaker.util.PinyinUtil; |
| 6 | -import com.cnlive.strike.xiaojiaspeaker.util.search.SearchUtil; | |
| 6 | +import com.cnlive.libs.base.util.search.SearchUtil; | |
| 7 | 7 | |
| 8 | 8 | import java.util.Locale; |
| 9 | 9 | ... | ... |
module_xiaojiaSoundBox/src/main/java/com/cnlive/strike/xiaojiaspeaker/model/IMFriendNoLetterInfo.java
| ... | ... | @@ -3,7 +3,7 @@ package com.cnlive.strike.xiaojiaspeaker.model; |
| 3 | 3 | import android.text.TextUtils; |
| 4 | 4 | |
| 5 | 5 | import com.cnlive.strike.xiaojiaspeaker.util.PinyinUtil; |
| 6 | -import com.cnlive.strike.xiaojiaspeaker.util.search.SearchUtil; | |
| 6 | +import com.cnlive.libs.base.util.search.SearchUtil; | |
| 7 | 7 | |
| 8 | 8 | import java.util.Locale; |
| 9 | 9 | ... | ... |
module_xiaojiaSoundBox/src/main/java/com/cnlive/strike/xiaojiaspeaker/util/search/CharacterParser.java deleted
100644 → 0
| 1 | -package com.cnlive.strike.xiaojiaspeaker.util.search; | |
| 2 | - | |
| 3 | -public class CharacterParser { | |
| 4 | - private static int[] pyvalue = new int[]{-20319, -20317, -20304, -20295, | |
| 5 | - -20292, -20283, -20265, -20257, -20242, -20230, -20051, -20036, | |
| 6 | - -20032, -20026, -20002, -19990, -19986, -19982, -19976, -19805, | |
| 7 | - -19784, -19775, -19774, -19763, -19756, -19751, -19746, -19741, | |
| 8 | - -19739, -19728, -19725, -19715, -19540, -19531, -19525, -19515, | |
| 9 | - -19500, -19484, -19479, -19467, -19289, -19288, -19281, -19275, | |
| 10 | - -19270, -19263, -19261, -19249, -19243, -19242, -19238, -19235, | |
| 11 | - -19227, -19224, -19218, -19212, -19038, -19023, -19018, -19006, | |
| 12 | - -19003, -18996, -18977, -18961, -18952, -18783, -18774, -18773, | |
| 13 | - -18763, -18756, -18741, -18735, -18731, -18722, -18710, -18697, | |
| 14 | - -18696, -18526, -18518, -18501, -18490, -18478, -18463, -18448, | |
| 15 | - -18447, -18446, -18239, -18237, -18231, -18220, -18211, -18201, | |
| 16 | - -18184, -18183, -18181, -18012, -17997, -17988, -17970, -17964, | |
| 17 | - -17961, -17950, -17947, -17931, -17928, -17922, -17759, -17752, | |
| 18 | - -17733, -17730, -17721, -17703, -17701, -17697, -17692, -17683, | |
| 19 | - -17676, -17496, -17487, -17482, -17468, -17454, -17433, -17427, | |
| 20 | - -17417, -17202, -17185, -16983, -16970, -16942, -16915, -16733, | |
| 21 | - -16708, -16706, -16689, -16664, -16657, -16647, -16474, -16470, | |
| 22 | - -16465, -16459, -16452, -16448, -16433, -16429, -16427, -16423, | |
| 23 | - -16419, -16412, -16407, -16403, -16401, -16393, -16220, -16216, | |
| 24 | - -16212, -16205, -16202, -16187, -16180, -16171, -16169, -16158, | |
| 25 | - -16155, -15959, -15958, -15944, -15933, -15920, -15915, -15903, | |
| 26 | - -15889, -15878, -15707, -15701, -15681, -15667, -15661, -15659, | |
| 27 | - -15652, -15640, -15631, -15625, -15454, -15448, -15436, -15435, | |
| 28 | - -15419, -15416, -15408, -15394, -15385, -15377, -15375, -15369, | |
| 29 | - -15363, -15362, -15183, -15180, -15165, -15158, -15153, -15150, | |
| 30 | - -15149, -15144, -15143, -15141, -15140, -15139, -15128, -15121, | |
| 31 | - -15119, -15117, -15110, -15109, -14941, -14937, -14933, -14930, | |
| 32 | - -14929, -14928, -14926, -14922, -14921, -14914, -14908, -14902, | |
| 33 | - -14894, -14889, -14882, -14873, -14871, -14857, -14678, -14674, | |
| 34 | - -14670, -14668, -14663, -14654, -14645, -14630, -14594, -14429, | |
| 35 | - -14407, -14399, -14384, -14379, -14368, -14355, -14353, -14345, | |
| 36 | - -14170, -14159, -14151, -14149, -14145, -14140, -14137, -14135, | |
| 37 | - -14125, -14123, -14122, -14112, -14109, -14099, -14097, -14094, | |
| 38 | - -14092, -14090, -14087, -14083, -13917, -13914, -13910, -13907, | |
| 39 | - -13906, -13905, -13896, -13894, -13878, -13870, -13859, -13847, | |
| 40 | - -13831, -13658, -13611, -13601, -13406, -13404, -13400, -13398, | |
| 41 | - -13395, -13391, -13387, -13383, -13367, -13359, -13356, -13343, | |
| 42 | - -13340, -13329, -13326, -13318, -13147, -13138, -13120, -13107, | |
| 43 | - -13096, -13095, -13091, -13076, -13068, -13063, -13060, -12888, | |
| 44 | - -12875, -12871, -12860, -12858, -12852, -12849, -12838, -12831, | |
| 45 | - -12829, -12812, -12802, -12607, -12597, -12594, -12585, -12556, | |
| 46 | - -12359, -12346, -12320, -12300, -12120, -12099, -12089, -12074, | |
| 47 | - -12067, -12058, -12039, -11867, -11861, -11847, -11831, -11798, | |
| 48 | - -11781, -11604, -11589, -11536, -11358, -11340, -11339, -11324, | |
| 49 | - -11303, -11097, -11077, -11067, -11055, -11052, -11045, -11041, | |
| 50 | - -11038, -11024, -11020, -11019, -11018, -11014, -10838, -10832, | |
| 51 | - -10815, -10800, -10790, -10780, -10764, -10587, -10544, -10533, | |
| 52 | - -10519, -10331, -10329, -10328, -10322, -10315, -10309, -10307, | |
| 53 | - -10296, -10281, -10274, -10270, -10262, -10260, -10256, -10254}; | |
| 54 | - public static String[] pystr = new String[]{"a", "ai", "an", "ang", "ao", | |
| 55 | - "ba", "bai", "ban", "bang", "bao", "bei", "ben", "beng", "bi", | |
| 56 | - "bian", "biao", "bie", "bin", "bing", "bo", "bu", "ca", "cai", | |
| 57 | - "can", "cang", "cao", "ce", "ceng", "cha", "chai", "chan", "chang", | |
| 58 | - "chao", "che", "chen", "cheng", "chi", "chong", "chou", "chu", | |
| 59 | - "chuai", "chuan", "chuang", "chui", "chun", "chuo", "ci", "cong", | |
| 60 | - "cou", "cu", "cuan", "cui", "cun", "cuo", "da", "dai", "dan", | |
| 61 | - "dang", "dao", "de", "deng", "di", "dian", "diao", "die", "ding", | |
| 62 | - "diu", "dong", "dou", "du", "duan", "dui", "dun", "duo", "e", "en", | |
| 63 | - "er", "fa", "fan", "fang", "fei", "fen", "feng", "fo", "fou", "fu", | |
| 64 | - "ga", "gai", "gan", "gang", "gao", "ge", "gei", "gen", "geng", | |
| 65 | - "gong", "gou", "gu", "gua", "guai", "guan", "guang", "gui", "gun", | |
| 66 | - "guo", "ha", "hai", "han", "hang", "hao", "he", "hei", "hen", | |
| 67 | - "heng", "hong", "hou", "hu", "hua", "huai", "huan", "huang", "hui", | |
| 68 | - "hun", "huo", "ji", "jia", "jian", "jiang", "jiao", "jie", "jin", | |
| 69 | - "jing", "jiong", "jiu", "ju", "juan", "jue", "jun", "ka", "kai", | |
| 70 | - "kan", "kang", "kao", "ke", "ken", "keng", "kong", "kou", "ku", | |
| 71 | - "kua", "kuai", "kuan", "kuang", "kui", "kun", "kuo", "la", "lai", | |
| 72 | - "lan", "lang", "lao", "le", "lei", "leng", "li", "lia", "lian", | |
| 73 | - "liang", "liao", "lie", "lin", "ling", "liu", "long", "lou", "lu", | |
| 74 | - "lv", "luan", "lue", "lun", "luo", "ma", "mai", "man", "mang", | |
| 75 | - "mao", "me", "mei", "men", "meng", "mi", "mian", "miao", "mie", | |
| 76 | - "min", "ming", "miu", "mo", "mou", "mu", "na", "nai", "nan", | |
| 77 | - "nang", "nao", "ne", "nei", "nen", "neng", "ni", "nian", "niang", | |
| 78 | - "niao", "nie", "nin", "ning", "niu", "nong", "nu", "nv", "nuan", | |
| 79 | - "nue", "nuo", "o", "ou", "pa", "pai", "pan", "pang", "pao", "pei", | |
| 80 | - "pen", "peng", "pi", "pian", "piao", "pie", "pin", "ping", "po", | |
| 81 | - "pu", "qi", "qia", "qian", "qiang", "qiao", "qie", "qin", "qing", | |
| 82 | - "qiong", "qiu", "qu", "quan", "que", "qun", "ran", "rang", "rao", | |
| 83 | - "re", "ren", "reng", "ri", "rong", "rou", "ru", "ruan", "rui", | |
| 84 | - "run", "ruo", "sa", "sai", "san", "sang", "sao", "se", "sen", | |
| 85 | - "seng", "sha", "shai", "shan", "shang", "shao", "she", "shen", | |
| 86 | - "sheng", "shi", "shou", "shu", "shua", "shuai", "shuan", "shuang", | |
| 87 | - "shui", "shun", "shuo", "si", "song", "sou", "su", "suan", "sui", | |
| 88 | - "sun", "suo", "ta", "tai", "tan", "tang", "tao", "te", "teng", | |
| 89 | - "ti", "tian", "tiao", "tie", "ting", "tong", "tou", "tu", "tuan", | |
| 90 | - "tui", "tun", "tuo", "wa", "wai", "wan", "wang", "wei", "wen", | |
| 91 | - "weng", "wo", "wu", "xi", "xia", "xian", "xiang", "xiao", "xie", | |
| 92 | - "xin", "xing", "xiong", "xiu", "xu", "xuan", "xue", "xun", "ya", | |
| 93 | - "yan", "yang", "yao", "ye", "yi", "yin", "ying", "yo", "yong", | |
| 94 | - "you", "yu", "yuan", "yue", "yun", "za", "zai", "zan", "zang", | |
| 95 | - "zao", "ze", "zei", "zen", "zeng", "zha", "zhai", "zhan", "zhang", | |
| 96 | - "zhao", "zhe", "zhen", "zheng", "zhi", "zhong", "zhou", "zhu", | |
| 97 | - "zhua", "zhuai", "zhuan", "zhuang", "zhui", "zhun", "zhuo", "zi", | |
| 98 | - "zong", "zou", "zu", "zuan", "zui", "zun", "zuo"}; | |
| 99 | - private StringBuilder buffer; | |
| 100 | - private String resource; | |
| 101 | - private static CharacterParser characterParser = new CharacterParser(); | |
| 102 | - | |
| 103 | - public static CharacterParser getInstance() { | |
| 104 | - return characterParser; | |
| 105 | - } | |
| 106 | - | |
| 107 | - public String getResource() { | |
| 108 | - return resource; | |
| 109 | - } | |
| 110 | - | |
| 111 | - public void setResource(String resource) { | |
| 112 | - this.resource = resource; | |
| 113 | - } | |
| 114 | - | |
| 115 | - /** | |
| 116 | - * 汉字转成ASCII码 | |
| 117 | - * | |
| 118 | - * @param chs | |
| 119 | - * @return | |
| 120 | - */ | |
| 121 | - private int getChsAscii(String chs) { | |
| 122 | - int asc = 0; | |
| 123 | - try { | |
| 124 | - byte[] bytes = chs.getBytes("gb2312"); | |
| 125 | - if (bytes == null || bytes.length > 2 || bytes.length <= 0) { | |
| 126 | - throw new RuntimeException("illegal resource string"); | |
| 127 | - } | |
| 128 | - if (bytes.length == 1) { | |
| 129 | - asc = bytes[0]; | |
| 130 | - } | |
| 131 | - if (bytes.length == 2) { | |
| 132 | - int hightByte = 256 + bytes[0]; | |
| 133 | - int lowByte = 256 + bytes[1]; | |
| 134 | - asc = (256 * hightByte + lowByte) - 256 * 256; | |
| 135 | - } | |
| 136 | - } catch (Exception e) { | |
| 137 | - System.out | |
| 138 | - .println("ERROR:ChineseSpelling.class-getChsAscii(String chs)" | |
| 139 | - + e); | |
| 140 | - } | |
| 141 | - return asc; | |
| 142 | - } | |
| 143 | - | |
| 144 | - /** | |
| 145 | - * 单字解析 | |
| 146 | - **/ | |
| 147 | - public String convert(String str) { | |
| 148 | - String result = null; | |
| 149 | - int ascii = getChsAscii(str); | |
| 150 | - if (ascii > 0 && ascii < 160) { | |
| 151 | - result = String.valueOf((char) ascii); | |
| 152 | - } else { | |
| 153 | - for (int i = (pyvalue.length - 1); i >= 0; i--) { | |
| 154 | - if (pyvalue[i] <= ascii) { | |
| 155 | - result = pystr[i]; | |
| 156 | - break; | |
| 157 | - } | |
| 158 | - } | |
| 159 | - } | |
| 160 | - return result; | |
| 161 | - } | |
| 162 | - | |
| 163 | - /** | |
| 164 | - * 词组解析 | |
| 165 | - * | |
| 166 | - * @param chs | |
| 167 | - * @return | |
| 168 | - */ | |
| 169 | - public String getSelling(String chs) { | |
| 170 | - String key, value; | |
| 171 | - buffer = new StringBuilder(); | |
| 172 | - for (int i = 0; i < chs.length(); i++) { | |
| 173 | - key = chs.substring(i, i + 1); | |
| 174 | - if (key.getBytes().length >= 2) { | |
| 175 | - value = (String) convert(key); | |
| 176 | - if (value == null) { | |
| 177 | - value = "unknown"; | |
| 178 | - } | |
| 179 | - } else { | |
| 180 | - value = key; | |
| 181 | - } | |
| 182 | - buffer.append(value); | |
| 183 | - } | |
| 184 | - return buffer.toString(); | |
| 185 | - } | |
| 186 | - | |
| 187 | - public String getSpelling() { | |
| 188 | - return this.getSelling(this.getResource()); | |
| 189 | - } | |
| 190 | - | |
| 191 | -} | |
| 192 | 0 | \ No newline at end of file |
module_xiaojiaSoundBox/src/main/java/com/cnlive/strike/xiaojiaspeaker/util/search/FirstLetterUtil.java deleted
100644 → 0
| 1 | -package com.cnlive.strike.xiaojiaspeaker.util.search; | |
| 2 | - | |
| 3 | -public class FirstLetterUtil { | |
| 4 | - private static int BEGIN = 45217; | |
| 5 | - private static int END = 63486; | |
| 6 | - // 按照声母表示,这个表是在GB2312中的出现的第一个汉字,也就是说“啊”是代表首字母a的第一个汉字。 | |
| 7 | - // i, u, v都不做声母, 自定规则跟随前面的字母 | |
| 8 | - private static char[] chartable = {'啊', '芭', '擦', '搭', '蛾', '发', '噶', '哈', | |
| 9 | - '哈', '击', '喀', '垃', '妈', '拿', '哦', '啪', '期', '然', '撒', '塌', '塌', | |
| 10 | - '塌', '挖', '昔', '压', '匝',}; | |
| 11 | - // 二十六个字母区间对应二十七个端点 | |
| 12 | - // GB2312码汉字区间十进制表示 | |
| 13 | - private static int[] table = new int[27]; | |
| 14 | - // 对应首字母区间表 | |
| 15 | - private static char[] initialtable = {'a', 'b', 'c', 'd', 'e', 'f', 'g', | |
| 16 | - 'h', 'h', 'j', 'k', 'l', 'm', 'n', 'o', 'p', 'q', 'r', 's', 't', | |
| 17 | - 't', 't', 'w', 'x', 'y', 'z',}; | |
| 18 | - | |
| 19 | - // 初始化 | |
| 20 | - static { | |
| 21 | - for (int i = 0; i < 26; i++) { | |
| 22 | - table[i] = gbValue(chartable[i]);// 得到GB2312码的首字母区间端点表,十进制。 | |
| 23 | - } | |
| 24 | - table[26] = END;// 区间表结尾 | |
| 25 | - } | |
| 26 | - | |
| 27 | - /** | |
| 28 | - * 根据一个包含汉字的字符串返回一个汉字拼音首字母的字符串 最重要的一个方法,思路如下:一个个字符读入、判断、输出 | |
| 29 | - */ | |
| 30 | - public static String getFirstLetter(String sourceStr) { | |
| 31 | - String result = ""; | |
| 32 | - String str = sourceStr.toLowerCase(); | |
| 33 | - int StrLength = str.length(); | |
| 34 | - int i; | |
| 35 | - try { | |
| 36 | - for (i = 0; i < StrLength; i++) { | |
| 37 | - result += Char2Initial(str.charAt(i)); | |
| 38 | - } | |
| 39 | - } catch (Exception e) { | |
| 40 | - result = ""; | |
| 41 | - } | |
| 42 | - return result; | |
| 43 | - } | |
| 44 | - | |
| 45 | - /** | |
| 46 | - * 输入字符,得到他的声母,英文字母返回对应的大写字母,其他非简体汉字返回 '0' | |
| 47 | - */ | |
| 48 | - private static char Char2Initial(char ch) { | |
| 49 | - // 对英文字母的处理:小写字母转换为大写,大写的直接返回 | |
| 50 | - if (ch >= 'a' && ch <= 'z') { | |
| 51 | - return ch; | |
| 52 | - } | |
| 53 | - if (ch >= 'A' && ch <= 'Z') { | |
| 54 | - | |
| 55 | - return ch; | |
| 56 | - } | |
| 57 | - // 对非英文字母的处理:转化为首字母,然后判断是否在码表范围内, | |
| 58 | - // 若不是,则直接返回。 | |
| 59 | - // 若是,则在码表内的进行判断。 | |
| 60 | - int gb = gbValue(ch);// 汉字转换首字母 | |
| 61 | - | |
| 62 | - if ((gb < BEGIN) || (gb > END))// 在码表区间之前,直接返回 | |
| 63 | - { | |
| 64 | - return ch; | |
| 65 | - } | |
| 66 | - | |
| 67 | - int i; | |
| 68 | - for (i = 0; i < 26; i++) {// 判断匹配码表区间,匹配到就break,判断区间形如“[,)” | |
| 69 | - if ((gb >= table[i]) && (gb < table[i + 1])) { | |
| 70 | - break; | |
| 71 | - } | |
| 72 | - } | |
| 73 | - | |
| 74 | - if (gb == END) {// 补上GB2312区间最右端 | |
| 75 | - i = 25; | |
| 76 | - } | |
| 77 | - return initialtable[i]; // 在码表区间中,返回首字母 | |
| 78 | - } | |
| 79 | - | |
| 80 | - /** | |
| 81 | - * 取出汉字的编码 cn 汉字 | |
| 82 | - */ | |
| 83 | - private static int gbValue(char ch) {// 将一个汉字(GB2312)转换为十进制表示。 | |
| 84 | - String str = new String(); | |
| 85 | - str += ch; | |
| 86 | - try { | |
| 87 | - byte[] bytes = str.getBytes("GB2312"); | |
| 88 | - if (bytes.length < 2) { | |
| 89 | - return 0; | |
| 90 | - } | |
| 91 | - return (bytes[0] << 8 & 0xff00) + (bytes[1] & 0xff); | |
| 92 | - } catch (Exception e) { | |
| 93 | - return 0; | |
| 94 | - } | |
| 95 | - } | |
| 96 | - | |
| 97 | - | |
| 98 | -} | |
| 99 | 0 | \ No newline at end of file |
module_xiaojiaSoundBox/src/main/java/com/cnlive/strike/xiaojiaspeaker/util/search/SearchData.java deleted
100644 → 0
| 1 | -package com.cnlive.strike.xiaojiaspeaker.util.search; | |
| 2 | - | |
| 3 | -import java.io.Serializable; | |
| 4 | - | |
| 5 | -/** | |
| 6 | - * author gujun | |
| 7 | - * date: 2019/4/2 | |
| 8 | - * desc: | |
| 9 | - */ | |
| 10 | -public class SearchData implements Serializable { | |
| 11 | - | |
| 12 | - private String searchContent; | |
| 13 | - private int spannableNickStartIndex; | |
| 14 | - private int spannableNickEndIndex; | |
| 15 | - | |
| 16 | - public String getSearchContent() { | |
| 17 | - return searchContent; | |
| 18 | - } | |
| 19 | - | |
| 20 | - public void setSearchContent(String searchContent) { | |
| 21 | - this.searchContent = searchContent; | |
| 22 | - } | |
| 23 | - | |
| 24 | - public int getSpannableNickStartIndex() { | |
| 25 | - return spannableNickStartIndex; | |
| 26 | - } | |
| 27 | - | |
| 28 | - public void setSpannableNickStartIndex(int spannableNickStartIndex) { | |
| 29 | - this.spannableNickStartIndex = spannableNickStartIndex; | |
| 30 | - } | |
| 31 | - | |
| 32 | - public int getSpannableNickEndIndex() { | |
| 33 | - return spannableNickEndIndex; | |
| 34 | - } | |
| 35 | - | |
| 36 | - public void setSpannableNickEndIndex(int spannableNickEndIndex) { | |
| 37 | - this.spannableNickEndIndex = spannableNickEndIndex; | |
| 38 | - } | |
| 39 | -} |
module_xiaojiaSoundBox/src/main/java/com/cnlive/strike/xiaojiaspeaker/util/search/SearchUtil.java deleted
100644 → 0
| 1 | -package com.cnlive.strike.xiaojiaspeaker.util.search; | |
| 2 | - | |
| 3 | -import android.text.TextUtils; | |
| 4 | - | |
| 5 | -import java.util.ArrayList; | |
| 6 | -import java.util.List; | |
| 7 | -import java.util.regex.Matcher; | |
| 8 | -import java.util.regex.Pattern; | |
| 9 | - | |
| 10 | -/** | |
| 11 | - * Created by gujun on 2018/1/9. | |
| 12 | - */ | |
| 13 | - | |
| 14 | -public class SearchUtil { | |
| 15 | - | |
| 16 | - /** | |
| 17 | - * 获取联系人模糊查询列表 | |
| 18 | - * | |
| 19 | - * @param filter 过滤条件 | |
| 20 | - * @param list 原始列表 | |
| 21 | - * @return | |
| 22 | - */ | |
| 23 | - public static <T extends SearchData> List<T> getFilterContactList(CharSequence filter, List<T> list) { | |
| 24 | - if (list == null || list.size() <= 0) return null; | |
| 25 | - List<T> filterList = new ArrayList<>(); | |
| 26 | - | |
| 27 | - for (T contactInfo : list) { | |
| 28 | - if (!TextUtils.isEmpty(filter)) { | |
| 29 | - if (contains(contactInfo, filter.toString())) { | |
| 30 | - filterList.add(contactInfo); | |
| 31 | - } | |
| 32 | - } else { | |
| 33 | - contactInfo.setSpannableNickEndIndex(0); | |
| 34 | - contactInfo.setSpannableNickStartIndex(0); | |
| 35 | - filterList.add(contactInfo); | |
| 36 | - } | |
| 37 | - } | |
| 38 | - return filterList; | |
| 39 | - } | |
| 40 | - | |
| 41 | - /** | |
| 42 | - * 判断某个联系人昵称是否包含查询字段 | |
| 43 | - * | |
| 44 | - * @param searchData | |
| 45 | - * @param filter | |
| 46 | - * @return | |
| 47 | - */ | |
| 48 | - private static boolean contains(SearchData searchData, String filter) { | |
| 49 | - String name = searchData.getSearchContent(); | |
| 50 | - if (TextUtils.isEmpty(name) | |
| 51 | - && TextUtils.isEmpty(name)) { | |
| 52 | - return false; | |
| 53 | - } | |
| 54 | - | |
| 55 | - int startIndex = 0; | |
| 56 | - int endIndex = 0; | |
| 57 | - boolean flag = false; | |
| 58 | - | |
| 59 | - if (!isAllEnglish(filter)) { | |
| 60 | - // 只要不全是英文就字符串对比 | |
| 61 | - if (name.contains(filter)) { | |
| 62 | - flag = true; | |
| 63 | - startIndex = name.indexOf(filter); | |
| 64 | - endIndex = startIndex + filter.length(); | |
| 65 | - } | |
| 66 | - } else { | |
| 67 | - String key = ""; | |
| 68 | - String searchPinYin = ""; | |
| 69 | - if (!flag) { | |
| 70 | - CharacterParser finder = CharacterParser.getInstance(); | |
| 71 | - // 先将输入的字符串转换为拼音 | |
| 72 | - finder.setResource(filter); | |
| 73 | - searchPinYin = finder.getSpelling(); | |
| 74 | - //处理通配符 | |
| 75 | - key = processingWildcards(searchPinYin); | |
| 76 | - | |
| 77 | - // 简拼匹配,如果输入在字符串长度大于6就不按首字母匹配了 | |
| 78 | - if (key.length() < 6) { | |
| 79 | - String firstLetters = FirstLetterUtil | |
| 80 | - .getFirstLetter(name); | |
| 81 | - // 不区分大小写 | |
| 82 | - Pattern firstLetterPattern = Pattern.compile(key, | |
| 83 | - Pattern.CASE_INSENSITIVE); | |
| 84 | - Matcher firstLetterMatcher = firstLetterPattern.matcher(firstLetters); | |
| 85 | - flag = firstLetterMatcher.find(); | |
| 86 | - if (flag) { | |
| 87 | - startIndex = firstLetterMatcher.start(); | |
| 88 | - endIndex = firstLetterMatcher.end(); | |
| 89 | - } | |
| 90 | - } | |
| 91 | - } | |
| 92 | - | |
| 93 | - if (!flag && key.length() > 1) { | |
| 94 | - //key.length() > 1代表:仅有一个字母时必须匹配首字母,不能使用全拼 | |
| 95 | - // 全拼匹配 | |
| 96 | - CharacterParser finder = CharacterParser.getInstance(); | |
| 97 | - finder.setResource(name); | |
| 98 | - // 不区分大小写 | |
| 99 | - Pattern pattern = Pattern | |
| 100 | - .compile(key, Pattern.CASE_INSENSITIVE); | |
| 101 | - Matcher matcher = pattern.matcher(finder.getSpelling()); | |
| 102 | - flag = matcher.find(); | |
| 103 | - if (flag) { | |
| 104 | - int matcherStart = matcher.start(); | |
| 105 | - int matcherEnd = matcher.end(); | |
| 106 | - if (matcherStart < matcherEnd) { | |
| 107 | - int scanPinYinCount = 0; | |
| 108 | - char nickNameChar[] = name.toCharArray(); | |
| 109 | - for (int i = 0; i < nickNameChar.length; i++) { | |
| 110 | - CharacterParser finderNick = CharacterParser.getInstance(); | |
| 111 | - finderNick.setResource(String.valueOf(nickNameChar[i])); | |
| 112 | - String nickNamePinYin = finderNick.getSpelling(); | |
| 113 | - if (matcherStart >= scanPinYinCount && matcherStart < scanPinYinCount + nickNamePinYin.length()) { | |
| 114 | - startIndex = i; | |
| 115 | - } | |
| 116 | - if (matcherEnd > scanPinYinCount && matcherEnd <= scanPinYinCount + nickNamePinYin.length()) { | |
| 117 | - endIndex = i + 1; | |
| 118 | - } | |
| 119 | - scanPinYinCount += nickNamePinYin.length(); | |
| 120 | - } | |
| 121 | - } | |
| 122 | - } | |
| 123 | - } | |
| 124 | - } | |
| 125 | - | |
| 126 | - if (flag) { | |
| 127 | - searchData.setSpannableNickStartIndex(startIndex); | |
| 128 | - searchData.setSpannableNickEndIndex(endIndex); | |
| 129 | - } | |
| 130 | - | |
| 131 | - return flag; | |
| 132 | - } | |
| 133 | - | |
| 134 | - /** | |
| 135 | - * 处理转义字符包括 * . ? + $ ^ [ ] ( ) { } | \ | |
| 136 | - * | |
| 137 | - * @param searchPinYin | |
| 138 | - * @return | |
| 139 | - */ | |
| 140 | - private static String processingWildcards(String searchPinYin) { | |
| 141 | - String key = ""; | |
| 142 | - // 处理通配符问题 | |
| 143 | - if (searchPinYin.contains("*") || | |
| 144 | - searchPinYin.contains(".") || | |
| 145 | - searchPinYin.contains("?") || | |
| 146 | - searchPinYin.contains("+") || | |
| 147 | - searchPinYin.contains("$") || | |
| 148 | - searchPinYin.contains("^") || | |
| 149 | - searchPinYin.contains("[") || | |
| 150 | - searchPinYin.contains("]") || | |
| 151 | - searchPinYin.contains("(") || | |
| 152 | - searchPinYin.contains(")") || | |
| 153 | - searchPinYin.contains("{") || | |
| 154 | - searchPinYin.contains("}") || | |
| 155 | - searchPinYin.contains("|") || | |
| 156 | - searchPinYin.contains("\\")) { | |
| 157 | - char[] chars = searchPinYin.toCharArray(); | |
| 158 | - for (int k = 0; k < chars.length; k++) { | |
| 159 | - if (chars[k] == '*') { | |
| 160 | - key = key + "\\*"; | |
| 161 | - } else if (chars[k] == '(') { | |
| 162 | - key = key + "\\("; | |
| 163 | - } else if (chars[k] == ')') { | |
| 164 | - key = key + "\\)"; | |
| 165 | - } else if (chars[k] == '?') { | |
| 166 | - key = key + "\\?"; | |
| 167 | - } else if (chars[k] == '.') { | |
| 168 | - key = key + "\\."; | |
| 169 | - } else if (chars[k] == '+') { | |
| 170 | - key = key + "\\+"; | |
| 171 | - } else if (chars[k] == '$') { | |
| 172 | - key = key + "\\$"; | |
| 173 | - } else if (chars[k] == '^') { | |
| 174 | - key = key + "\\^"; | |
| 175 | - } else if (chars[k] == '[') { | |
| 176 | - key = key + "\\["; | |
| 177 | - } else if (chars[k] == ']') { | |
| 178 | - key = key + "\\]"; | |
| 179 | - } else if (chars[k] == '{') { | |
| 180 | - key = key + "\\{"; | |
| 181 | - } else if (chars[k] == '}') { | |
| 182 | - key = key + "\\}"; | |
| 183 | - } else if (chars[k] == '|') { | |
| 184 | - key = key + "\\|"; | |
| 185 | - } else if (chars[k] == '\\') { | |
| 186 | - key = key + "\\\\"; | |
| 187 | - } else { | |
| 188 | - key = key + String.valueOf(chars[k]); | |
| 189 | - } | |
| 190 | - } | |
| 191 | - } else { | |
| 192 | - key = searchPinYin; | |
| 193 | - } | |
| 194 | - return key; | |
| 195 | - } | |
| 196 | - | |
| 197 | - /** | |
| 198 | - * 是否全是英文 | |
| 199 | - * | |
| 200 | - * @param str | |
| 201 | - * @return | |
| 202 | - */ | |
| 203 | - public static boolean isAllEnglish(String str) { | |
| 204 | - Pattern p = Pattern.compile("[a-zA-Z]"); | |
| 205 | - Matcher m = p.matcher(str); | |
| 206 | - if (m.find()) { | |
| 207 | - return true; | |
| 208 | - } | |
| 209 | - return false; | |
| 210 | - } | |
| 211 | -} |