Skip to content

Commit 5cf3d78

Browse files
committed
Commit for trigrams program.
1 parent 42b4d7b commit 5cf3d78

File tree

1 file changed

+52
-0
lines changed

1 file changed

+52
-0
lines changed
Lines changed: 52 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,52 @@
1+
# -*- coding: utf-8 -*-
2+
"""
3+
Created on Thu Dec 19 17:26:51 2019
4+
5+
@author: bclas
6+
"""
7+
8+
import sys, random
9+
10+
###opens file from input
11+
import random
12+
13+
def open_text_file():
14+
choice = input("Enter a file to read")
15+
with open(choice, "r") as f:
16+
opened_file = f.read()
17+
f.close()
18+
return opened_file
19+
20+
def output_split(f):
21+
return f.split()
22+
23+
def make_trigrams(words):
24+
trigrams = {}
25+
for i in range(len(words)-2):
26+
pair = tuple(words[i:i+2])
27+
follow = words[i+2]
28+
29+
clean_pair = ("".join([i for i in pair[0] if i.isalpha()]), "".join([i for i in pair[1] if i.isalpha()]))
30+
clean_follow = "".join([i for i in follow if i.isalpha()])
31+
32+
if clean_pair in trigrams:
33+
trigrams[clean_pair] += [clean_follow]
34+
else:
35+
trigrams[clean_pair] = [clean_follow]
36+
return trigrams
37+
38+
def build_new(text_words):
39+
"""Assembles the new text output"""
40+
output_list = []
41+
for k in text_words:
42+
random_key = random.randint(0,1)
43+
random_value = random.randint(0,1)
44+
output_list.append(k[random_key])
45+
output_list.append(k[random_value])
46+
47+
for i in range(len(output_list)):
48+
x = (" ".join(output_list))
49+
return x
50+
51+
if __name__ == "__main__":
52+
(build_new(make_trigrams((open_text_file().split()))))

0 commit comments

Comments
 (0)