[Python] Python汉字转拼音 →→→→→进入此内容的聊天室

来自 , 2019-09-28, 写在 Python, 查看 122 次.
URL http://www.code666.cn/view/d882050b
  1. # -*- coding: utf-8 -*-
  2. # ------------------------------------------------------------
  3. # Script   Name: convert.py
  4. # Creation Date: 2010-09-21  02:12
  5. # Last Modified: 2011-11-12 18:38:13
  6. # Copyright (c)2011, DDTCMS Project
  7. # Purpose: This file used for DDTCMS Project
  8. # ------------------------------------------------------------
  9.  
  10. #####################################
  11. #   Written by caocao               #
  12. #   Modified by huyoo353@126.com    #
  13. #   caocao@eastday.com              #
  14. #   http://nethermit.yeah.net       #
  15. #####################################
  16.  
  17. # python.
  18. import sys,os
  19. import re
  20. import string
  21. class CConvert:
  22.         def __init__(self):
  23.                 self.has_shengdiao = False
  24.                 self.just_shengmu  = False
  25.                 self.spliter = '-'
  26.                 "Load data table"
  27.                 try:
  28.                         fp=open(os.path.join(settings.PROJECT_DIR, 'utils', 'convert-utf-8.txt'))
  29.                 except IOError:
  30.                         print "Can't load data from convert-utf-8.txt\nPlease make sure this file exists."
  31.                         sys.exit(1)
  32.                 else:
  33.                         self.data=fp.read().decode("utf-8")# decoded data to unicode
  34.                         fp.close()
  35.        
  36.         def convert1(self, strIn):
  37.                 "Convert Unicode strIn to PinYin"
  38.                 length, strOutKey, strOutValue, i=len(strIn), "", "", 0
  39.                 while i<length:
  40.                         code1 =ord(strIn[i:i+1])
  41.                         if code1>=0x4e02 and code1<=0xe863:
  42.                                 strTemp   = self.getIndex(strIn[i:i+1])
  43.                                 if not self.has_shengdiao:
  44.                                         strTemp  = strTemp[:-1]
  45.                                 strLength = len(strTemp)
  46.                                 if strLength<1:strLength=1
  47.                                 strOutKey   += string.center(strIn[i:i+1], strLength)+" "
  48.                                 strOutValue += self.spliter + string.center(strTemp, strLength) + self.spliter
  49.                         else:#ascii code;
  50.                                 strOutKey+=strIn[i:i+1]+" "
  51.                                 strOutValue+=strIn[i:i+1] + ' '
  52.                         i+=1
  53.                         #############################
  54.                         #txlist = utf8String.split()
  55.                         #out=convert.convert(utf8String)
  56.                         #l=[]
  57.                         #for t in map(convert.convert, txlist):
  58.                         #       l.append(t[0])
  59.                         #v = '-'.join(l).replace(' ','').replace(u'--','-').strip('-')
  60.                         #############################
  61.                 return [strOutValue, strOutKey]
  62.        
  63.         def getIndex(self, strIn):
  64.                 "Convert single Unicode to PinYin from index"
  65.                 if strIn==' ':return self.spliter
  66.                 if set(strIn).issubset("'\"`~!@#$%^&*()=+[]{}\\|;:,.<>/?"):return self.spliter # or return ""
  67.                 if set(strIn).issubset("-—!##%%&&()*,、。:;?? @@\{{|}}~~‘’“”《》【】++==×¥·… ".decode("utf-8")):return ""
  68.                 pos=re.search("^"+strIn+"([0-9a-zA-Z]+)", self.data, re.M)
  69.                 if pos==None:
  70.                         return strIn
  71.                 else:
  72.                         if not self.just_shengmu:
  73.                                 return pos.group(1)
  74.                         else:
  75.                                 return pos.group(1)[:1]
  76.        
  77.         def convert(self, strIn):
  78.                 "Convert Unicode strIn to PinYin"
  79.                 if self.spliter != '-' and self.spliter !='_' and self.spliter != '' and self.spliter != ' ':
  80.                         self.spliter = '-'
  81.                 pinyin_list=[]
  82.                 for c in strIn :
  83.                         pinyin_list.append(self.getIndex(c))
  84.                 pinyin=''
  85.                 for p in pinyin_list:
  86.                         if p==' ':
  87.                                 pinyin+= self.spliter
  88.                                 continue
  89.                         if len(p)<2:# only shengmu,just get one char,or number
  90.                                 #if p.isdigit():
  91.                                 #       pinyin += p + ' '
  92.                                 #else:
  93.                                 #       pinyin += p + ' '
  94.                                 pinyin += p + ' '
  95.                         else:
  96.                                 if not self.has_shengdiao: p = p[:-1]
  97.                                 pinyin += self.spliter + p + self.spliter
  98.                 pinyin = pinyin.replace(' ','') \
  99.                                 .replace(self.spliter+self.spliter,self.spliter) \
  100.                                 .strip(self.spliter+' ').replace(self.spliter+self.spliter,self.spliter)
  101.                 return pinyin
  102. #//python/1184

回复 "Python汉字转拼音"

这儿你可以回复上面这条便签

captcha