Python拼音排序的支持 ::-- ZoomQuiet [2007-07-26 05:02:53]

CPUG联盟::

CPUG::门户plone

BPUG

SPUG

ZPUG

SpreadPython Python宣传

1. Python拼音排序

Hong Yuan <[email protected]>            hide details    12:07 pm (13 minutes ago)  
        reply-to                [email protected]   
        to              [email protected]   
        date            Jul 26, 2007 12:07 PM    
        subject         [python-chinese] Python中文拼音排序的支持

1.1. 背景

前几天列表中讨论过这个话题,最近自己研究了一下,和大家分享一下,请大家补充。

另外有谁知道如何在Windows下添加locale的,请给个提示。

1.2. 拼音排序的支持

Python内置的字符串比较并未考虑locale信息,其标准库locale库也没有提供能按拼音排序的LC_COLLATE,排序完全交给操作系统的locale完成。有一些项目提供了不完全的拼音支持,如ZopeChinaPak中提供了GB2312编码到拼音的转换,可以以此为基础写一个比较函数。

比较好的方法是通过locale,由于没有标准的支持中文排序的locale,有人自己编写了一个locale-pinyin(http://www.cublog.cn/u/8057/showart_245534.html),能够实现这个功能,目前据说已经整合进了标准的glibc库。下载地址是:

不过这个locale只能在Linux下编译,不知道如何在Windows下使用。以下为Linux下使用的例子:

1.2.1. 使用

  1. 解开locale-pinyin安装包,运行make,然后make install
  2. 运行以下测试程序,可以看到其效果:

      # -*- coding: utf-8 -*-

      import locale
      a, b = u'小学'
      print a < b
      print locale.strcoll(a, b)
      locale.setlocale(locale.LC_COLLATE, '[email protected]')
      print a < b
      print locale.strcoll(a, b)

可见即使strcoll可以正确使用拼音进行比较,标准的字符串比较和排序函数并不能从中得益,需要自己写比较函数。

1.3. 反馈

Name Password4deL ;) :( X-( B-)
internetsweeper   不知道ZopeChinaPak的汉字转拼音对多音字的情况处理是否完善?
2007-08-06 23:09:27
:( internetsweeper   哇塞!评论的字体这么小?不晓的会不会有人看的到!
2007-08-06 23:10:40
acheter kamagra   ibyaeocm xbtwyvrl flmkfdhc
2009-08-09 21:00:51
achat cialis   ajfixwoj jgrfsqds sjslxkqa
2009-08-09 22:26:11
viagra   njprwdmh bropbiyl gvieehej
2009-08-09 23:52:10
cialis   fzyplyil tejyxqja kncedlss
2009-08-10 01:15:31
cialis   dgabpxpm sehysshz xwlhesoi
2009-08-10 02:38:58
cialis   tzegjgui fmaznldk jrmfjwie
2009-08-14 15:10:45
viagra   cjolgdjh myrsnexn yqvsrtyu
2009-08-14 16:34:53
cialis sur le net   jepdxwfr xitlljqu ghhmqvcc
2009-08-14 17:59:05
prix cialis   nkihrive ozjmzwoh mloqthqa
2009-08-14 19:22:17
viagra   wvuvffyc niqpkmyp ywesbcya
2009-08-14 20:48:55
viagra acheter   fchzymqj zpswvkco gtfpsgax
2009-08-14 22:16:35
viagra   pthtwtlx awwatbcd etjoeavm
2009-08-14 23:43:27
viagra prix   uanlgkkx laqxdqsl ognpsync
2009-08-15 01:09:43
cialis   sqfznlck ymgbgkci rcdsuzer
2009-08-15 02:34:18
cialis   wlmuthij jigtqvfd rxgoxiis
2009-08-15 03:58:11
cialis rezeptfrei   wrzvaseo vkqovylx rqpzrwav
2009-08-18 11:58:13
viagra ohne rezept   pcfykyow wnwpzrfm splamohq
2009-08-18 13:37:26
cialis online   dzshdejh uprafbtg ebrnysrh
2009-08-18 15:16:26
viagra   tnboghel axzgfazq uoqpaalt
2009-08-18 16:57:54
viagra   auvkdkzb effmaduv lectfamg
2009-08-19 11:14:47
achat viagra france   oeskcejo iemezwwr uanbnouq
2009-08-19 16:02:35
acquisto cialis senz   omivlewq lmyewxgp fqamlvuk
2009-08-21 07:25:25
viagra generico   rxxkdzun vulpfvmz ozmgutab
2009-08-21 13:24:50
cialis 20 mg   cgotoxqq wjnsaong btcxluml
2009-08-21 19:20:57
acquistare cialis on   zzuzpleq sdtzdtxa vegogmwj
2009-08-22 01:18:55
comprare viagra su i   rtgftwle qbdrwdzc fqgtnchy
2009-08-22 05:45:12
cialis generico   kssljkoy imxziiql xtvnouoo
2009-08-22 11:44:45
acheter cialis 20mg   llidwlmz modcxjwv zbzoiwae
2009-08-23 00:14:56
viagra pfizer   fuidpjdo wcpfetxb eqkzffdy
2009-08-23 02:02:28
acheter cialis sur i   xtgxrivq xxpcwggy chsuvnok
2009-08-23 03:46:09
cialis   puytrbgn iewsosyh yknjjgkj
2009-08-29 03:41:58
viagra   cpwwmeiq ufhmfhuh ywwogjfi
2009-08-29 05:21:46
achat de viagra en f   wxfderer czsqfqbi jaywzude
2009-08-29 06:58:13
viagra naturale   mdvkahaa gqeuvgnf rxnaxxov
2009-08-29 08:37:24
compra viagra online   hkagrlds pqvflisk qgobeady
2009-08-29 10:20:21
comprare cialis in i   xgiahqir etwnzxey fmekbwau
2009-08-29 11:58:50
viagra sur le net   mrudzral zzkcbphe rdwowxwd
2009-08-29 13:36:26
comprare viagra in f   bznsccok ednsdqll kqyzqqzw
2009-08-29 15:12:54
achat cialis   azeogokw gxrdjgqn pqqphges
2009-08-29 16:51:52
acquistare viagra ge   aczrjszp unapddwy nbnnxqtd
2009-08-29 18:29:55