python 讀取txt中的英文內容 分析詞頻 可視化顯示

python 讀取txt中的英文內容 分析詞頻 可視化顯示

調用turtle庫 平臺:Spyder

import turtle

##全局變量##
pi=3.14159
#詞頻排列顯示個數
count = 10
#單詞頻率數組-作爲y軸數據
data = []
#單詞數組-作爲x軸數據
words = []

import random
#--------------------------------------讀取 字符統計函數
#filename:文件名
#data:出現次數從高到低排序
#words:名字從高到低排序
def read(filename,data,words):
    txt1= open(filename,"r")#打開文件操作
    ##數據預處理   進行統計,統計後的數據裝在字典word_spss中
    word_spss=process_read(txt1)
    pairs=list(word_spss.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])
#--------------------------------------隨機顏色
#生成一個隨機的碼
def randomcolor():
    colorArr = ['1','2','3','4','5','6','7','8','9','A','B','C','D','E','F']
    color = ""
    for i in range(6):
        color += colorArr[random.randint(0,14)]
    return "#"+color
#--------------------------------------將英文標點替換標點爲空格
#輸入line
#輸出line
def replaceMark(line):
    for ch in line:
        if ch in "~@#$%^&*()_-+=<>?/,.:;{}[]|\'\"":
               line=line.replace(ch," ")
    return line
#--------------------------------------數據預處理
#輸入txt1
#輸出處理後的數據的頻率
def process_read(txt1):
    word_spss={}
    for line in txt1:
        line=line.lower()#換成小寫
        line=replaceMark(line)#去除符號
        words=line.split()#該函數利用split來區分空格 從而達到提取數組的效果 
        for word in words:
            if word in word_spss:
                word_spss[word]+=1
            else:
                word_spss[word]=1
    return word_spss
#--------------------------------------畫圖函數
def DIY_draw(data,words):
    turtle.title("詞頻結果統計圖")
    turtle.setup(1200, 500, 0, 0)
    t=turtle.Turtle()
    
    sum=0
    for ii  in range(9):
       sum+=data[ii]
    
    #輸出結果
    print(sum)
    #開始畫圖
    x0=150
    coefficient=2*x0/sum*1.2;#係數確認
    t0=-1
    pp=[]
    for i in range(9):
        pp.append(words[i]+"["+str(data[i])+"]")
        
    print(pp[1])
    for i in range(9):
        
        t.color(randomcolor())
        t.penup()
        t.goto(x0,-data[i]*coefficient)
        #t.write(data[i], False, align="center", font=("Arial", 18, "normal"))
        x0=x0-data[i]*coefficient-data[i+1]*coefficient
        t.pendown()
        t.begin_fill()
        t.circle(data[i]*coefficient)
        t.end_fill()
        t0=t0*(-1)
        
    x0=150
    for i in range(9):
        
        t.color(randomcolor())
        t.penup()
        t.goto(x0,-data[i]*coefficient-t0*data[i]*coefficient*1.2)
        t.write(pp[i], False, align="center", font=("Arial", 18, "normal"))
        x0=x0-data[i]*coefficient-data[i+1]*coefficient
        t.pendown()
        t0=t0*(-1)
    t.down()
#--------------------------------------讀取函數        
def main():
    #讀取用戶輸入的數據
    read("C:\\Users\\yiqing\\Desktop\\ex1.txt",data,words) #調用read()函數
    DIY_draw(data,words)
main()

結果

在這裏插入圖片描述

txt

在這裏插入圖片描述

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章