源 /
segmentfault
文 /
离岛
整理
/
顶级程序员
故事的起因是上周六看《中国好声音》,一个周杰伦战队的学员用人工智能写的歌词,于是乎,我也有了这个想法,代码的主题思路是看Crossin先生的文章,虽然最后不能写出一首歌,但是押韵脚这事情分分钟搞定了
主题的思路,就是先抓取很多首歌曲的歌词,利用jieba分词后,将分好的词按照押韵表进行分类,最后匹配查询就可以了
准备一:押韵表
这个地方可以去网上搜押韵表
(温馨提示:代码可左右滑动)
import requests
import jieba
import re
from xpinyin import Pinyin
p = Pinyin()
RhymeIndex = [('1', ['a', 'ia', 'ua']), ('2', ['ai', 'uai']), ('3', ['an', 'ian', 'uan']),
('4', ['ang', 'iang', 'uang']), ('5', ['ao', 'iao']), ('6', ['e', 'o', 'uo']), ('7', ['ei', 'ui']),
('8', ['en', 'in', 'un']), ('9', ['eng', 'ing', 'ong', 'iong']), ('10', ['er']), ('11', ['i']),
('12', ['ie', 'ye']), ('13', ['ou', 'iu']), ('14', ['u']), ('16', ['ue']), ('15', ['qu', 'xu', 'yu'])]
RhymeDct = {'ui': '7', 'uan': '3', 'ian': '3', 'iu': '13', 'en': '8', 'ue': '16', 'ing': '9', 'a': '1', 'ei': '7',
'eng': '9', 'uo': '6', 'ye': '12', 'in': '8', 'ou': '13', 'ao': '5', 'uang': '4', 'ong': '9', 'ang': '4',
'ai'
: '2', 'ua': '1', 'uai': '2', 'an': '3', 'iao': '5', 'ia': '1', 'ie': '12', 'iong': '9', 'i': '11',
'er': '10', 'e': '6', 'u': '14', 'un': '8', 'iang': '4', 'o': '6', 'qu': '15', 'xu': '15', 'yu': '15'}
准备二:分词对应押韵表编码
分好的词与押韵表对应起来,举个栗子,比如“没有”对应的是“7-13”,就等于你给每个词都贴了一个标签,这样你以后想搜索的时候,就可以根据标签找到这些词了
def _analysis_words(words):
word_py =p.get_pinyin((u'{}'.format(words)))
lst_words = word_py.split('-')
r = []
for i in lst_words:
while True:
if not i:
break
token = RhymeDct.get(i, None)
if token:
r.append(token)
break
i = i[1:]
if len(r) == len(words):
return '-'.join(r)
#print( analysis words('兄弟'))
第一步:爬虫抓取歌词信息
这个地方数据爬取的越多,肯定你的词库就越壮大,后面分词也越高,我这里只爬取了3首歌曲的歌词,并且最后是存储到txt中,当然,放数据库里就更好了
def GetKeyword():
tracks=["431795900",'33850315','430053482']
with open('keyword.txt','a') as f:
f.write("[")
for i in tracks:
print(111)
lrcurl = "http://music.163.com/api/song/lyric?os=pc&id="+str(i)+"&lv=-1&kv=-1&tv=-1"
lrcreq = requests.get(lrcurl)
dt = lrcreq.json()
lrc=re.sub(u"\[.*?]", "", dt['lrc']['lyric'])
seg_list = list(jieba.cut(lrc, cut_all=True))
for i in seg_list:
if len(i)==2:
if _analysis_words(i)!=None:
f.write("{'"+_analysis_words(i)+"':'"+i+"'},")
f.write("]")
f.close()
第二步:调用分词的方法
GetKeyword()
第三步:分析分词后的txt
def Findkey(str):
result={}
with open('keyword.txt', 'r') as f:
list=eval(f.readlines()[0])
for item in