python学习笔记2词频统计

对英文文本中的英文单词进行词频统计:

代码如下:

# -*- coding: utf-8 -*-

"""
Created on Thu Apr  5 20:07:09 2018


@author: Administrator
"""
import turtle
count=5
data=[]
words=[]
yScale=10
xScale=30
#点连成线
def drawLine(t,x1,y1,x2,y2):
    t.penup()
    t.goto(x1,y1)
    t.pendown()
    t.goto(x2,y2)
#写入文字    
def drawText(t,x,y,text):
    t.penup()
    t.goto(x,y)
    t.pendown()
    t.write(text)
#绘图 
def drawGraph(t):
    drawLine(t,0,0,400,0)
    drawLine(t,0,400,0,0)
    for x in range(count):
        x=x+1
        drawText(t,x*xScale-4,-20,(words[x-1]))
        drawText(t,x*xScale-4,data[x-1]*yScale+10,data[x-1])
    drawBar(t)
#绘制直方图       
def drawRectangle(t,x,y):
    x=x*xScale
    y=y*yScale
    drawLine(t,x-10,0,x-10,y)
    drawLine(t,x-10,y,x+10,y)
    drawLine(t,x+10,y,x+10,0)
    drawLine(t,x+10,0,x-10,0)
#绘制多个直方图  
def drawBar(t):
    for i in range(count):
        drawRectangle(t,i+1,data[i])
#处理文本
def processLine(line,wordCounts):
   
    line=replacePunctuations(line)
    #从每一行获取每个词
    words=line.split()
    for word in words:
        if word in wordCounts:
            wordCounts[word] +=1
        else:
            wordCounts[word] =1
            
 #用空格替换标点符号           
def replacePunctuations(line):
    for ch in line:
        if ch in "/~@#$%^&*()_-+=<>?,.:;{}[]|\'""":
            line =line.replace(ch,"")
    return line


def main():
    #用户输入一个文件名
    filename=input("enter a filename:").strip()
    infile=open(filename,"r")
    #建立用于计算词频的空字典
    wordCounts={}
    for line in infile:
        processLine(line.lower(),wordCounts)
    #从字典中获取数据对
    pairs=list(wordCounts.items())
    #列表中的数据对交换位置,数据对排序
    items=[[x,y]for (y,x) in pairs]
    items.sort()
    #输出count个数词频结果
    for i in range(len(items)-1,len(items)-count-1,-1):
        print(items[i][1]+"\t"+str(items[i][0]))
        data.append(items[i][0])
        words.append(items[i][1])
    #根据词频结果绘制柱状图
    turtle.title('词频结果柱状图')
    turtle.setup(900,750,0,0)
    t=turtle.Turtle()
    t.hideturtle()
    t.width(3)
    drawGraph(t)
#调用main()函数
if __name__=='__main__':

    main()

输出显示:

扫描二维码关注公众号,回复: 4808512 查看本文章

猜你喜欢

转载自blog.csdn.net/lyc44813418/article/details/79878738