npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2024 – Pkg Stats / Ryan Hefner

cecc

v3.0.0

Published

繁簡轉換函式庫 追求正確率 先解析詞性再繁簡轉換 繁體中文↔簡體中文轉換 Chinese converter between Traditional Chinese and Simplified Chinese.

Downloads

12

Readme

npm version npm downloads

Known Vulnerabilities codebeat badge Codacy Badge DeepScan grade

CeCC - Colorless echo Chinese converter

在人工智慧讀通文義、繁簡轉換前,應用自然語言處理中文分詞、標注詞性、判斷語境之後再做轉換,應比單純詞彙比對更準確。辭典應可如維基百科般由眾人編輯,且記錄改變原由,加進 test suit。

Concepts

Chinese_converter 採用先中文分詞(附帶詞義、詞性標注),再以 CeL.zh_conversion 繁簡轉換的方法,來輕量化繁簡轉換辭典,同時達到較為精確的繁簡轉換。

單純從前至後替換文字的缺陷

CeL.zh_conversion 採用與 OpenCC 和新同文堂相同的技術,從前至後,於每個字元位置檢查符合辭典檔中詞彙的最長詞彙,一旦符合就置換並跳到下一個字元位置。

這種方法在遇到某些字詞必須與前一個字詞連動時,就可能漏失掉。例如「干」預設會轉成「幹」。(轉換標的通常是用途雜亂,最難找出規則又常出現、例外多的字詞。)因此當辭典檔中有「芒果」卻沒有「芒果乾」時,遇到「芒果干」就可能換成「芒果幹」。 我們可以藉由把這些需要連動的詞彙全部加入辭典檔來完善轉換結果,例如令「芒果干」轉換成「芒果乾」,additional.to_TW.txt 與 CN_to_TW.LTP.PoS.txt 中就有許多例子。但這造成辭典檔複雜龐大,就本例來說,我們畢竟不可能把所有動植物,如蘋果乾、響尾蛇乾全加進去。 而且每次加入新的詞彙也得考慮是否會影響到上下文。例如加入「上千」將造成「算得上千钧一发」因為先符合了「上千」,「钧一发」本身不在辭典檔中,將造成轉換錯誤。而加入了「下游」→「下游」,也得同時加入「下游戏」→「下游戏」以防止「玩一下游戏」、「停下游戏」被錯誤轉換。

此外這種做法最大的問題是不能依上下文判斷。例如「这颗梨子干你什么事」、「我拿水蜜桃干他朋友什么事」就不容易正確轉換。而有些詞像「排泄」、「排洩」,「自制」、「自製」有兩種可能性,也必須依上下文來判斷。

先中文分詞的好處

先中文分詞來判斷,可依句子與片語的結構來轉換。不但較靈活,也更能應對特殊情況。例如 zh_conversion 指定了許多 这只→這隻、一只→一隻 之類的轉換。在遇到「這隻是小鳥」、「這只是妄想」時常常出錯。若能判斷出「只」是否為量詞,則可減少許多錯誤。

雖然中文分詞有其優勢,可惜現在中文分詞精確度尚待加強。辭典檔規則極為依賴中文分詞系統,加上 zh_conversion 執行速度快許多,因此 Chinese_converter 只對分詞極少出錯,或者要寫成 zh_conversion 形式規則太過繁雜的特殊情況,才採用中文分詞辭典。

歡迎提供句子以做測試,也歡迎提交辭典檔規則。

Process

繁簡轉換流程:

  1. 中文分詞(採用外包程式中文斷詞)
  2. TODO: 自動判斷/手動指定句子、段落的語境(配合維基百科專有名詞轉換)。
  3. 依照相應詞典轉換各詞彙

Features

  1. 經由判斷詞性,可簡單判斷如何轉換。例如動詞用,形容詞用
  2. 自帶條件式生成功能,可快速生成辭典用的候選條件式。
  3. 自附 cache 功能,可大大降低多次轉譯長文的時間(例如在測試期間、修改辭典條件欲重新轉換)。

Installation

Install LTP first.

經實測,採用哈工大 LTP 4.1.5.post2 Base(v3) 模型的服務端版本,配合相對應辭典;以 繁→簡→繁 轉換測試檔中的文字,可轉換回原先之內容。

Install 中文分詞: LTP

On Windows, install LTP:

  1. 安裝 PyTorch 支援的 Python。(最新的 Python 常不能執行 PyTorch。)
  2. 安裝 LTP 與 LTP server 所依賴的軟體包:
pip install ltp
pip install tornado
pip install fire
  1. Upgrade LTP:
pip install --upgrade ltp

Install cecc

Install Node.js, and then install cecc:

npm install cecc

Usage

  1. 直接下載 LTP server 原始碼並改 'small''base'

  2. 啟動 LTP server,預設為 http://localhost:5000/ 。您可能需要 6 GB 記憶體來啟動 server。第一次執行需要下載超過 500 MiB 的辭典檔。

  3. Try codes:

    // load module
    const CeCC = require('cecc');
    // chinese_converter
    const cecc = new CeCC({ LTP_URL : 'http://localhost:5000/' });
    cecc.to_TW('简体中文');
    cecc.to_CN('繁體中文');
  4. 完整測試。

    # 重新生成 .converted.* 解答檔案。
    npm test regenerate_converted
    # TODO: 重新生成所有詞性查詢 cache。
    npm test ignore_cache

Mechanism 文字替換機制

  1. 若有符合附帶詞性辭典檔的文字,則依之變換。其他未符合的交由 CeL.extension.zh_conversion 處理。
  2. zh_conversion 基本上採用 OpenCC 的辭典,並以 generate_additional_table.js 合併新同文堂和 ConvertZZ 的辭典檔成 additional.to_TW.auto-generated.txt 與 additional.to_CN.auto-generated.txt。依照 CeL.extension.zh_conversion 中 Converter.options 之辭典檔順序,每個序列由長至短轉換。實際文字替換轉換作業在 CeL.data.Convert_Pairs 中的 function convert_using_pair_Map_by_length(text)

辭典修訂流程

Chinese_converter 辭典修訂過程

常規單句式辭典修訂流程

  1. 閱讀轉換過的文字,發現轉換錯誤。
  2. 改成正確的句子,填入測試檔 general.TW.txtgeneral.TW.answer.txt
  3. 啟動 LTP servernpm test 跑測試。
  4. 檢核測試工具自動生成的條件式,將合適的條件式填入辭典檔 CN_to_TW.LTP.PoS.txtTW_to_CN.LTP.PoS.txt。必要時添加新 filter 功能函數於 CN_to_TW.LTP.filters.js
  5. npm test 確認無衝突。
  6. 通過測試後 push 新辭典檔。

邊閱讀文本邊修訂流程

有時另外挑出句子會解析出不同語法,此時必須透過完整轉換文本修訂辭典:通過 work_crawler 選擇繁簡轉換功能,並隨時修訂辭典,應先設定 .cache_directory(work_crawler 會自動設定)。

  1. 閱讀轉換過的文字,發現轉換錯誤。
  2. 改成正確的句子,填入作品相應的測試檔 _test suite/articles/watch_target.作品名稱.(TW|CN).txt (e.g., watch_target.第一序列.TW.txt),會在每次轉換都測試是否有相符之文字。
  3. 持續修改辭典檔至能通過 npm test nowiki 測試。
  4. 重新生成繁簡轉換後文本,檢核測試工具自動生成的條件式,將合適的條件式填入辭典檔 CN_to_TW.LTP.PoS.txtTW_to_CN.LTP.PoS.txt
  5. 全部閱讀檢核完後,將作品相應的測試檔的文句填入測試檔 general.TW.txtgeneral.TW.answer.txt

Defect

  • LTP 轉換速率過慢。
  • 詞典仍過於薄弱、有缺陷,尚待加強。
  • 因為當前語言解析程式仍處於粗糙階段,解析結果不甚穩定;更換 LTP 程式版本就需要大幅改變辭典。

See also

中文分詞

久未更新

詞性標記 词性标注

久未更新

簡繁轉換

未考慮詞性之簡繁轉換: