首页 > 其他分享 >jieba 分词

jieba 分词

时间:2023-12-28 21:23:37浏览次数:25  
标签:winsB jieba winsA word probB probA 分词 def

jieba分词:

import jieba

with open("C:\\Users\\86133\\Desktop\\liaozhai.txt", "r", encoding='utf_8') as f:
    words = jieba.lcut(f.read())
counts = {}
for word in words:
    if len(word) == 1:
        continue
    elif word == "举孝廉" or word == "王公大人":
        rword = "刘孝廉"
    elif word == "厉鬼" or word == "鬼":
        rword = "群鬼"
    else:
        rword = word
    counts[rword] = counts.get(rword, 0) + 1
items = list(counts.items())
items.sort(key=lambda x: x[1], reverse=True)

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

 模拟体育竞技分析:

#羽毛球比赛规则(学号47)
import random
def printIntro():
    print("这个程序模拟量个选手A和B的某种竞技比赛")
    print("程序运行需要A和B的能力值(以0到1之间的小数表示)")
 
def getInputs():
    a = eval(input("请输入选手A的能力值(0-1): "))
    b = eval(input("请输入选手B的能力值(0-1): "))
    n = eval(input("模拟比赛的场次: "))
    return a, b, n
 
def printSummary(winsA, winsB):
    n = winsA + winsB
    print("竞技分析开始, 共模拟{}场比赛".format(n))
    print("选手A获胜{}场比赛, 占比{:0.1%}".format(winsA, winsA/n))
    print("选手B获胜{}场比赛, 占比{:0.1%}".format(winsB, winsB/n))
 
def gameOver(a, b):
    return a == 20 or b == 20
    
def simOneGame(probA, probB):
    scoreA, scoreB = 0, 0
    serving = "A"
    while not gameOver(scoreA, scoreB):
        if serving == "A":
            if random.random() < probA:
                scoreA += 1
            else:
                serving = "B"
        else:
            if random.random() < probB:
                scoreB += 1
            else:
                serving = "A"
    return scoreA, scoreB
 
def simNGames(n ,probA, probB):
    winsA, winsB = 0, 0
    for i in range(n):
        scoreA, scoreB = simOneGame(probA, probB)
        if scoreA > scoreB:
            winsA += 1
        else:
            winsB += 1
    return winsA, winsB
 
def main():
    printIntro()
    probA, probB, n = getInputs()
    winsA, winsB = simNGames(n, probA, probB)
    printSummary(winsA, winsB)
main()

 

标签:winsB,jieba,winsA,word,probB,probA,分词,def
From: https://www.cnblogs.com/LXxx007/p/17933595.html

相关文章

  • 聊斋jieba库
    importjiebaprint("0217向悦")#读取文本文件path="聊斋志异.txt"file=open(path,"r",encoding="utf-8")text=file.read()file.close()#使用jieba分词words=jieba.lcut(text)#统计词频counts={}forwordinwords:#过滤掉长度为1的词语......
  • 红楼梦jieba分词
    importjiebaexcludes={"什么","一个","我们","那里","你们","如今","说道","知道","起来","姑娘","这里","出来","他们","众人","自己",&quo......
  • jieba分词 红楼梦相关分词
    importjiebatext=open('C:\Users\李文卓\Desktop\xn\红楼梦.txt',"r",encoding='utf-8').read()words=jieba.lcut(text)counts={}forwordinwords:iflen(word)==1:#排除带个字符的分词效果continueelse:counts[word]=counts.get(word,0)+1it......
  • jieba分词
    ```importjieba#读取文本文件path="西游记.txt"file=open(path,"r",encoding="utf-8")text=file.read()file.close()#使用jieba分词words=jieba.lcut(text)#统计词频counts={}forwordinwords:#过滤掉长度为1的词语iflen(word......
  • 西游记jieba分词
    引用jiaba库点击查看代码importjieba读取文件,文件路径填写文件放取的位置并且使用jieba分词的精确模式点击查看代码txt=open('西游记.txt','r',encoding='utf-8').read()words=jieba.lcut(txt)count={}#通过键值对的形式存储词语及其出现的次数将同一人......
  • jieba西游记
    importjiebawithopen('E:\西游记.txt','r',encoding='utf-8')asf:#打开文件txt=f.read()#读取为txtwords=jieba.lcut(txt)#利用jieba库的lcut分词counts={}#创建字典forwordinwords:#逐个遍历iflen(word)==1:#对于......
  • jieba分词 | 西游记相关分词,出现次数最高的20个。
    代码1importjieba23txt=open("《西游记》.txt","r",encoding='utf-8').read()45words=jieba.lcut(txt)#使用精确模式对文本进行分词67counts={}#通过键值对的形式存储词语及其出现的次数89forwordinwords:10iflen(word)==......
  • jieba 分词
    尾号为7,8,9,0的同学做,聊斋相关的分词,出现次数最高的20个。#-*-coding:utf-8-*-"""CreatedonSatDec2318:00:492023@author:86135"""importjieba#读取文本文件path="C:\\Users\\86135\\Desktop\\聊斋.txt"file=open(path,&q......
  • jieba分词
    importjiebatxt=open("D:\python-learn\lianxi\聊斋志异.txt","r",encoding='utf-8').read()words=jieba.lcut(txt)counts={}forwordinwords:iflen(word)==1:continueelse:counts[word]=count......
  • jieba分词
    importjieba#读取文本文件path="红楼梦.txt"file=open(path,"r",encoding="GB2312",errors="ignore")text=file.read()file.close()#使用jieba分词words=jieba.lcut(text)#统计词频counts={}forwordinwords:#过滤掉长度为1的词语iflen......