cnchar
repository·master·Indexed 25 days ago
https://github.com/theajack/cncharA feature-rich JavaScript library for processing Chinese characters, supporting browser and Node.js environments. It provides tools for Pinyin conversion, stroke analysis, character drawing, idiom lookup, and traditional-to-simplified conversion. Version 3.2.6 includes plugins for visual stroke rendering (cnchar-draw), Xiehouyu (cnchar-xhy), radicals (cnchar-radical), character explanations (cnchar-explain), and voice synthesis/recognition (cnchar-voice).
What's inside cnchar
- cnchar is a comprehensive, multi-platform JavaScript library for Chinese character Pinyin, strokes, and other linguistic features. It is designed to be feature-rich and easy to use for various applications involving Chinese character processing.
Overview of cnchar features
mastercnchar is a comprehensive JavaScript library for handling Chinese characters, Pinyin, and stroke information. It supports a wide range of capabilities including:
- Pinyin & Phonetics: Get Pinyin (initials, case sensitivity, array splitting), handle polyphonic characters (多音字), and access phonetic details (initials, finals, tones).
- Stroke Information: Retrieve stroke counts, stroke orders, and detailed stroke names. Supports visual stroke drawing.
- Character Operations: Convert between Simplified, Traditional, and 'Mars' (火星文) characters. Search for characters by Pinyin, stroke count, or stroke order.
- Linguistic Data: Query idioms (成语), two-part allegorical sayings (歇后语), radicals (偏旁部首), and word formation (组词).
- Advanced Features: Speech synthesis and recognition, character encoding queries, and input method support (Pinyin and Wubi).
- Generation: Randomly generate Pinyin, characters, words, idioms, and Chinese names.
Technical Specifications:
- Small Footprint: Minified version is ~75KB; zipped version is ~50KB.
- Environment Support: Works in Browsers, Node.js, Mini-programs, React Native, Weex, Uniapp, Electron, and Webpack.
- Compatibility: Supports IE9 and above.
- Language: Written in TypeScript.
Overview of cnchar capabilities
mastercnchar is a comprehensive Chinese character processing library. Core capabilities include:
- Pinyin: Get pinyin, support for initials, case sensitivity, and tone marks.
- Strokes: Get stroke counts and stroke order; derive characters from stroke sequences.
- Conversions: Convert between Simplified Chinese, Traditional Chinese, and 'Mars Text' (火星文).
- Character Info: Access idioms, Xiehouyu, character explanations, and radical/component information.
- Audio: Speech synthesis (voice) and recognition (v3.1.0+).
Core Features of cnchar
mastercnchar is a comprehensive Chinese character utility library. Key capabilities include:
- Pinyin Support: Get Pinyin for characters, including support for initials, case sensitivity, and tone marks.
- Stroke Analysis: Retrieve stroke counts and stroke sequences (order), and derive characters from stroke sequences.
- Character Conversion: Convert between Simplified Chinese, Traditional Chinese, and 'Mars Text' (火星文).
- Polyphone Support: Handle polyphones (characters with multiple pronunciations) and polyphonic words (words with multiple pronunciations).
- Visual Drawing: Visualize character strokes using the
cnchar-drawplugin. - Extensive Plugins: Access specialized data via plugins like
cnchar-idiom(idioms),cnchar-xhy(歇后语/twists),cnchar-code(encodings like Unicode, Cangjie, Wubi), andcnchar-explain(definitions). - Environment Support: Works in browsers, Webpack, Node.js, and most JavaScript environments.
Install and use cnchar-name for Chinese name generation
masterStarting from version 3.2.0, Chinese name generation features are provided via the
cnchar-namelibrary. This library can run independently of the maincncharcore. It supports generating names, checking if a string is a surname, and checking if a string is a full name.To use these features, you must install the
cnchar-namepackage.Install and use cnchar-idiom for idiom support
masterTo use idiom (成语) features incnchar, you must install thecnchar-idiomlibrary. This library can run independently of the maincncharcore. If you are using a CDN, theCncharIdiomobject will be exposed on thewindowobject.Develop custom cnchar plugins
masterPlugins are independent modules that can be injected into
cnchar. A plugin must have apluginNameproperty. Theinstallmethod is called bycnchar.use(), receiving thecncharinstance as a callback, allowing the plugin to access core methods and other plugins.TypeScript Implementation
For TypeScript support, it is recommended to install
cnchar-typesto get proper declarations.import ICnChar, {IPlugin} from 'cnchar-types'; const plugin: IPlugin = { pluginName: 'custom', install (cnchar: ICnChar) { console.log(cnchar); }, version: '0.0.1', log: () => console.log('hello cnchar-plugin!'), }; declare module 'cnchar-types/main/index' { interface ICnChar { custom: { pluginName: 'custom'; version: string; log: () => void; }; } } export default plugin;Setup cnchar-random for random data generation
masterTo enable random generation of Pinyin, Chinese characters, idioms, etc., you must install
cnchar-random. This library can run independently of the maincncharlibrary.If you are using
cncharas the host, you usecnchar.use(random, plugin). If you are usingcnchar-randomstandalone, you must userandom.use(plugin)to register the necessary dictionary plugins.// Option 1: Using with cnchar import cnchar from 'cnchar'; import random from 'cnchar-random'; import idiom from 'cnchar-idiom'; cnchar.use(random, idiom); cnchar.random.idiom({number: 10}); // Option 2: Using cnchar-random standalone import random from 'cnchar-random'; import idiom from 'cnchar-idiom'; random.use(idiom); random.idiom({number: 10});Install cnchar
masterYou can install
cncharvia npm or include it directly in your HTML using a script tag.npm installation:
npm i cncharUsage in JavaScript/TypeScript:
import cnchar from 'cnchar'; // do somethingUsage via script tag:
<script src="https://fastly.jsdelivr.net/gh/theajack/cnchar/dist/cnchar.latest.min.js"></script> <script> // do something </script>npm i cncharEnable plugins using .use()
masterIn non-browser environments (like Node.js), you must explicitly enable plugins using the
.use()method. In browser environments, simply importing the plugin is sufficient.Node.js Usage:
var cnchar = require('cnchar'); var xxx = require('cnchar-xxx'); cnchar.use(xxx);Initialize cnchar-trad plugin
masterTo use the plugin in a JavaScript environment, import both
cncharandcnchar-trad, then register the plugin usingcnchar.use(trad). Note thatcnchar.useis optional in browser environments where the script is loaded via CDN.import cnchar from 'cnchar'; import trad from 'cnchar-trad'; cnchar.use(trad);Enable plugins in non-browser environments using .use()
masterIn non-browser environments (like Node.js), you must explicitly enable plugins using the
cnchar.use()method. Ensure the corecncharlibrary is imported first, followed by the desired plugin libraries.In browser environments, you do not need to call
.use(); simply importing the plugin (e.g.,import 'cnchar-xxx') aftercncharis sufficient.// Node.js environment var cnchar = require('cnchar'); var xxx = require('cnchar-xxx'); cnchar.use(xxx);