pinyin_pro_flutter library

A professional Chinese pinyin conversion library for Flutter/Dart.

Ported from the TypeScript pinyin-pro library (v3.28.x).

Quick start

import 'package:pinyin_pro_flutter/pinyin_pro_flutter.dart';

// Basic conversion
pinyin('汉语拼音'); // 'hàn yǔ pīn yīn'

// Array output
pinyin('汉语', options: PinyinOptions(returnType: PinyinReturnType.array));
// ['hàn', 'yǔ']

// Text-pinyin matching
matchPinyin('中文拼音', 'zwp'); // [0, 1, 2]

// HTML ruby markup
htmlPinyin('汉字'); // '<span class="py-result-item">...'

Classes

CustomPinyinOptions
Options for customPinyin.
DictOptions
Options for addDict.
HtmlOptions
Options for htmlPinyin.
MatchOptions
Options for matchPinyin.
PinyinAllData
Full information about a single character's pinyin.
PinyinOptions
Configuration for the pinyin function.
PolyphonicAllData
Full info about one reading of a character from polyphonic.
PolyphonicOptions
Options for polyphonic.
SegmentItem
Output segment from segment function.
SegmentOptions
Options for segmentPinyin.
SingleWordResult
Internal intermediate result for a single character.
ToneSandhiOptions
Fine-grained control over tone sandhi (变调).

Enums

ConvertFormat
Format for convertPinyin.
CustomDictType
Dict type identifiers for clearCustomDict.
CustomHandleType
Whether to add to or replace the existing dict entry.
DictHandleType
How to handle single-character dict1 entries.
InitialPattern
How y/w are treated as initials.
MatchPrecision
Matching precision levels.
MatchSpace
Space handling during matching.
NonZhMode
How non-Chinese characters are handled.
OutputFormat
Output format for segmentPinyin.
PinyinMode
Pinyin matching mode.
PinyinPattern
Which phonetic component to return.
PinyinReturnType
Return format for pinyin function.
PolyphonicReturnType
Return type for polyphonic.
SurnameMode
Scope of surname matching.
TokenizationAlgorithm
Tokenization algorithm used by AcAutomaton.search.
ToneType
Tone display format.

Functions

addDict(Map<String, DictValue> dict, {DictOptions? options}) → void
Adds a custom dictionary to the AC automaton.
addTraditionalDict(Map<String, String> dict) → void
Registers a traditional→simplified character mapping.
clearCustomDict(dynamic dict) → void
Clears the specified custom dict(s).
convertPinyin(dynamic input, {String separator = ' ', ConvertFormat format = ConvertFormat.numToSymbol}) → dynamic
Converts pinyin format between numeric-tone, symbol-tone, and no-tone.
customPinyin(Map<String, String> config, {CustomPinyinOptions? options}) → void
Registers custom pinyin mappings.
getAllPinyin(String char, [SurnameMode surname = SurnameMode.off]) → List<String>
Returns all possible pinyin readings for char.
getFinalParts(String pinyin) → ({String body, String head, String tail})
Returns the head, body, and tail components of the final (韵母) in pinyin.
getInitialAndFinal(String pinyin, [InitialPattern? initialPattern]) → ({String final_, String initial})
Splits pinyin by spaces and returns the initial (声母) and final (韵母) components joined by spaces.
getNumOfTone(String pinyin) → String
Returns the tone number (0–4) for each syllable in pinyin.
getPinyinWithoutTone(String pinyin) → String
Removes tone marks from pinyin, returning plain vowels.
getSingleWordPinyin(String char) → String
Returns the most common pinyin for a single Chinese character.
getTraditionalDict() → List<String?>
Returns the current traditional character mapping.
htmlPinyin(String text, {HtmlOptions options = const HtmlOptions()}) → String
Generates an HTML string with ruby annotations (pinyin over Chinese chars).
matchPinyin(String text, String pinyinQuery, {MatchOptions options = const MatchOptions()}) → List<int>?
Checks whether text (Chinese string) matches the pinyinQuery.
pinyin(String word, {PinyinOptions options = const PinyinOptions()}) → dynamic
Converts a Chinese word to its pinyin representation.
pinyinAll(String word, {PinyinOptions options = const PinyinOptions()}) → List<PinyinAllData>
Converts word to a list of PinyinAllData objects.
pinyinArray(String word, {PinyinOptions options = const PinyinOptions()}) → List<String>
Converts word to a list of pinyin syllables.
pinyinString(String word, {PinyinOptions options = const PinyinOptions()}) → String
Converts word to a pinyin String joined by PinyinOptions.separator (a space by default).
polyphonic(String text, {PolyphonicOptions options = const PolyphonicOptions()}) → dynamic
Returns all possible pinyin readings for each character in text.
polyphonicAll(String text, {PolyphonicOptions options = const PolyphonicOptions()}) → List<List<PolyphonicAllData>>
Returns the readings of each character in text as PolyphonicAllData.
polyphonicArray(String text, {PolyphonicOptions options = const PolyphonicOptions()}) → List<List<String>>
Returns the readings of each character in text as nested lists.
polyphonicString(String text, {PolyphonicOptions options = const PolyphonicOptions()}) → List<String>
Returns one space-separated readings string per character in text.
removeDict([String? dictName]) → void
Removes the dictionary named dictName from the AC automaton.
segmentAllArray(String word, {SegmentOptions options = const SegmentOptions()}) → List<List<SegmentItem>>
Segments word and returns nested SegmentItem lists.
segmentAllSegment(String word, {SegmentOptions options = const SegmentOptions()}) → List<SegmentItem>
Segments word and returns one SegmentItem per segment.
segmentAllString(String word, {SegmentOptions options = const SegmentOptions()}) → SegmentItem
Segments word and returns a single SegmentItem.
segmentPinyin(String word, {SegmentOptions options = const SegmentOptions()}) → dynamic
Converts word to pinyin with word segmentation.
segmentPinyinArray(String word, {SegmentOptions options = const SegmentOptions()}) → List<List<String>>
Segments word and returns nested pinyin lists.
segmentPinyinSegment(String word, {SegmentOptions options = const SegmentOptions()}) → List<String>
Segments word and returns pinyin for each segment.
segmentPinyinString(String word, {SegmentOptions options = const SegmentOptions()}) → String
Segments word and returns a single pinyin string joined by SegmentOptions.separator (a space by default).
segmentZhArray(String word, {SegmentOptions options = const SegmentOptions()}) → List<List<String>>
Segments word and returns nested original-text lists.
segmentZhSegment(String word, {SegmentOptions options = const SegmentOptions()}) → List<String>
Segments word and returns the original text of each segment.
segmentZhString(String word, {SegmentOptions options = const SegmentOptions()}) → String
Segments word and returns the original text as a single string.