· 9 years ago · Nov 28, 2016, 09:58 PM
1from collections import Counter
2from random import choice
3import re
4
5
6class Cup:
7 """ A class defining a cup that will hold the words that we will pull out """
8 def __init__(self):
9 self.next_word = Counter() # will keep track of how many times a word appears in a cup
10
11 def add_next_word(self,word):
12 """ Used to add words to the cup and keep track of how many times we see it """
13 self.next_word[word] += 1
14
15class NGram:
16
17 def make_cups(self,order,string):
18 """ Get a string of input text and make all the unique cups with the different words in it """
19 cups = dict()
20
21 words = re.findall(r"([\w]+)",string)
22 token = []
23 next_word = ''
24 for i in range(len(words)-order):
25 token = []
26 next_word = words[i+order]
27 for j in range(order):
28 token.append(words[i+j])
29 # create a cup if we've never seen this token before
30 if not cups.has_key(tuple(token)):
31 cups[tuple(token)] = Cup()
32 cups[tuple(token)].add_next_word(next_word)
33 else:
34 cups[tuple(token)].add_next_word(next_word)
35
36 return cups
37
38
39 def print_cups(self,cups):
40 """ You can use this to see what cups there are and the words inside of it """
41 for token in cups.keys():
42 print "key %s : nextword %s" %(token,cups[token].next_word)
43
44
45 def topword(self,n,cup):
46 """ A function that picks the top words in a cup. I realized I could've just randomly picked from a list of words
47 but instead a made a Counter() object of words to keep track of them
48 """
49 # reverse sort based on values
50 temp = list(reversed(sorted(cup.next_word,key=cup.next_word.get)))
51 if len(temp[:n]) > 0:
52 return choice(temp[:n]) # random element from the temp list
53 else:
54 return ""
55
56
57 def generate_from_file(self,order,filename):
58 """ Generate words based on a text file """
59 # put file in one big string
60 with open(filename,'r') as f:
61 string = f.read()
62
63 string = re.sub('[,\.?"-\'!:;]','',string)
64
65 cups = {}
66 cups = self.make_cups(order,string)
67
68
69 first_token = choice(cups.keys()) # random first token
70 this_token = first_token
71 generated_words = []
72 generated_words += list(this_token)
73 next_word = "" # first next word is blank
74
75
76 for i in range(10):
77 next_word = self.topword(1,cups[this_token])
78
79 temp = list(this_token)
80 temp.pop(0) # remove first word in token
81 temp.append(next_word) # add new word to the token
82 next_token = tuple(temp)
83
84 this_token = next_token
85
86 generated_words += [next_word]
87
88 print ' '.join(generated_words)
89
90 def generate_from_string(self,order,string,number_of_words):
91 """ Generate words based on a string """
92 string = re.sub('[,\.?"-\'!:;]','',string)
93 cups = {}
94 cups = self.make_cups(order,string)
95
96
97 first_token = choice(cups.keys()) # random first token
98 this_token = first_token
99 generated_words = []
100 generated_words += list(this_token)
101 next_word = "" # first next_word is blank
102
103
104 for i in range(1,number_of_words):
105 # if the name of the token exists
106 if cups.has_key(this_token):
107 next_word = self.topword(2,cups[this_token])
108 else:
109 this_token = choice(cups.keys()) # random token
110 next_word = self.topword(2,cups[this_token])
111
112 temp = list(this_token)
113 temp.pop(0) # remove first word in token
114 temp.append(next_word) # add new word to the end of the token
115 next_token = tuple(temp)
116
117 this_token = next_token
118
119 generated_words += [next_word]
120
121
122 print ' '.join(generated_words)
123
124ngram = NGram()
125
126# uncomment to use if you have a file you want to generate from
127#ngram.generate_from_file(2,'shakespeare.txt')
128strings = {}
129strings['sal'] = "Let's say we have the equation 7 times x is equal to 14. Now before even trying to solve this equation, what I want to do is think a little bit about what this actually means. 7x equals 14, this is the exact same thing as saying 7 times x - let me write it this way -- 7 times x -- we'll do the x in orange again -- 7 times x is equal to 14. Now you might be able to do this in your head. You could literally go through the 7 times table. You say well 7 times 1 is equal to 7, so that won't work. 7 times 2 is equal to 14, so 2 works here. So you would immediately be able to solve it. You would immediately, just by trying different numbers out, say hey, that's going to be a 2. But what we're going to do in this video is to think about how to solve this systematically. Because what we're going to find is as these equations get more and more complicated, you're not going to be able to just think about it and do it in your head. So it's really important that one, you understand how to manipulate these equations, but even more important to understand what they actually represent. This literally just says 7 times x is equal to 14. In algebra we don't write the times there. When you write two numbers next to each other or a number next"
130strings['sherlock'] = "It was in the spring of the year 1894 that all London was interested, and the fashionable world dismayed, by the murder of the Honourable Ronald Adair under most unusual and inexplicable circumstances. The public has already learned those particulars of the crime which came out in the police investigation; but a good deal was suppressed upon that occasion, since the case for the prosecution was so overwhelmingly strong that it was not necessary to bring forward all the facts. Only now, at the end of nearly ten years, am I allowed to supply those missing links which make up the whole of that remarkable chain. The crime was of interest in itself, but that interest was as nothing to me compared to the inconceivable sequel, which afforded me the greatest shock and surprise of any event in my adventurous life. Even now, after this long interval, I find myself thrilling as I think of it, and feeling once more that sudden flood of joy, amazement, and incredulity which utterly submerged my mind. Let me say to that public which has shown some interest in those glimpses which I have occasionally given them of the thoughts and actions of a very remarkable man that they are not to blame me if I have not shared my knowledge with them, for I should have considered it my first duty to have done so had I not been barred by a positive prohibition from his own lips, which was only withdrawn upon the third of last month."
131strings['hamlet'] = "To be, or not to be: that is the question: whether 'tis nobler in the mind to suffer the slings and arrows of outrageous fortune, or to take arms against a sea of troubles, and by opposing end them? To die: to sleep; no more; and, by a sleep to say we end the heart-ache and the thousand natural shocks that flesh is heir to, 'tis a consummation devoutly to be wish'd. To die, to sleep; to sleep: perchance to dream: ay, there's the rub; for in that sleep of death what dreams may come when we have shuffled off this mortal coil, must give us pause. There's the respect that makes calamity of so long a life; for who would bear the whips and scorns of time, the oppressor's wrong, the proud man's contumely, the pangs of dispriz'd love, the law's delay, the insolence of office, and the spurns that patient merit of the unworthy takes, when he himself might his quietus make with a bare bodkin? Who would fardels bear, to grunt and sweat under a weary life, but that the dread of something after"
132strings['winnie'] = "Once upon a time, a very long time ago now, about last Friday, Winnie-the-Pooh lived in a forest all by himself under the name of Sanders. ('What does 'under the name' mean?' asked Christopher Robin. 'It means he had the name over the door in gold letters, and lived under it.' 'Winnie-the-Pooh wasn't quite sure,' said Christopher Robin. 'Now I am,' said a growly voice. 'Then I will go on,' said I.) One day when he was out walking, he came to an open place in the middle of the forest, and in the middle of this place was a large oak-tree, and, from the top of the tree, there came a loud buzzing-noise. Winnie-the-Pooh sat down at the foot of the tree, put his head between his paws and began to think. First of all he said to himself: 'That buzzing-noise means something. You don't get a buzzing-noise like that, just buzzing and buzzing, without its meaning something. If there's a buzzing-noise, somebody's making a buzzing-noise, and the only reason for making a buzzing-noise that I know of is because you're a bee.' Then he thought another long time, and said: 'And the only reason for being a bee that I know of is making honey.' And then he got up, and said: 'And the only reason for making honey is so as I can eat it.' So he began to climb the tree"
133
134print "\nSal: "
135ngram.generate_from_string(order=2,string=strings['sal'],number_of_words=100)
136print "\nSherlock: "
137ngram.generate_from_string(order=2,string=strings['sherlock'],number_of_words=100)
138print "\nHamlet: "
139ngram.generate_from_string(order=2,string=strings['hamlet'],number_of_words=100)
140print "\nWinnie the Pooh: "
141ngram.generate_from_string(order=2,string=strings['winnie'],number_of_words=100)