cnchar

repository·master·Indexed 25 days ago

https://github.com/theajack/cnchar

A 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).

Tokens
44.9K
Snippets
115
Records
379
Agent score
35%

What's inside cnchar

  1. Overview of cnchar

    master
    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.
  2. Overview of cnchar features

    master

    cnchar 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.
  3. Overview of cnchar capabilities

    master

    cnchar 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+).
  4. Core Features of cnchar

    master

    cnchar 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-draw plugin.
    • Extensive Plugins: Access specialized data via plugins like cnchar-idiom (idioms), cnchar-xhy (歇后语/twists), cnchar-code (encodings like Unicode, Cangjie, Wubi), and cnchar-explain (definitions).
    • Environment Support: Works in browsers, Webpack, Node.js, and most JavaScript environments.
  5. Install and use cnchar-name for Chinese name generation

    master

    Starting from version 3.2.0, Chinese name generation features are provided via the cnchar-name library. This library can run independently of the main cnchar core. 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-name package.

  6. Install and use cnchar-idiom for idiom support

    master
    To use idiom (成语) features in cnchar, you must install the cnchar-idiom library. This library can run independently of the main cnchar core. If you are using a CDN, the CncharIdiom object will be exposed on the window object.
  7. Develop custom cnchar plugins

    master

    Plugins are independent modules that can be injected into cnchar. A plugin must have a pluginName property. The install method is called by cnchar.use(), receiving the cnchar instance as a callback, allowing the plugin to access core methods and other plugins.

    TypeScript Implementation

    For TypeScript support, it is recommended to install cnchar-types to 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;
  8. Setup cnchar-random for random data generation

    master

    To enable random generation of Pinyin, Chinese characters, idioms, etc., you must install cnchar-random. This library can run independently of the main cnchar library.

    If you are using cnchar as the host, you use cnchar.use(random, plugin). If you are using cnchar-random standalone, you must use random.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});
  9. Install cnchar

    master

    You can install cnchar via npm or include it directly in your HTML using a script tag.

    npm installation:

    npm i cnchar

    Usage in JavaScript/TypeScript:

    import cnchar from 'cnchar';
    // do something

    Usage via script tag:

    <script src="https://fastly.jsdelivr.net/gh/theajack/cnchar/dist/cnchar.latest.min.js"></script>
    <script>
        // do something
    </script>
    npm i cnchar
  10. Enable plugins using .use()

    master

    In 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);
  11. Initialize cnchar-trad plugin

    master

    To use the plugin in a JavaScript environment, import both cnchar and cnchar-trad, then register the plugin using cnchar.use(trad). Note that cnchar.use is optional in browser environments where the script is loaded via CDN.

    import cnchar from 'cnchar';
    import trad from 'cnchar-trad';
    cnchar.use(trad);
  12. Enable plugins in non-browser environments using .use()

    master

    In non-browser environments (like Node.js), you must explicitly enable plugins using the cnchar.use() method. Ensure the core cnchar library 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') after cnchar is sufficient.

    // Node.js environment
    var cnchar = require('cnchar');
    var xxx = require('cnchar-xxx');
    cnchar.use(xxx);