1. 程式人生 > >python學習筆記2詞頻統計

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 "/

[email protected]#$%^&*()_-+=<>?,.:;{}[]|\'""":
            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()

輸出顯示: