def MergedFile(self):
aaa=0
for i in glob.glob("*歌曲名*"):
file_object = open(i,'r',encoding='UTF-8')
list_of_line = file_object.readlines()
for p in list_of_line:
if "作词" in p or "作曲" in p or "混音助理" in p or "混音师" in p or "录音师" in p or "执行制作" in p or "编曲" in p or "制作人" in p or "录音工程" in p or "录音室" in p or "混音录音室" in p or "混音工程" in p or "Programmer" in p or p == "\n" or "和声" in p or "吉他" in p or "录音助理" in p or "陈任佑鼓" in p or "周杰伦" in p:
aaa+=1
print(p)
else:
with open ("allLyric"+".txt","a",encoding='UTF-8') as f :
f.write(p)
f.write("\n")
print(aaa)
#合并歌曲
file1 = open('allLyric.txt', 'r', encoding='utf-8') # 要去掉空行的文件
file2 = open('allLyric1.txt', 'w', encoding='utf-8') # 生成没有空行的文件
try:
for line in file1.readlines():
if line == '\n':
line = line.strip("\n")
file2.write(line)
finally:
file1.close()
file2.close()
print("合并歌词文件完成")
上面需要注意的是:我们合并数据的时候,可以选择性的删除一些无用数据
结果如下
下面我们对周杰伦歌曲进行情绪化分析
def EmotionAnalysis(self):
from snownlp import SnowNLP
from pyecharts import Bar
xzhou=[]
yzhou=[]
for i in glob.glob("*歌曲名*"):
count=0
allsen=0
with open(i,'r', encoding='utf-8') as fileHandel:
fileList = fileHandel.readlines()
for p in fileList:
if "作词" in p or "作曲" in p or "鼓" in p or "混音师" in p or "录音师" in p or "执行制作" in p or "编曲" in p or "制作人" in p or "录音工程" in p or "录音室" in p or "混音录音室" in p or "混音工程" in p or "Programmer" in p or p == "\n":
pass
else:
s = SnowNLP(p)
# print(s.sentences[0])
s1 = SnowNLP(s.sentences[0])
#print(type(s1))
count+=1
allsen+=s1.sentiments
i=str(i)
xzhou1 = i.split("-", 1)[1].split(".",1)[0]
xzhou.append(xzhou1)
avg=int(allsen)/count
yzhou.append(avg)
#print("%s这首歌的情绪为%s"%(i,avg))
fileHandel.close()
bar = Bar("柱状图数据堆叠示例")
bar.add("周杰伦歌曲情绪可视化", xzhou, yzhou, is_stack=True,xaxis_interval=0)
bar.render(r"D:\学习\untitled4\allpicture\周杰伦歌曲情绪全部.html")
#显示最好的前五首歌
import heapq
yzhou1 = heapq.nlargest(10, yzhou)
temp = map(yzhou.index, heapq.nlargest(10, yzhou))
temp = list(temp)
xzhou1 = []
for i in temp:
xzhou1.append(xzhou[i])
# 情绪前十首歌个图
bar = Bar("周杰伦歌曲情绪较好前十首歌")
bar.add("周杰伦歌曲情绪可视化", xzhou1, yzhou1, is_stack=True)
bar.render(r"D:\学习\untitled4\allpicture\周杰伦歌曲最积极情绪top10.html")
#显示最差的十首歌
yzhou1 = heapq.nsmallest(10, yzhou)
temp = map(yzhou.index, heapq.nsmallest(10, yzhou))
temp = list(temp)
xzhou1 = []
for i in temp:
xzhou1.append(xzhou[i])
# print(xzhou1)
#print(yzhou1)
# 情绪前十首歌个图
bar = Bar("周杰伦歌曲情绪较差前十首歌")
bar.add("周杰伦歌曲情绪可视化",xzhou1, yzhou1,xaxis_interval=0,xzhou1_label_textsize=6)
bar.render(r"D:\学习\untitled4\allpicture\周杰伦歌曲最消极情绪top10.html")
print(xzhou1)
下面完成数据词频各种分析
#定义结巴分词的方法以及处理过程
def splitSentence(self,inputFile, outputFile):
fin = open(inputFile, 'r', encoding='utf-8')
fout = open(outputFile, 'w', encoding='utf-8')
for line in fin:
line = line.strip()
line = jieba.analyse.extract_tags(line)
outstr = " ".join(line)
fout.write(outstr + '\n')
fin.close()
fout.close()
#下面的程序完成分析前十的数据出现的次数
f = open("分词过滤后.txt", 'r', encoding='utf-8')
a = f.read().split()
b = sorted([(x, a.count(x)) for x in set(a)], key=lambda x: x[1], reverse=True)
print(sorted([(x, a.count(x)) for x in set(a)], key=lambda x: x[1], reverse=True))
#输出频率最多的前十个字,里面调用splitSentence完成频率出现最多的前十个词的分析
def LyricAnalysis(self):
import jieba
file = 'allLyric1.txt'
#这个技巧需要注意
alllyric = str([line.strip() for line in open('allLyric1.txt',encoding="utf-8").readlines()])
#获取全部歌词,在一行里面
alllyric1=alllyric.replace("'","").replace(" ","").replace("?","").replace(",","").replace('"','').replace("?","").replace(".","").replace("!","").replace(":","")
# print(alllyric1)
#在这里用结巴分词来分词过滤并且输出到一个文件里面,这个ting.txt
#import jieba.analyse 这里必须引入
jieba.analyse.set_stop_words("ting.txt")
self.splitSentence('allLyric1.txt', '分词过滤后.txt')
#下面是词频统计
import collections
# 读取文本文件,把所有的汉字拆成一个list
f = open("分词过滤后.txt", 'r', encoding='utf8') # 打开文件,并读取要处理的大段文字
txt1 = f.read()
txt1 = txt1.replace('\n', '') # 删掉换行符
txt1 = txt1.replace(' ', '') # 删掉换行符
txt1 = txt1.replace('.', '') # 删掉逗号
txt1 = txt1.replace('.', '') # 删掉句号
txt1 = txt1.replace('o', '') # 删掉句号
mylist = list(txt1)
mycount = collections.Counter(mylist)
for key, val in mycount.most_common(10): # 有序(返回前10个)
print(key, val)