Skip to content

Commit

Permalink
fixes to PinyinUtils
Browse files Browse the repository at this point in the history
  • Loading branch information
caseyryan committed Jan 4, 2023
1 parent 43cceeb commit 4c47366
Show file tree
Hide file tree
Showing 4 changed files with 36 additions and 4 deletions.
2 changes: 2 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -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]
Expand Down
2 changes: 1 addition & 1 deletion example/pubspec.lock
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
34 changes: 32 additions & 2 deletions lib/utils/pinyin_utils.dart
Original file line number Diff line number Diff line change
Expand Up @@ -221,6 +221,7 @@ class PinyinUtils {
"wei",
"hao",
"tai",
"lue",
"gan",
"lve",
"zha",
Expand Down Expand Up @@ -436,14 +437,17 @@ class PinyinUtils {
"me",
"si",
"ma",
"pi",
"mi",
"ce",
"te",
"na",
"ai",
"fa",
"ba",
"ou",
"e",
"o",
"a",
"r",
];
Expand Down Expand Up @@ -487,7 +491,13 @@ class PinyinUtils {

/// Returns a list of tones for the whole sentence
static List<int> getPinyinTones(String sentence) {
return splitToSyllables<String>(sentence).map(getPinyinTone).toList();
return splitToSyllables<SyllableData>(sentence)
.where((e) => e.isValid)
.map(
(e) => e.value,
)
.map(getPinyinTone)
.toList();
}

/// Detects a tone of a single syllable where 5 means neutral tone
Expand Down Expand Up @@ -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;
}
Expand All @@ -622,6 +650,7 @@ class PinyinUtils {
/// so it would split more precisely
static const Map<String, String> _splittableExceptions = {
'nine': 'ni ne',
'jini': 'ji ni',
};

static String splitToSyllablesBySeparator(
Expand All @@ -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);

/// тут надо вставить предопределенные пробелы, чтобы
Expand Down
2 changes: 1 addition & 1 deletion pubspec.yaml
Original file line number Diff line number Diff line change
@@ -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:
Expand Down

0 comments on commit 4c47366

Please sign in to comment.