├── uploadMaster ├── screenshot └── 2018-08-17_13_13_09.gif ├── lib ├── src │ ├── pinyin_format.dart │ ├── pinyin_exception.dart │ ├── pinyin_resource.dart │ ├── chinese_helper.dart │ └── pinyin_helper.dart └── lpinyin.dart ├── pubspec.yaml ├── CHANGELOG.md ├── .gitignore ├── analysis_options.yaml ├── example └── lib │ └── main.dart ├── LICENSE └── README.md /uploadMaster: -------------------------------------------------------------------------------- 1 | git push origin master 2 | -------------------------------------------------------------------------------- /screenshot/2018-08-17_13_13_09.gif: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/Sky24n/lpinyin/HEAD/screenshot/2018-08-17_13_13_09.gif -------------------------------------------------------------------------------- /lib/src/pinyin_format.dart: -------------------------------------------------------------------------------- 1 | /** 2 | * 拼音格式: 3 | * WITHOUT_TONE--不带声调 4 | * WITH_TONE_MARK--带声调 5 | * WITH_TONE_NUMBER--数字代表声调 6 | */ 7 | 8 | /// 9 | enum PinyinFormat { WITHOUT_TONE, WITH_TONE_MARK, WITH_TONE_NUMBER } 10 | -------------------------------------------------------------------------------- /lib/lpinyin.dart: -------------------------------------------------------------------------------- 1 | library lpinyin; 2 | 3 | export 'src/dict_data.dart'; 4 | export 'src/pinyin_format.dart'; 5 | export 'src/pinyin_helper.dart'; 6 | export 'src/pinyin_exception.dart'; 7 | export 'src/pinyin_resource.dart'; 8 | export 'src/chinese_helper.dart'; 9 | -------------------------------------------------------------------------------- /lib/src/pinyin_exception.dart: -------------------------------------------------------------------------------- 1 | class PinyinException implements Exception { 2 | String message; 3 | 4 | PinyinException([this.message]); 5 | 6 | String toString() { 7 | if (message == null) return "Exception"; 8 | return "Exception: $message"; 9 | } 10 | } 11 | -------------------------------------------------------------------------------- /pubspec.yaml: -------------------------------------------------------------------------------- 1 | name: lpinyin 2 | description: Chinese character conversion pinyin library.Accurate and complete Chinese character dictionary.Fast conversion. 3 | version: 1.0.6 4 | author: thl <863764940@qq.com> 5 | homepage: https://github.com/flutterchina/lpinyin 6 | 7 | environment: 8 | sdk: ">=1.19.0 <3.0.0" 9 | 10 | -------------------------------------------------------------------------------- /CHANGELOG.md: -------------------------------------------------------------------------------- 1 | ## 1.0.6 2 | 3 | * TODO: Packages arrangement. 4 | 5 | ## 1.0.2 6 | 7 | * TODO: Delete useless files. 8 | 9 | ## 1.0.1 10 | 11 | * TODO: Optimize Pinyin conversion speed. 12 | 13 | ## 1.0.0 14 | 15 | * TODO: fix "" separator bug. 16 | 17 | ## 0.0.2 18 | 19 | * TODO: fix sdk environment. 20 | 21 | ## 0.0.1 22 | 23 | * TODO: lPinyin initial release. 24 | -------------------------------------------------------------------------------- /.gitignore: -------------------------------------------------------------------------------- 1 | # See https://www.dartlang.org/guides/libraries/private-files 2 | 3 | # Files and directories created by pub 4 | .dart_tool/ 5 | .packages 6 | .pub/ 7 | build/ 8 | # If you're building an application, you may want to check-in your pubspec.lock 9 | pubspec.lock 10 | 11 | # Directory created by dartdoc 12 | # If you don't generate documentation locally you can remove this line. 13 | doc/api/ 14 | 15 | .gradle 16 | .idea 17 | *.iml 18 | build 19 | local.properties 20 | */.idea/ 21 | .idea/ 22 | /.idea/workspace.xml 23 | /.idea/libraries 24 | -------------------------------------------------------------------------------- /analysis_options.yaml: -------------------------------------------------------------------------------- 1 | # https://www.dartlang.org/guides/language/analysis-options 2 | # Source of linter options: 3 | # http://dart-lang.github.io/linter/lints/options/options.html 4 | analyzer: 5 | strong-mode: 6 | implicit-casts: false 7 | implicit-dynamic: false 8 | errors: 9 | todo: ignore 10 | exclude: 11 | - flutter/** 12 | - lib/*.dart 13 | 14 | linter: 15 | rules: 16 | - camel_case_types 17 | - hash_and_equals 18 | - iterable_contains_unrelated_type 19 | - list_remove_unrelated_type 20 | - unrelated_type_equality_checks 21 | - valid_regexps 22 | -------------------------------------------------------------------------------- /example/lib/main.dart: -------------------------------------------------------------------------------- 1 | import 'package:lpinyin/lpinyin.dart'; 2 | 3 | void main() { 4 | String str = "天府广场"; 5 | 6 | //字符串拼音首字符 7 | PinyinHelper.getShortPinyin(str); // tfgc 8 | 9 | //字符串首字拼音 10 | PinyinHelper.getFirstWordPinyin(str); // tian 11 | 12 | PinyinHelper.convertToPinyinString(str); //tian fu guang chang 13 | PinyinHelper.convertToPinyinString(str, separator: " ", format: PinyinFormat.WITHOUT_TONE); 14 | 15 | PinyinHelper.convertToPinyinStringWithoutException(str); //tian fu guang chang 16 | PinyinHelper.convertToPinyinStringWithoutException(str, separator: " ", format: PinyinFormat.WITHOUT_TONE); 17 | 18 | //添加用户自定义字典 19 | List dict1 = ['耀=yào', '老=lǎo']; 20 | PinyinHelper.addPinyinDict(dict1); //拼音字典 21 | List dict2 = ['奇偶=jī,ǒu', '成都=chéng,dū']; 22 | PinyinHelper.addMultiPinyinDict(dict2); //多音字词组字典 23 | List dict3 = ['倆=俩', '們=们']; 24 | ChineseHelper.addChineseDict(dict3); //繁体字字典 25 | } 26 | 27 | 28 | 29 | -------------------------------------------------------------------------------- /lib/src/pinyin_resource.dart: -------------------------------------------------------------------------------- 1 | import 'dart:collection'; 2 | 3 | import 'package:lpinyin/src/dict_data.dart'; 4 | 5 | class PinyinResource { 6 | static Map getPinyinResource() { 7 | return getResource(PINYIN_DICT); 8 | } 9 | 10 | static Map getChineseResource() { 11 | return getResource(CHINESE_DICT); 12 | } 13 | 14 | static Map getMultiPinyinResource() { 15 | return getResource(MULTI_PINYIN_DICT); 16 | } 17 | 18 | static Map getResource(List list) { 19 | Map map = new HashMap(); 20 | List> mapEntryList = new List(); 21 | for (int i = 0, length = list.length; i < length; i++) { 22 | List tokens = list[i].trim().split("="); 23 | MapEntry mapEntry = new MapEntry(tokens[0], tokens[1]); 24 | mapEntryList.add(mapEntry); 25 | } 26 | map.addEntries(mapEntryList); 27 | return map; 28 | } 29 | } 30 | -------------------------------------------------------------------------------- /LICENSE: -------------------------------------------------------------------------------- 1 | BSD 2-Clause License 2 | 3 | Copyright (c) 2018, Sky24n 4 | All rights reserved. 5 | 6 | Redistribution and use in source and binary forms, with or without 7 | modification, are permitted provided that the following conditions are met: 8 | 9 | * Redistributions of source code must retain the above copyright notice, this 10 | list of conditions and the following disclaimer. 11 | 12 | * Redistributions in binary form must reproduce the above copyright notice, 13 | this list of conditions and the following disclaimer in the documentation 14 | and/or other materials provided with the distribution. 15 | 16 | THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" 17 | AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 18 | IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE 19 | DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE 20 | FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 21 | DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR 22 | SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER 23 | CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, 24 | OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE 25 | OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 26 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | # lpinyin (汉字转拼音Flutter版) 2 | 3 | [![Pub](https://img.shields.io/pub/v/lpinyin.svg?style=flat-square)](https://pub.dartlang.org/packages/lpinyin) 4 | 5 | lpinyin是一个汉字转拼音的Dart package. 主要参考Java开源类库[jpinyin](https://github.com/SilenceDut/jpinyin). 6 | ①准确、完善的字库 7 | ②拼音转换速度快 8 | ③支持多种拼音输出格式:带音标、不带音标、数字表示音标以及拼音首字母输出格式 9 | ④支持常见多音字的识别,其中包括词组、成语、地名等 10 | ⑤简繁体中文转换 11 | ⑥支持添加用户自定义字典 12 | 13 | ## Demo: [flutter_demos](https://github.com/Sky24n/flutter_demos). 14 | 15 | ## Android扫码下载APK 16 | ![](https://github.com/Sky24n/LDocuments/blob/master/AppImgs/flutter_demos/qrcode.png) 17 | 18 | ## Demo截图 19 | ![image](https://github.com/Sky24n/lpinyin/blob/master/screenshot/2018-08-17_13_13_09.gif) 20 | 21 | ### Add dependency 22 | 23 | ```yaml 24 | dependencies: 25 | lpinyin: x.x.x #latest version 26 | ``` 27 | 28 | ### Example 29 | 30 | ``` dart 31 | 32 | // Import package 33 | import 'package:lpinyin/lpinyin.dart'; 34 | 35 | String text = "天府广场"; 36 | 37 | //字符串拼音首字符 38 | PinyinHelper.getShortPinyin(str); // tfgc 39 | 40 | //字符串首字拼音 41 | PinyinHelper.getFirstWordPinyin(str); // tian 42 | 43 | //无法转换拼音会 throw PinyinException 44 | PinyinHelper.convertToPinyinString(text); 45 | PinyinHelper.convertToPinyinString(text, separator: " ", format: PinyinFormat.WITHOUT_TONE);//tian fu guang chang 46 | 47 | //无法转换拼音 默认用'#'替代 48 | PinyinHelper.convertToPinyinStringWithoutException(text); 49 | PinyinHelper.convertToPinyinStringWithoutException(text, separator: " ", defPinyin: '#', format: PinyinFormat.WITHOUT_TONE);//tian fu guang chang 50 | 51 | //添加用户自定义字典 52 | List dict1 = ['耀=yào','老=lǎo']; 53 | PinyinHelper.addPinyinDict(dict1);//拼音字典 54 | List dict2 = ['奇偶=jī,ǒu','成都=chéng,dū']; 55 | PinyinHelper.addMultiPinyinDict(dict2);//多音字词组字典 56 | List dict3 = ['倆=俩','們=们']; 57 | ChineseHelper.addChineseDict(dict3);//繁体字字典 58 | 59 | ``` 60 | -------------------------------------------------------------------------------- /lib/src/chinese_helper.dart: -------------------------------------------------------------------------------- 1 | import 'package:lpinyin/src/pinyin_resource.dart'; 2 | 3 | class ChineseHelper { 4 | static final String chineseRegex = "[\\u4e00-\\u9fa5]"; 5 | static final RegExp chineseRegexp = new RegExp(chineseRegex); 6 | static final Map chineseMap = 7 | PinyinResource.getChineseResource(); 8 | 9 | /** 10 | *判断某个字符是否为汉字 11 | * @return 是汉字返回true,否则返回false 12 | */ 13 | 14 | /// 15 | static bool isChinese(String c) { 16 | return '〇' == c || chineseRegexp.hasMatch(c); 17 | } 18 | 19 | /** 20 | * 判断某个字符是否为繁体字 21 | * 22 | * @param c 需要判断的字符 23 | * 24 | * @return 是繁体字返回true,否则返回false 25 | */ 26 | 27 | /// 28 | static bool isTraditionalChinese(String c) { 29 | return chineseMap.containsKey(c); 30 | } 31 | 32 | /** 33 | * 判断字符串中是否包含中文 34 | * 35 | * @param str 字符串 36 | * 37 | * @return 包含汉字返回true,否则返回false 38 | */ 39 | 40 | /// 41 | static bool containsChinese(String str) { 42 | for (int i = 0, len = str.length; i < len; i++) { 43 | if (isChinese(str[i])) { 44 | return true; 45 | } 46 | } 47 | return false; 48 | } 49 | 50 | /** 51 | * 将单个繁体字转换为简体字 52 | * 53 | * @param c 需要转换的繁体字 54 | * 55 | * @return 转换后的简体字 56 | */ 57 | 58 | /// 59 | static String convertCharToSimplifiedChinese(String c) { 60 | String simplifiedChinese = chineseMap[c]; 61 | if (simplifiedChinese != null) { 62 | return simplifiedChinese; 63 | } 64 | return c; 65 | } 66 | 67 | /** 68 | * 将单个简体字转换为繁体字 69 | * 70 | * @param c 需要转换的简体字 71 | * 72 | * @return 转换后的繁字体 73 | */ 74 | 75 | /// 76 | static String convertCharToTraditionalChinese(String c) { 77 | if (chineseMap.containsValue(c)) { 78 | Iterable> iterable = chineseMap.entries; 79 | for (int i = 0, length = iterable.length; i < length; i++) { 80 | MapEntry entry = iterable.elementAt(i); 81 | if (entry.value == c) { 82 | return entry.key; 83 | } 84 | } 85 | } 86 | return c; 87 | } 88 | 89 | /** 90 | * 将繁体字转换为简体字 91 | * 92 | * @param str 需要转换的繁体字 93 | * 94 | * @return 转换后的简体字 95 | */ 96 | 97 | /// 98 | static String convertToSimplifiedChinese(String str) { 99 | StringBuffer sb = new StringBuffer(); 100 | for (int i = 0, len = str.length; i < len; i++) { 101 | sb.write(convertCharToSimplifiedChinese(str[i])); 102 | } 103 | return sb.toString(); 104 | } 105 | 106 | /** 107 | * 将简体字转换为繁体字 108 | * 109 | * @param str 110 | * 需要转换的简体字 111 | * @return 转换后的繁字体 112 | */ 113 | 114 | /// 115 | static String convertToTraditionalChinese(String str) { 116 | StringBuffer sb = new StringBuffer(); 117 | for (int i = 0, len = str.length; i < len; i++) { 118 | sb.write(convertCharToTraditionalChinese(str[i])); 119 | } 120 | return sb.toString(); 121 | } 122 | 123 | ///添加繁体字字典 124 | static void addChineseDict(List list) { 125 | chineseMap.addAll(PinyinResource.getResource(list)); 126 | } 127 | } 128 | -------------------------------------------------------------------------------- /lib/src/pinyin_helper.dart: -------------------------------------------------------------------------------- 1 | import 'dart:collection'; 2 | 3 | import 'package:lpinyin/src/chinese_helper.dart'; 4 | import 'package:lpinyin/src/pinyin_exception.dart'; 5 | import 'package:lpinyin/src/pinyin_format.dart'; 6 | import 'package:lpinyin/src/pinyin_resource.dart'; 7 | 8 | /** 9 | * 汉字转拼音类 10 | */ 11 | 12 | /// 13 | class PinyinHelper { 14 | static Map pinyinMap = PinyinResource.getPinyinResource(); 15 | static Map multiPinyinMap = 16 | PinyinResource.getMultiPinyinResource(); 17 | 18 | static final String pinyinSeparator = ","; // 拼音分隔符 19 | // 所有带声调的拼音字母 20 | static final String allMarkedVowel = "āáǎàēéěèīíǐìōóǒòūúǔùǖǘǚǜ"; 21 | static final String allUnmarkedVowel = "aeiouv"; 22 | static int minMultiLength = 2; 23 | static int maxMultiLength = 0; 24 | 25 | /** 26 | *获取字符串首字拼音 27 | *@param str 需要转换的字符串 28 | * @return 首字拼音 29 | */ 30 | 31 | /// 32 | static String getFirstWordPinyin(String str) { 33 | String _pinyin = 34 | convertToPinyinStringWithoutException(str, separator: pinyinSeparator); 35 | return _pinyin.split(pinyinSeparator)[0]; 36 | } 37 | 38 | /** 39 | * 获取字符串对应拼音的首字母 40 | * @param str 需要转换的字符串 41 | * @param defPinyin 拼音分隔符 def: '#' 42 | * @return 对应拼音的首字母 43 | */ 44 | 45 | /// 46 | static String getShortPinyin(String str, {String defPinyin: '#'}) { 47 | StringBuffer sb = new StringBuffer(); 48 | String pinyin = convertToPinyinStringWithoutException(str, 49 | separator: pinyinSeparator, defPinyin: defPinyin); 50 | List list = pinyin.split(pinyinSeparator); 51 | list.forEach((value) { 52 | sb.write(value[0]); 53 | }); 54 | return sb.toString(); 55 | } 56 | 57 | /** 58 | * 将字符串转换成相应格式的拼音 59 | * @param str 需要转换的字符串 60 | * @param separator 拼音分隔符 def: " " 61 | * @param format 拼音格式 def: PinyinFormat.WITHOUT_TONE 62 | * @return 字符串的拼音 63 | */ 64 | 65 | /// 66 | static String convertToPinyinString(String str, 67 | {String separator: " ", PinyinFormat format: PinyinFormat.WITHOUT_TONE}) { 68 | StringBuffer sb = new StringBuffer(); 69 | str = ChineseHelper.convertToSimplifiedChinese(str); 70 | int strLen = str.length; 71 | int i = 0; 72 | while (i < strLen) { 73 | String subStr = str.substring(i); 74 | MultiPinyin node = convertToMultiPinyin(subStr, separator, format); 75 | if (node == null) { 76 | String _char = str[i]; 77 | if (ChineseHelper.isChinese(_char)) { 78 | List pinyinArray = convertToPinyinArray(_char, format); 79 | if (pinyinArray.length > 0) { 80 | sb.write(pinyinArray[0]); 81 | } else { 82 | throw new PinyinException("Can't convert to pinyin: " + _char); 83 | } 84 | } else { 85 | sb.write(_char); 86 | } 87 | if (i < strLen) { 88 | sb.write(separator); 89 | } 90 | i++; 91 | } else { 92 | sb.write(node.pinyin); 93 | i += node.word.length; 94 | } 95 | } 96 | return ((sb.toString().endsWith(separator) && separator != "") 97 | ? sb.toString().substring(0, sb.toString().length - 1) 98 | : sb.toString()); 99 | } 100 | 101 | /** 102 | * 将字符串转换成相应格式的拼音 (不能转换的字拼音默认用'#'替代 ) 103 | * @param str 需要转换的字符串 104 | * @param separator 拼音分隔符 def: " " 105 | * @param defPinyin 拼音分隔符 def: '#' 106 | * @param format 拼音格式 def: PinyinFormat.WITHOUT_TONE 107 | * @return 字符串的拼音 108 | */ 109 | 110 | /// 111 | static String convertToPinyinStringWithoutException(String str, 112 | {String separator: " ", 113 | String defPinyin: '#', 114 | PinyinFormat format: PinyinFormat.WITHOUT_TONE}) { 115 | StringBuffer sb = new StringBuffer(); 116 | str = ChineseHelper.convertToSimplifiedChinese(str); 117 | int strLen = str.length; 118 | int i = 0; 119 | while (i < strLen) { 120 | String subStr = str.substring(i); 121 | MultiPinyin node = convertToMultiPinyin(subStr, separator, format); 122 | if (node == null) { 123 | String _char = str[i]; 124 | if (ChineseHelper.isChinese(_char)) { 125 | List pinyinArray = convertToPinyinArray(_char, format); 126 | if (pinyinArray.length > 0) { 127 | sb.write(pinyinArray[0]); 128 | } else { 129 | sb.write(defPinyin); 130 | print("### Can't convert to pinyin: " + 131 | _char + 132 | " defPinyin: " + 133 | defPinyin); 134 | } 135 | } else { 136 | sb.write(_char); 137 | } 138 | if (i < strLen) { 139 | sb.write(separator); 140 | } 141 | i++; 142 | } else { 143 | sb.write(node.pinyin); 144 | i += node.word.length; 145 | } 146 | } 147 | return ((sb.toString().endsWith(separator) && separator != "") 148 | ? sb.toString().substring(0, sb.toString().length - 1) 149 | : sb.toString()); 150 | } 151 | 152 | /** 153 | * 获取多音字拼音 154 | * @param str 需要转换的字符串 155 | * @param separator 拼音分隔符 156 | * @param format 拼音格式 157 | * @return 多音字拼音 158 | */ 159 | 160 | /// 161 | static MultiPinyin convertToMultiPinyin( 162 | String str, String separator, PinyinFormat format) { 163 | if (str.length < minMultiLength) return null; 164 | if (maxMultiLength == 0) { 165 | List keys = multiPinyinMap.keys.toList(); 166 | for (int i = 0, length = keys.length; i < length; i++) { 167 | if (keys[i].length > maxMultiLength) { 168 | maxMultiLength = keys[i].length; 169 | } 170 | } 171 | } 172 | for (int end = minMultiLength, length = str.length; 173 | (end <= length && end <= maxMultiLength); 174 | end++) { 175 | String subStr = str.substring(0, end); 176 | String multi = multiPinyinMap[subStr]; 177 | if (multi != null && multi.length > 0) { 178 | List str = multi.split(pinyinSeparator); 179 | StringBuffer sb = new StringBuffer(); 180 | str.forEach((value) { 181 | List pinyin = formatPinyin(value, format); 182 | sb.write(pinyin[0]); 183 | sb.write(separator); 184 | }); 185 | return new MultiPinyin(word: subStr, pinyin: sb.toString()); 186 | } 187 | } 188 | return null; 189 | } 190 | 191 | /** 192 | * 将单个汉字转换为相应格式的拼音 193 | * @param c 需要转换成拼音的汉字 194 | * @param format 拼音格式 195 | * @return 汉字的拼音 196 | */ 197 | 198 | /// 199 | static List convertToPinyinArray(String c, PinyinFormat format) { 200 | String pinyin = pinyinMap[c]; 201 | if ((pinyin != null) && ("null" != pinyin)) { 202 | return formatPinyin(pinyin, format); 203 | } 204 | return new List(); 205 | } 206 | 207 | /** 208 | * 将带声调的拼音格式化为相应格式的拼音 209 | * @param pinyinStr 带声调格式的拼音 210 | * @param format 拼音格式 211 | * @return 格式转换后的拼音 212 | */ 213 | 214 | /// 215 | static List formatPinyin(String pinyinStr, PinyinFormat format) { 216 | if (format == PinyinFormat.WITH_TONE_MARK) { 217 | return pinyinStr.split(pinyinSeparator); 218 | } else if (format == PinyinFormat.WITH_TONE_NUMBER) { 219 | return convertWithToneNumber(pinyinStr); 220 | } else if (format == PinyinFormat.WITHOUT_TONE) { 221 | return convertWithoutTone(pinyinStr); 222 | } 223 | return new List(); 224 | } 225 | 226 | /** 227 | * 将带声调格式的拼音转换为不带声调格式的拼音 228 | * @param pinyinArrayStr 带声调格式的拼音 229 | * @return 不带声调的拼音 230 | */ 231 | 232 | /// 233 | static List convertWithoutTone(String pinyinArrayStr) { 234 | List pinyinArray; 235 | for (int i = allMarkedVowel.length - 1; i >= 0; i--) { 236 | int originalChar = allMarkedVowel.codeUnitAt(i); 237 | double index = (i - i % 4) / 4; 238 | int replaceChar = allUnmarkedVowel.codeUnitAt(index.toInt()); 239 | pinyinArrayStr = pinyinArrayStr.replaceAll( 240 | String.fromCharCode(originalChar), String.fromCharCode(replaceChar)); 241 | } 242 | // 将拼音中的ü替换为v 243 | pinyinArray = pinyinArrayStr.replaceAll("ü", "v").split(pinyinSeparator); 244 | // 去掉声调后的拼音可能存在重复,做去重处理 245 | LinkedHashSet pinyinSet = new LinkedHashSet(); 246 | pinyinArray.forEach((value) { 247 | pinyinSet.add(value); 248 | }); 249 | return pinyinSet.toList(); 250 | } 251 | 252 | /** 253 | * 将带声调格式的拼音转换为数字代表声调格式的拼音 254 | * @param pinyinArrayStr 带声调格式的拼音 255 | * @return 数字代表声调格式的拼音 256 | */ 257 | 258 | /// 259 | static List convertWithToneNumber(String pinyinArrayStr) { 260 | List pinyinArray = pinyinArrayStr.split(pinyinSeparator); 261 | for (int i = pinyinArray.length - 1; i >= 0; i--) { 262 | bool hasMarkedChar = false; 263 | String originalPinyin = pinyinArray[i].replaceAll("ü", "v"); // 将拼音中的ü替换为v 264 | for (int j = originalPinyin.length - 1; j >= 0; j--) { 265 | int originalChar = originalPinyin.codeUnitAt(j); 266 | // 搜索带声调的拼音字母,如果存在则替换为对应不带声调的英文字母 267 | if (originalChar < 'a'.codeUnitAt(0) || 268 | originalChar > 'z'.codeUnitAt(0)) { 269 | int indexInAllMarked = 270 | allMarkedVowel.indexOf(String.fromCharCode(originalChar)); 271 | int toneNumber = indexInAllMarked % 4 + 1; // 声调数 272 | double index = (indexInAllMarked - indexInAllMarked % 4) / 4; 273 | int replaceChar = allUnmarkedVowel.codeUnitAt(index.toInt()); 274 | pinyinArray[i] = originalPinyin.replaceAll( 275 | String.fromCharCode(originalChar), 276 | String.fromCharCode(replaceChar)) + 277 | toneNumber.toString(); 278 | hasMarkedChar = true; 279 | break; 280 | } 281 | } 282 | if (!hasMarkedChar) { 283 | // 找不到带声调的拼音字母说明是轻声,用数字5表示 284 | pinyinArray[i] = originalPinyin + "5"; 285 | } 286 | } 287 | 288 | return pinyinArray; 289 | } 290 | 291 | /** 292 | * 将单个汉字转换成带声调格式的拼音 293 | * @param c 需要转换成拼音的汉字 294 | * @return 字符串的拼音 295 | */ 296 | 297 | /// 298 | static List convertCharToPinyinArray(String c) { 299 | return convertToPinyinArray(c, PinyinFormat.WITH_TONE_MARK); 300 | } 301 | 302 | /** 303 | * 判断一个汉字是否为多音字 304 | * @param c汉字 305 | * @return 判断结果,是汉字返回true,否则返回false 306 | */ 307 | 308 | /// 309 | static bool hasMultiPinyin(String c) { 310 | List pinyinArray = convertCharToPinyinArray(c); 311 | if (pinyinArray != null && pinyinArray.length > 1) { 312 | return true; 313 | } 314 | return false; 315 | } 316 | 317 | ///添加拼音字典 318 | static void addPinyinDict(List list) { 319 | pinyinMap.addAll(PinyinResource.getResource(list)); 320 | } 321 | 322 | ///添加多音字字典 323 | static void addMultiPinyinDict(List list) { 324 | multiPinyinMap.addAll(PinyinResource.getResource(list)); 325 | } 326 | } 327 | 328 | class MultiPinyin { 329 | String word; 330 | String pinyin; 331 | 332 | MultiPinyin({this.word, this.pinyin}); 333 | 334 | @override 335 | String toString() { 336 | return " {" + 337 | " \"word\":\"" + 338 | word + 339 | "\"" + 340 | ", \"pinyin\":\"" + 341 | pinyin + 342 | "\"" + 343 | '}'; 344 | } 345 | } 346 | --------------------------------------------------------------------------------