Python之拼音拆分
经常会需要用到将zhangwei
转化为ZhangWei
、Zw
、Zhangw
之类的,就涉及到一个拼音拆分算法,这里写了一个demo分享给大家
我的思路是先将声母转换为大写,然后就可以根据大写字母来分割单个拼音
学的拼音早忘差不多了,百度了一下,声母有bpmfdtnlgkhjqxrzczyw
转化代码
def sm(strs):
smlist = 'bpmfdtnlgkhjqxrzcsyw'
for s in smlist:
strs = strs.replace(s,s.upper())
return strs
然后发现有个问题,韵母中也包含了声母的元素,zhangwei
就会变成ZHaNGWei
发现两个问题,一个是Zh
、Ch
、Sh
这类的包含了声母h,一个是er
、an
、en
、in
、un
、vn
、ang
、eng
、ing
、ong
这类的包含了声母r
、n
、g
于是再加一个转换
def sm(strs):
smlist = 'bpmfdtnlgkhjqxrzcsyw'
nosm = ['eR','aN','eN','iN','uN','vN','nG','NG']
rep = {'ZH':'Zh','CH':'Ch','SH':'Sh'}
for s in smlist:
strs = strs.replace(s,s.upper())
for s in nosm:
strs = strs.replace(s,s.lower())
for s in rep.keys():
strs = strs.replace(s,rep[s])
return strs
这时候zhangwei
已经可以转为ZhangWei
了
在进行批量转换的时候又遇到一个问题,碰到chenguiying
(陈桂英)这种拼音的时候,会转化为ChenguiYing
,这是因为r
、n
、g
既可以做结尾,也可以做声母,于是又对nosm
这个list进行一次判断,发现这类后,再往后判断一个字符,判断是否在声母表中
def sm(strs):
smlist = 'bpmfdtnlgkhjqxrzcsyw'
nosm = ['eR','aN','eN','iN','uN','vN','nG','NG']
rep = {'ZH':'Zh','CH':'Ch','SH':'Sh'}
for s in smlist:
strs = strs.replace(s,s.upper())
for s in nosm:
strs = strs.replace(s,s.lower())
for s in rep.keys():
strs = strs.replace(s,rep[s])
for s in nosm:
tmp_num = 0
isOk = False
while (tmp_num < len(strs)) and (isOk==False):
try:
tmp_num = strs.index(s.lower(),tmp_num)
except:
isOk = True
else:
tmp_num = tmp_num + len(s)
if strs[tmp_num:tmp_num+1].lower() not in smlist:
strs = strs[:tmp_num-1]+strs[tmp_num-1:tmp_num].upper()+strs[tmp_num:]
return strs
这时候已经可以提取声母了,剩下就简单了,碰到大写字母后就是一个拼音的开始,提取简拼就只找大写字母
拆分
def onep(strs):
restr = ''
strs = sm(strs)
for s in strs:
if 'A' <= s and s <= 'Z':
restr = restr + ' ' + s
else:
restr = restr + s
restr = restr[1:]
restr = restr.lower()
return restr.split(' ')
返回
['chen','gui','ying']
简拼提取
def simplep(strs):
restr = ''
strs = sm(strs)
for s in strs:
if 'A' <= s and s <= 'Z':
restr = restr + s
restr = restr.lower()
return restr
返回
cgy
然后就可以玩很多了
附一个通过拼音生成弱口令字典的脚本
#!/usr/bin/python
# Author : wkong
# Crack
def clearChar(chars):
reStr = ['\n','\r','\t',' ']
for reS in reStr:
chars = chars.replace(reS, '')
return chars
def sm(strs):
smlist = 'bpmfdtnlgkhjqxrzcsyw'
nosm = ['eR','aN','eN','iN','uN','vN','nG','NG']
rep = {'ZH':'Zh','CH':'Ch','SH':'Sh'}
for s in smlist:
strs = strs.replace(s,s.upper())
for s in nosm:
strs = strs.replace(s,s.lower())
for s in rep.keys():
strs = strs.replace(s,rep[s])
for s in nosm:
tmp_num = 0
isOk = False
while (tmp_num < len(strs)) and (isOk==False):
try:
tmp_num = strs.index(s.lower(),tmp_num)
except:
isOk = True
else:
tmp_num = tmp_num + len(s)
if strs[tmp_num:tmp_num+1].lower() not in smlist:
strs = strs[:tmp_num-1]+strs[tmp_num-1:tmp_num].upper()+strs[tmp_num:]
return strs
def simplep(strs):
restr = ''
strs = sm(strs)
for s in strs:
if 'A' <= s and s <= 'Z':
restr = restr + s
restr = restr.lower()
restr = restr.capitalize()
return restr
def rePass(name):
uList = []
uList.append(user.capitalize())
uList.append(sm(user))
uList.append(simplep(user))
pwdList = []
ce = ['!@#123','123!@#','@123','@1234','@12345','@123456','123','1234','12345','123456','123.','1234.','12345.','123456.','123123','abc','abc@123','qwer!@#','!@#qwer','qwe!@#','!@#qwe','!QAZ2wsx','1q2w3e']
for s in ce:
for u in uList:
pwdList.append(u+s)
return pwdList
def autoCrack(username, password):
print(username+':'+password)
if __name__ == '__main__':
userFile = 'zhangwei.txt'
pUserFile = open(userFile, 'r')
userList = pUserFile.readlines()
pUserFile.close()
for user in userList:
user = clearChar(user)
pwd = rePass(user)
for pw in pwd:
autoCrack(user, pw)