为了账号安全,请及时绑定邮箱和手机立即绑定

统计英文词频,运行代码出现 >>> 输入文章.txt 报错

统计英文词频,运行代码出现 >>> 输入文章.txt 报错

慕码人1445446 2017-09-13 20:50:57
import turtlecount=5data=[]words=[]yScale=6xScale=30#从点(x1,y1)到(x2,y2)绘制线段def drawLine(t,x1,y1,x2,y2):    t.penup()    t.goto(x1,y1)    t.pendown()    t.goto(x2,y2)#在坐标(x,y)处写文字def drawText(t,x,y,text):    t.penup()    t.goto(x,y)    t.pendown()    t.write(text)def drawGraph(t):    drawLine(t,0,0,360,0)    drawLine(t,0,300,0,0)    for x in range(count):        x+=1        drawText(t,x*xScale-4,-20,(words[x-1]))        drwaText(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-5,0,x-5,y)    drawLine(t,x-5,0,x+5,y)    drawLine(t,x+5,0,x+5,y)    drawLine(t,x+5,0,x-5,y)def drawBar(t):    for i in range(count):        drawRectangle(t,i+1,data[i])       def replacePunctuation(line):    for ch in line:        if ch in "~@#$%^&*((_+<>?:',.\[]{}|":            line=line.replace(ch,"")    return line    def processLine(line,wordCounts):    line=replacePunctuations(line)    words=line.split()    for word in words:        if word in wordCounts:            wordCount[word]+=1        else:            wordCounts[word]=1def 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()    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.setup(900,750,0,0)    t=turtle.Turtle()    t.hideturtle()    t.width(3)    drawGraph(t)    if __name__=='__main__':        main()             
查看完整描述

目前暂无任何回答

  • 0 回答
  • 0 关注
  • 1381 浏览
慕课专栏
更多

添加回答

举报

0/150
提交
取消
意见反馈 帮助中心 APP下载
官方微信