Skip to main content

cn_sort

Fast, accurate Chinese word sorting by Pinyin or stroke order

快速、精确地按拼音或笔顺排序简体中文词组

PyPI version Python versions Downloads License: MIT GitHub stars

Installation · Quick Start · API · How It Works · Contributing


Overview

cn_sort sorts Simplified Chinese word lists by Pinyin (with stroke order as tiebreaker) or stroke order alone. It handles polyphonic characters correctly using context-aware pinyin detection, and scales to millions of words via a multi-process pipeline.

支持百万量级中文词组排序,多音字自动识别,中英混排均可处理。

from cn_sort import sort_text_list, Mode

sort_text_list(['唯依', '唯衣', '唯一', '啊'])
# → ['啊', '唯一', '唯衣', '唯依']

sort_text_list(['重要', '重庆'])
# → ['重庆', '重要']  ✓ polyphonic: chóng vs zhòng

Features

Feature Details
🔤 Pinyin sort Tone-aware, uses context to pick correct reading for polyphonic chars
✍️ Stroke-order sort Pure stroke-count ordering (Mode.BIHUA)
Scales to 1M+ words Auto-switches to multiprocess producer-consumer pipeline
🔡 Mixed content Latin / numeric / CJK all handled; non-CJK sorts before CJK
📦 Zero config pip install cn-sort — no extra data downloads needed

Installation

pip install cn-sort

Requirements: Python 3.6+, pypinyin, jieba (installed automatically)


Quick Start

from cn_sort import sort_text_list, Mode

# --- Pinyin mode (default) ---
sort_text_list(['唯依', '唯衣', '唯一', '啊'])
# ['啊', '唯一', '唯衣', '唯依']

# --- Stroke-order mode ---
sort_text_list(['三', '二', '一', '一二'], mode=Mode.BIHUA)
# ['一', '一二', '二', '三']

# --- Polyphonic character handling ---
sort_text_list(['重要', '重庆'])
# ['重庆', '重要']   (chóng < zhòng)

# --- Mixed Chinese / English ---
sort_text_list(['中国', 'abc', '啊'])
# ['abc', '啊', '中国']

# --- Large-scale: multiprocess kicks in automatically ---
sort_text_list(my_million_word_list)   # threshold=100000 by default

API Reference

sort_text_list(text_list, freeze=False, threshold=100000, mode=Mode.PINYIN) → list[str]

Parameter Type Default Description
text_list list[str] required Words to sort
mode Mode Mode.PINYIN PINYIN or BIHUA
threshold int 100_000 Switch to multiprocess above this count
freeze bool False Set True when calling outside if __name__ == '__main__' on Windows

set_stdout_level(level: str) → bool

Set console log verbosity. level{"DEBUG", "INFO", "WARN", "ERROR", "CRITICAL"}.

Mode (enum)

Value Meaning
Mode.PINYIN Sort by Pinyin reading, stroke order as tiebreaker
Mode.BIHUA Sort by stroke count only

How It Works

cn_sort uses LSD Radix Sort — each word is converted to a tuple of integer priorities, then sorted column-by-column (last character first) using Python's stable timsort.

Architecture Overview

Step-by-step (Pinyin mode)

Pinyin Sort Flow

  1. Priority table — 20 000+ characters pre-ranked by pinyin + stroke order, stored in all_word.json for O(1) hash lookup
  2. Word → tuple — each character maps to char_pinyin signature (e.g. 人_ren2), looked up in the table
  3. LSD radix sort — sort from least-significant column to most-significant using operator.itemgetter for speed
  4. Polyphonic charspypinyin uses surrounding context to pick the correct reading automatically

Radix Sort Diagram

Stroke-order mode

Stroke Sort Flow

Characters are ranked by their stroke increment level instead of pinyin signature. No jieba dependency needed.

Large-scale multiprocess pipeline

When len(words) > threshold, cn_sort spawns a producer-consumer process pool:

Multiprocess Pipeline

  • N producer processes (one per CPU − 1): segment text with jieba, deduplicate, push to independent queues
  • 1 consumer process: reads all queues, builds a priority-tuple cache for every unique token
  • Main process: reassembles segments using the cache, applies final radix sort

Priority table schema

Priority Table


Performance

Benchmark

Scale Mode Time
< 1 000 words single-process < 5 ms
10 000 words single-process ~180 ms
1 000 000 words multiprocess ~20 s (4-core)

README note: the jieba segmentation step dominates large-scale runs. Replacing jieba with a faster segmenter would be the highest-leverage future optimisation.

Polyphonic example

Polyphonic Example


Dependencies

Package Purpose
pypinyin Context-aware hanzi → pinyin conversion
jieba Chinese word segmentation (multiprocess mode only)

Contributing

Contributions are welcome! Here's how to get started:

git clone https://github.com/bmxbmx3/cn_sort.git
cd cn_sort
pip install pypinyin jieba
  1. Fork the repo and create a feature branch: git checkout -b feat/your-feature
  2. Make your changes and add tests if applicable
  3. Open a Pull Request — describe what you changed and why

Good first issues: improving large-scale performance, adding Traditional Chinese support, writing a test suite.


License

MIT © bmxbmx3

Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

cn_sort-0.11.0.tar.gz (473.3 kB view details)

Uploaded Source

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

cn_sort-0.11.0-py3-none-any.whl (495.9 kB view details)

Uploaded Python 3

File details

Details for the file cn_sort-0.11.0.tar.gz.

File metadata

  • Download URL: cn_sort-0.11.0.tar.gz
  • Upload date:
  • Size: 473.3 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/7.0.0 CPython/3.12.7

File hashes

Hashes for cn_sort-0.11.0.tar.gz
Algorithm Hash digest
SHA256 1442b0fab2c9fbe0813339734b24b07a45ee43f20a33714ff724848d24503faa
MD5 e4abd7c0ee41681dd0c4bba6dce5395a
BLAKE2b-256 b473f401dfd90214c48c788b57cb3d6591a52e1b61ed8d35a85688e0547814df

See more details on using hashes here.

File details

Details for the file cn_sort-0.11.0-py3-none-any.whl.

File metadata

  • Download URL: cn_sort-0.11.0-py3-none-any.whl
  • Upload date:
  • Size: 495.9 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/7.0.0 CPython/3.12.7

File hashes

Hashes for cn_sort-0.11.0-py3-none-any.whl
Algorithm Hash digest
SHA256 e878244fbaadccb9f044e340e1b1f4ab30ab382ea75e824c2f38b23cd3fb4afe
MD5 c4ae74e5f0e76d603bcb1fd0475cc629
BLAKE2b-256 d7ea2948a882ea7bf05958f0b2a61115afbc74f15b0e787ac4e9794e6c75a99e

See more details on using hashes here.

Release history Release notifications | RSS feed

0.14.0

2 files

0.13.0

2 files

0.12.1

2 files

0.12.0

2 files

This release

0.11.0 This release

2 files

0.10.0

2 files

0.9.0

2 files

0.8.0

1 file

0.7.9

1 file

0.7.8

1 file

0.7.6

1 file

0.7.5

1 file

0.7.4

1 file

0.7.3

1 file

0.7.2

1 file

0.7.1

1 file

0.7.0

1 file

0.6.6

1 file

0.6.5

1 file

0.6.4

1 file

0.6.3

1 file

0.6.2

2 files

0.6.1

2 files

0.5.9

2 files

0.5.7

1 file

0.5.6

1 file

0.5.5

1 file

0.5.4

1 file

0.5.3

1 file

0.5.2

1 file

0.5.1

1 file

0.4.8

1 file

0.4.7

1 file

0.4.6

1 file

0.4.5

1 file

0.4.4

1 file

Anthropic, PBC Visionary sponsor Bloomberg Visionary sponsor Hudson River Trading Visionary sponsor Meta Visionary sponsor NVIDIA Visionary sponsor Microsoft Sustainability sponsor Depot Continuous Integration AWS Cloud computing and Security Sponsor Datadog Monitoring Fastly CDN Google Download Analytics Sentry Error logging StatusPage Status page