From 4c4736621067a6dfa89c0d9156d5ee64d7eda2f8 Mon Sep 17 00:00:00 2001 From: Konstantin Serov Date: Thu, 5 Jan 2023 00:25:31 +0700 Subject: [PATCH] fixes to PinyinUtils --- CHANGELOG.md | 2 ++ example/pubspec.lock | 2 +- lib/utils/pinyin_utils.dart | 34 ++++++++++++++++++++++++++++++++-- pubspec.yaml | 2 +- 4 files changed, 36 insertions(+), 4 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 9e37542..fed5fc6 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -1,3 +1,5 @@ +## [2.10.1] +- A few fixes to PinyinUtils ## [2.10.0] - Merged https://github.com/caseyryan/flutter_multi_formatter/pull/112 ## [2.9.14] diff --git a/example/pubspec.lock b/example/pubspec.lock index e2bda58..a93449b 100644 --- a/example/pubspec.lock +++ b/example/pubspec.lock @@ -96,7 +96,7 @@ packages: path: ".." relative: true source: path - version: "2.10.0" + version: "2.10.1" flutter_test: dependency: "direct dev" description: flutter diff --git a/lib/utils/pinyin_utils.dart b/lib/utils/pinyin_utils.dart index bca2239..ac323dd 100644 --- a/lib/utils/pinyin_utils.dart +++ b/lib/utils/pinyin_utils.dart @@ -221,6 +221,7 @@ class PinyinUtils { "wei", "hao", "tai", + "lue", "gan", "lve", "zha", @@ -436,6 +437,7 @@ class PinyinUtils { "me", "si", "ma", + "pi", "mi", "ce", "te", @@ -443,7 +445,9 @@ class PinyinUtils { "ai", "fa", "ba", + "ou", "e", + "o", "a", "r", ]; @@ -487,7 +491,13 @@ class PinyinUtils { /// Returns a list of tones for the whole sentence static List getPinyinTones(String sentence) { - return splitToSyllables(sentence).map(getPinyinTone).toList(); + return splitToSyllables(sentence) + .where((e) => e.isValid) + .map( + (e) => e.value, + ) + .map(getPinyinTone) + .toList(); } /// Detects a tone of a single syllable where 5 means neutral tone @@ -588,31 +598,49 @@ class PinyinUtils { /// converts all spcial symbols in pinyin to it's /// normal latin analog like ě -> e or ǔ -> u static String simplifyPinyin(String pinyin) { + int i = pinyin.length; while (pinyin.contains(_aRegex)) { + i--; pinyin = pinyin.replaceFirst(_aRegex, 'a'); + if (i <= 0) break; } + i = pinyin.length; while (pinyin.contains(_eRegex)) { + i--; pinyin = pinyin.replaceFirst(_eRegex, 'e'); + if (i <= 0) break; } + i = pinyin.length; while (pinyin.contains(_iRegex)) { + i--; pinyin = pinyin.replaceFirst(_iRegex, 'i'); + if (i <= 0) break; } + i = pinyin.length; while (pinyin.contains(_oRegex)) { + i--; pinyin = pinyin.replaceFirst(_oRegex, 'o'); + if (i <= 0) break; } + i = pinyin.length; while (pinyin.contains(_uRegex)) { + i--; pinyin = pinyin.replaceFirst(_uRegex, 'u'); + if (i <= 0) break; } /// i is just a safeguard from an endless loop - int i = pinyin.length; + i = pinyin.length; while (pinyin.contains(_uDottedRegex)) { i--; pinyin = pinyin.replaceFirst(_uDottedRegex, 'u'); if (i <= 0) break; } + i = pinyin.length; while (pinyin.contains(_vRegex)) { + i--; pinyin = pinyin.replaceFirst(_vRegex, 'v'); + if (i <= 0) break; } return pinyin; } @@ -622,6 +650,7 @@ class PinyinUtils { /// so it would split more precisely static const Map _splittableExceptions = { 'nine': 'ni ne', + 'jini': 'ji ni', }; static String splitToSyllablesBySeparator( @@ -648,6 +677,7 @@ class PinyinUtils { T == String || T == SyllableData, 'T can only be a String or a SyllableData', ); + value = value.replaceAll('\'', ''); var simplified = simplifyPinyin(value); /// тут надо вставить предопределенные пробелы, чтобы diff --git a/pubspec.yaml b/pubspec.yaml index f9983f6..6d859b4 100644 --- a/pubspec.yaml +++ b/pubspec.yaml @@ -1,6 +1,6 @@ name: flutter_multi_formatter description: A package of formatters for international phone numbers, credit / debit cards and a masked formatter -version: 2.10.0 +version: 2.10.1 homepage: https://github.com/caseyryan/flutter_multi_formatter environment: