• 回答数

    4

  • 浏览数

    243

夜月叶耶也
首页 > 英语培训 > 词频统计英文

4个回答 默认排序
  • 默认排序
  • 按时间排序

Mary瑶瑶

已采纳

感觉不是很难啊,1.split切句子,2.split切词,3.遍历生成词组,扔到dict里统计就可以了啊

词频统计英文

315 评论(12)

sisley0522

import refrom itertools import imap as mapfrom collections import Counterdef parserwords(sentence):    preword = ''    result = []    for word in re.findall('\w+', sentence.lower()):        if preword:            result.append((preword, word))        preword = word    return resultcontext = """Do you hear the people sing, singing a song of angry men. It is the music of a people, who will not be slaves again, when the beating of your heart echoes the beating of the drums. There is a life about to start when tomorrow comes."""words = []for sentence in map(parserwords,         re.split(r'[,.]', context.lower())):    words.extend(sentence)prefixcounter = Counter([word[0] for word in words])counter = Counter(words)meter = {}for pre, post in counter.iterkeys():    meter[(pre, post)] = 1. * counter[(pre, post)] / prefixcounter[pre]result = sorted(meter.iteritems(),    cmp = lambda a, b: cmp(b[1], a[1]) or cmp(a[0], b[0])    )print result[:5]

335 评论(9)

红月光薇儿

#include #include int main(void){ int a = 0, b = 0, c = 0; char buf[128]; FILE *fp; /* 打开文件,文件名必须大写 */ fp= fopen("DATA5610.TXT", "r"); if (!fp) { printf("No 'DATA5610.TXT' found.\n"); return -1; } /* 逐次读取单词,空格或回车分割 */ while (fscanf(fp, "%s", buf) > 0) { /* 如读取到的单词是 if,则a自增 1 */ if (strcmp(buf, "if") == 0) a++; else if (strcmp(buf, "while") == 0) b++; else if (strcmp(buf, "for") == 0) c++; } printf("if: %d, while: %d, for: %d\n", a, b, c); fclose(fp); return 0;}

310 评论(15)

卉峰呢喃

data="""Do you hear the people sing, singing a song of angry men. It is the music of a people, who will not be slaves again, when the beating of your heart echoes the beating of the drums. There is a life about to start when tomorrow comes."""data=data.replace(',','')data=data.replace('.','')ws=data.split()dic={}#count two wordsws2=[]#two wordsfor i in range(len(ws)-1): ws2.append(ws[i]+" "+ws[i+1])for w2 in ws2: if dic.get(w2)==None: dic[w2]=1 else: dic[w2]+=1dic_first={}#count two words by first wordfor w2 in ws2: (l,r)=w2.split() if dic_first.get(l)==None: dic_first[l]=1 else: dic_first[l]+=1for w2 in ws2:#output (l,r)=w2.split() print w2,dic[w2],dic_first[l],dic[w2]/float(dic_first[l])

291 评论(11)

相关问答