def addpinyin(text): ltext = list(text) textcount=len(text) pinyin = []#用过的拼音 index = []#用过的索引 pinyincount=int(textcount*0.5)#最小添加拼音的字数,1个字 print textcount,pinyincount for i in range(pinyincount): j = random.randint(0,textcount-1)#随机抽取一个文字的索引 if findindex(j,index):continue index.append(j) s = ltext[j]#随机抽取一个文字 if s.strip()=="":continue if s.strip().isdigit():continue p = hanzi2pinyin(s)#获取这个字的拼音 if p in pinyin:continue pinyin.append(p)#添加到已用拼音列表中 news = "%s(%s)" % (s,hanzi2pinyin(s)) ltext[j] = news return "".join(ltext)
def assign_pinyin(person, propertyName, phoneticPropertyName): name = person.valueForProperty_(propertyName) if name and contain_cjk_char(name): pname = person.valueForProperty_(phoneticPropertyName) if not reset and pname: return None name_py = pinyin.hanzi2pinyin(name).capitalize() person.setValue_forProperty_(name_py, phoneticPropertyName) return (name, name_py) return None
def assign_pinyin(person, propertyName, phoneticPropertyName): name = person.valueForProperty_(propertyName) if name and contain_cjk_char(name): pname = person.valueForProperty_(phoneticPropertyName) if not args.reset and pname: return None name_py = pinyin.hanzi2pinyin(name).capitalize() if not args.dryrun: person.setValue_forProperty_(name_py, phoneticPropertyName) return (name, name_py) return None
def testMixedText(self): assert hanzi2pinyin(u'公共的 模 asdf!') == u'gonggongde mo asdf!'
def testEnglish(self): assert hanzi2pinyin(u'this is a english') == u'this is a english'
def testChinese(self): assert hanzi2pinyin(u'公共的模') == u'gonggongdemo'