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]
红月光薇儿
#include
卉峰呢喃
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])