217day(jieba库和文本词频统计)

《2018年5月16日》【连续217天】

标题:jieba库和文本词频统计;

内容:
A.jieba库:一个强大的中文分词的第三方库:

包括精确模式,全模式,搜索引擎模式;

1.jieba.lcut(s)

2.jieba.lcut(s,cut_all=True)

3jieba.lcut_for_search(s)

 

B.文本词频统计:
英文版,以Hamlet中的 单词出现次数为例:
 

#CalHamletV1.py
def getText():
    txt =open("hamlet.txt","r").read()
    txt = txt.lower()
    for ch in "!@#$%^&*()_+-;'./`~?\|{}[],.<>":
        txt = txt.replace(ch," ")
    return txt
hamletTxt =getText()
words = hamletTxt.split()
counts ={}
for word in words:
    counts[word] =counts.get(word,0)+1
items =list(counts.items())
items.sort(key=lambda x:x[1],reverse=True)
for i in range(10):
    word,count=items[i]
    print("{0:<10}{1:>5}".format(word,count))
    

结果:
the        1143
and         966
to          762
of          668
i           630
a           546
you         540
my          514
hamlet      466

in          450

以三国演义中名字的出场频率为例:
 

#CalThreeKingdomsV2.py
import jieba
txt =open("threekingdoms.txt","r",encoding="utf-8").read()
excludes ={"将军","却说","荆州","二人","不可","不能","如此","如何","商议"\
           ,"军士","军马","左右","引兵","次日","主公","大喜","天下","东吴"\
           ,"于是","今日","不敢","魏兵","陛下","一人","人马","不知"}
words =jieba.lcut(txt)
counts ={}
for word in words:
    if len(word) ==1:
        continue
    elif word =="诸葛亮" or word =="孔明曰":
        rword ="孔明"
    elif word =="关公" or word =="云长":
        rword ="关羽"
    elif word =="玄德" or word =="玄德曰":
        rword ="刘备"
    elif word =="孟德" or word =="丞相":
        rword ="曹操"
    elif word =="都督":
        rword ="周瑜"
    else:
        rword =word
    counts[rword] =counts.get(rword,0)+1
for word in excludes:
    del counts[word]
items =list(counts.items())
items.sort(key=lambda x:x[1], reverse=True)
for i in range(10):
    word,count =items[i]
    print("{0:<10}{1:>5}".format(word,count))

结果:

Building prefix dict from the default dictionary ...
Loading model from cache C:\Users\WIN10\AppData\Local\Temp\jieba.cache
Loading model cost 1.561 seconds.
Prefix dict has been built succesfully.
曹操         1451
孔明         1383
刘备         1252
关羽          784
周瑜          438
张飞          358
吕布          300
赵云          278
孙权          264
司马懿         221

### 回答1: 好的,我会用中文回复。要统计hamlet.txt文件中出现的英文单词情况,可以先读取文件,然后使用正则表达式或者其他方法提取出所有的英文单词。接着,可以使用Python中的字典来统计每个单词出现的次数,并按照出现次数从大到小排序。最后,输出出现最多的前n个单词即可。需要注意的是,统计单词时要考虑大小写,可以将所有单词转换为小写再进行统计。 ### 回答2: 要回答这个问题,我们需要借助编程语言来统计hamlet.txt文件中出现的英文单词情况。以下是一个Python代码示例: ``` import re from collections import Counter # 读取文件内容 with open('hamlet.txt', 'r') as f: text = f.read() # 去除标点符号和换行符 text = re.sub(r'[^\w\s]', '', text) text = re.sub(r'\n', ' ', text) # 将所有单词转为小写并按照空格分割 words = text.lower().split() # 统计单词出现次数 word_counts = Counter(words) # 输出出现最多的前n个单词 n = 10 top_n_words = word_counts.most_common(n) for word, count in top_n_words: print(word, count) ``` 上述代码首先读取hamlet.txt文件的内容,并使用正则表达式去除标点符号和换行符,接着将所有单词转换为小写并按照空格分割,最后使用collections模块中的Counter类来统计每个单词出现的次数,并输出出现最多的前n个单词。 需要注意的是,由于hamlet.txt文件中可能存在一些非英文单词,比如人名、地名等,因此统计出的单词数可能会略微偏高。此外,许多单词可能有不同的形式,如复数形式、时态形式等,但在本文中我们将它们视为不同的单词来统计。 ### 回答3: 要解决这个问题,我们需要进行以下步骤: 1. 读取hamlet.txt文件的内容,并将其转换为小写字母。这是因为我们不希望区分大小写。 2. 使用正则表达式将文本中的标点符号和数字删除。这将使我们只剩下英文单词。 3. 将文本分割成单词,并计算每个单词出现的次数。 4. 对出现次数进行排序,并输出前n个单词。 下面是使用Python实现上述步骤的代码: ``` import re # 步骤1:读取文件并转换为小写字母 with open('hamlet.txt', 'r') as f: text = f.read().lower() # 步骤2:删除标点和数字 text = re.sub('[^a-zA-Z]', ' ', text) # 步骤3:分割单词,并计算出现次数 words = text.split() word_freq = {} for word in words: if word not in word_freq: word_freq[word] = 0 word_freq[word] += 1 # 步骤4:对出现次数进行排序 sorted_words = sorted(word_freq.items(), key=lambda x: x[1], reverse=True) # 输出前n个单词 n = 10 for i in range(n): print(sorted_words[i][0], sorted_words[i][1]) ``` 这段代码将输出出现最多的前10个单词及其出现次数。根据需要,可以更改n的值来输出更多或更少的单词。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值