鱼C论坛

 找回密码
 立即注册
查看: 1864|回复: 0

[作品展示] 使用turtle统计词频

[复制链接]
发表于 2017-12-14 11:10:27 | 显示全部楼层 |阅读模式

马上注册,结交更多好友,享用更多功能^_^

您需要 登录 才可以下载或查看,没有账号?立即注册

x
输入一个文件,统计词频,绘制柱状图显示,由于中文歧义,目前只适合英文、数字文本



  1. import turtle

  2. ##全局变量##
  3. #词频排列显示个数
  4. count = 10
  5. #单词频率数组-作为y轴数据
  6. data = []
  7. #单词数组-作为x轴数据
  8. words = []
  9. #y轴显示放大倍数-可以根据词频数量进行调节
  10. yScale = 6
  11. #x轴显示放大倍数-可以根据count数量进行调节
  12. xScale = 30

  13. ################# Turtle Start  ####################  
  14. #从点(x1,y1)到(x2,y2)绘制线段
  15. def drawLine(t, x1, y1, x2, y2):
  16.     t.penup()
  17.     t.goto (x1, y1)
  18.     t.pendown()
  19.     t.goto (x2, y2)

  20. # 在坐标(x,y)处写文字
  21. def drawText(t, x, y, text):
  22.     t.penup()
  23.     t.goto (x, y)
  24.     t.pendown()
  25.     t.write(text)

  26. def drawGraph(t):
  27.     #绘制x/y轴线
  28.     drawLine (t, 0, 0, 360, 0)
  29.     drawLine (t, 0, 300, 0, 0)

  30.     #x轴: 坐标及描述
  31.     for x in range(count):
  32.         x=x+1 #向右移一位,为了不画在原点上
  33.         drawText(t, x*xScale-4, -20, (words[x-1]))
  34.         drawText(t, x*xScale-4, data[x-1]*yScale+10, data[x-1])
  35.     drawBar(t)

  36. #绘制一个柱体
  37. def drawRectangle(t, x, y):
  38.     x = x*xScale
  39.     y = y*yScale#放大倍数显示
  40.     drawLine(t, x-5, 0, x-5, y)
  41.     drawLine(t, x-5, y, x+5, y)
  42.     drawLine(t, x+5, y, x+5, 0)
  43.     drawLine(t, x+5, 0, x-5, 0)
  44.      
  45. #绘制多个柱体
  46. def drawBar(t):
  47.     for i in range(count):
  48.         drawRectangle(t, i+1, data[i])   
  49. ################# Turtle End  ####################

  50.          
  51. #对文本的每一行计算词频的函数
  52. def processLine(line, wordCounts):
  53.     #用空格替换标点符号
  54.     line = replacePunctuations(line)
  55.     #从每一行获取每个词
  56.     words = line.split()
  57.     for word in words:
  58.         if word in wordCounts:
  59.             wordCounts[word] += 1
  60.         else:
  61.             wordCounts[word] = 1

  62. #空格替换标点的函数
  63. def replacePunctuations(line):
  64.     for ch in line:
  65.         if ch in "~@#$%^&*()_-+=<>?/,.:;{}[]|\'""":
  66.             line = line.replace(ch, " ")
  67.     return line

  68. def main():
  69.     #用户输入一个文件名
  70.     filename = input("enter a filename:").strip()
  71.     infile = open(filename, "r")
  72.      
  73.     #建立用于计算词频的空字典
  74.     wordCounts = {}
  75.     for line in infile:
  76.         processLine(line.lower(), wordCounts)
  77.          
  78.     #从字典中获取数据对
  79.     pairs = list(wordCounts.items())

  80.     #列表中的数据对交换位置,数据对排序
  81.     items = [[x,y]for (y,x)in pairs]
  82.     items.sort()

  83.     #输出count个数词频结果
  84.     for i in range(len(items)-1, len(items)-count-1, -1):
  85.         print(items[i][1]+"\t"+str(items[i][0]))
  86.         data.append(items[i][0])
  87.         words.append(items[i][1])
  88.          
  89.     infile.close()
  90.      
  91.     #根据词频结果绘制柱状图
  92.     turtle.title('词频结果柱状图')
  93.     turtle.setup(900, 750, 0, 0)
  94.     t = turtle.Turtle()
  95.     t.hideturtle()
  96.     t.width(3)
  97.     drawGraph(t)
  98.          
  99. #调用main()函数
  100. if __name__ == '__main__':
  101.     main()
复制代码
QQ截图20171214110939.png
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复

使用道具 举报

您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

小黑屋|手机版|Archiver|鱼C工作室 ( 粤ICP备18085999号-1 | 粤公网安备 44051102000585号)

GMT+8, 2024-4-20 07:49

Powered by Discuz! X3.4

© 2001-2023 Discuz! Team.

快速回复 返回顶部 返回列表