Created
March 4, 2016 20:00
-
-
Save cewing/7c52094c698ce8639e56 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# This submission is From Will Weatherford | |
from collections import Counter | |
import re | |
SEP = r'[\*]{10,999}' | |
def make_corpae(open_text_file): | |
text = open_text_file.read() | |
corpae = {corp: make_trigram_counts(corp) for corp in re.split(SEP, text)} | |
return corpae | |
def make_trigram_counts(corp): | |
trigrams = {} | |
words = corp.split() | |
for index, word in enumerate(words[2:]): | |
bigram = tuple(words[index -2: index]) | |
counter = trigrams.setdefault(bigram, Counter()) | |
counter[word] += 1 | |
return trigrams | |
def trigram_probability(trigram, corpus): | |
"""Return probability that given trigram occurs in corpus.""" | |
counter = corpus[trigram.keys()[0]] | |
total = sum([counter[ele] for ele in counter.elements()]) | |
return counter[trigram.items()[0]] / float(total) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment