(3)
在划线处填入合适代码,完善程序。
import pandas as pd
def cleantext( ):
txt =
open("textbook.txt","r").
read( )
txt=① #将字符串中所有大写字母转为小写
for ch in '! ( );:''',.? ' :
txt =
txt.replace(ch,"") #用空格替代ch的值
return txt
booktxt = cleantext( )
words = booktxt. split( )
#以空格为分隔符分割文本并生成列表
counts= { }
for word in words:
counts[word]=counts. get( word,0)+1
items= -list(counts. items( )) #返回所有键值对信息,生成列表
df= pd.DataFrame(items,columns=['word','times'])
df1= df.sort _values('word')
df1.plot( x='word', y='times',
kind='line', igsize=(8,3))
df2=②
print('文件中出现的不同单词数:',③)
print(df2[:10])
①________ ②________ ③________
本文介绍了如何使用Python进行文本预处理,包括将所有大写字母转换为小写,移除特殊字符,然后通过Pandas处理文本数据。程序展示了如何计算不同单词的频率,并通过matplotlib生成词频直方图。最后,报告了文件中不同单词的数量和前10个高频词。

被折叠的 条评论
为什么被折叠?



