[python] python jieba使用案例

import jieba 

print ('starting!!')
text1='中华人民共和国是一个伟大国家'
jieba.lcut(text1,cut_all=True)

## test txt vocabfenbu-- isok
'''
txt = open("F:/python/training/BFS_DFS_20200902/庆余年全文.txt", "r", encoding='utf-8').read()
words = jieba.lcut(txt)     # 使用精确模式对文本进行分词
counts = {}     # 通过键值对的形式存储词语及其出现的次数
for word in words:
    if  len(word) == 1:    # 单个词语不计算在内
        continue
    else:
        counts[word] = counts.get(word, 0) + 1    # 遍历所有词语,每出现一次其对应的值加 1
        
items = list(counts.items())#将键值对转换成列表
items.sort(key=lambda x: x[1], reverse=True)    # 根据词语出现的次数进行从大到小排序

for i in range(150,230):
    word, count = items[i]
    print("{0:<5}{1:>5}".format(word, count))

'''

## analyze eng
def get_text():
    txt = open("F:/python/training/BFS_DFS_20200902/lsof.txt", "r", encoding='UTF-8').read()
    txt = txt.lower()
    for ch in '!"#$%&()*+,-./:;<=>?@[\\]^_‘{|}~':
        txt = txt.replace(ch, " ")      # 将文本中特殊字符替换为空格
    return txt

file_txt = get_text()
words = file_txt.split()    # 对字符串进行分割,获得单词列表
counts = {}

for word in words:
    if len(word) == 1:
        continue
    else:
        counts[word] = counts.get(word, 0) + 1 

items = list(counts.items())    
items.sort(key=lambda x: x[1], reverse=True)      

for i in range(25):
    word, count = items[i]
    print("{0:<5}->{1:>5}".format(word, count))

 

posted @ 2020-09-04 15:37  allen514519  阅读(375)  评论(0)    收藏  举报