Skip to main content

cn_sort

PyPI version Python versions License: MIT Downloads

按拼音和笔顺精确、快速排序大量简体中文词组。

支持百万数量级排序,有效解决多音字混排问题,中英文混用词组同样适用。

Architecture Overview


Features

  • 两种排序模式:拼音+笔顺(默认)或纯笔顺
  • 多音字智能处理:基于上下文自动区分多音字优先级(如"重庆"和"重要")
  • 大规模多进程加速:词组量超过阈值时自动切换多进程模式
  • 中英文混排:非中文字符(字母、数字、标点)排在汉字前面
  • 简单易用:一行代码完成排序

Installation

pip install cn-sort

Quick Start

from cn_sort import sort_text_list, Mode

# 按拼音排序(默认)
words = ['唯依', '唯衣', '唯一', '啊']
result = sort_text_list(words)
print(result)  # ['啊', '唯一', '唯衣', '唯依']

# 按笔顺排序
result = sort_text_list(['三', '二', '一', '一二'], mode=Mode.BIHUA)
print(result)  # ['一', '一二', '二', '三']

# 多音字自动处理
result = sort_text_list(['重要', '重庆'])
print(result)  # ['重庆', '重要']

Sorting Modes

Mode Description Example Input Example Output
Mode.PINYIN 按拼音再按笔顺(默认) ['三','一','二'] ['二','三','一'](er < san < yi)
Mode.BIHUA 仅按笔顺 ['三','一','二'] ['一','二','三'](1画<2画<3画)

Pinyin Sort Flow

Pinyin Sort Flow

Stroke Order Sort

Stroke Sort Flow


API Reference

sort_text_list(text_list, freeze=False, threshold=100000, mode=Mode.PINYIN)

对汉字词组列表排序。

Parameter Type Default Description
text_list list[str] 待排序词组列表
freeze bool False Windows 下非 if __name__ == '__main__' 调用时设 True
threshold int 100000 超过此数量时启用多进程
mode Mode Mode.PINYIN 排序模式

Returns: 排序后的 list[str]

set_stdout_level(level)

设置终端日志输出级别。level 可选:"DEBUG" / "INFO" / "WARN" / "ERROR" / "CRITICAL"


How It Works

算法思路

cn_sort 基于基数排序(LSD Radix Sort),将词组转换为优先级整数数组,再逐列稳定排序。

Radix Sort Diagram

  1. 建立优先级表:预先收集 2 万多个汉字的拼音与笔顺,生成 all_word.json(以空间换时间,哈希查询 O(1))
  2. 词→优先级元组:每个词中每个字通过拼音签名(如 人_ren2)查表,得到整数优先级
  3. LSD 基数排序:从最低位(最后一字)到最高位(第一字),逐列使用 Python timsort 稳定排序
  4. 多音字处理pypinyin 结合词语上下文自动选择正确读音

多进程架构(大规模模式)

当词组数量超过 threshold(默认 10 万)时,自动启用多进程生产者-消费者架构:

Multiprocess Pipeline

  • 生产者进程(CPU 核数 - 1 个):jieba 分词,过滤重复词,推入各自独立的 Queue
  • 消费者进程(1 个):从所有队列收集词,批量查询优先级表,建立映射缓存
  • 主进程:汇总分段结果,应用映射,最终排序

优先级表结构

Priority Table


Performance

Benchmark

  • 小规模(< 1000 词):单进程,毫秒级
  • 中规模(1 万词):约 0.18s
  • 大规模(100 万词):启用多进程,利用全部 CPU 核心

多音字示例

Polyphonic Example


Dependencies

  • pypinyin — 汉字转拼音(含上下文多音字识别)
  • jieba — 中文分词(大规模模式下使用)

Contributing

欢迎提交 Issue 和 Pull Request!

  1. Fork 本仓库
  2. 创建功能分支:git checkout -b feature/your-feature
  3. 提交修改:git commit -m 'feat: add your feature'
  4. 推送分支:git push origin feature/your-feature
  5. 开启 Pull Request

License

MIT License. See LICENSE for details.

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.10.0.tar.gz (472.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.10.0-py3-none-any.whl (495.5 kB view details)

Uploaded Python 3

File details

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

File metadata

  • Download URL: cn_sort-0.10.0.tar.gz
  • Upload date:
  • Size: 472.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.10.0.tar.gz
Algorithm Hash digest
SHA256 ff6fff4b08c60b7ba46364d05ed8f02ff4acf8afe9d670042c8d790a7aa42631
MD5 43a06f58e5ecbf71e5f557e1b61a9410
BLAKE2b-256 c7602ff9c67c542c245ff0750913fa7d2a989f3832b828f1bfd8c7df7bebfd43

See more details on using hashes here.

File details

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

File metadata

  • Download URL: cn_sort-0.10.0-py3-none-any.whl
  • Upload date:
  • Size: 495.5 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.10.0-py3-none-any.whl
Algorithm Hash digest
SHA256 ad979dc214f03f6849e6c5a77ee583bbbc032a39d82bb53452bda6a9ff9969a2
MD5 3407a2b89d14f55fd7c23546996573c5
BLAKE2b-256 8a1f9d550578f8020f67bfbf205f6cf813d9b6f1176d4af8583ed729c7acc222

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

0.11.0

2 files

This release

0.10.0 This release

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