Skip to content
Snippets Groups Projects
Commit 377dc2ee authored by Bayan Alkhuzaei CS2023's avatar Bayan Alkhuzaei CS2023
Browse files

assignment2b

parent e6e7e914
No related branches found
No related tags found
No related merge requests found
File added
#!/usr/bin/env python
import random
from letters import file2pairs
import sys
file = sys.argv[1]
prev_letter = sys.argv[2]
letters_dict = file2pairs(file)
prob = letters_dict[prev_letter]
p_value = prob.values()
support = 'abcdefghijklmnopqrstuvwxyz '
answer = random.choices(support,p_value)
print(answer[0])
#!/usr/bin/env python
import random
from letters import file2pairs,file2prob
import sys
loops = int(sys.argv[2])
file = sys.argv[1]
support = 'abcdefghijklmnopqrstuvwxyz '
list = []
i = 0
letters_and_probs = file2prob(file)
probs = letters_and_probs.values()
letter = random.choices(support,probs)
letter = letter[0]
while i < loops:
letters_dict = file2pairs(file)
prob = letters_dict[letter]
p_value = prob.values()
letter = random.choices(support,p_value).pop()
list.append(letter)
i = i+1
print("".join(list))
#!/usr/bin/env python
letters = 'abcdefghijklmnopqrstuvwxyz '
def file2prob(filename):
"""
Read a file and return a dictionary of letters and
their probabilities
"""
letter_dict = { c: 0 for c in letters }
letter_total = 0
with open(filename, encoding="utf-8") as fp:
for c in fp.read():
if c.lower() not in letter_dict:
continue
letter_dict[c.lower()] += 1
letter_total += 1
probs = { c: letter_dict[c]/letter_total for c in letter_dict }
return probs
def file2pairs(filename):
"""
Read a file and return a dictionary of letters and
the probabilities of following letters. That is, the
conditional probability of a letter given its
predecessor.
"""
letter_dict = { c: { a: 0 for a in letters }
for c in letters }
previous = None
with open(filename, encoding="utf-8") as fp:
for c in fp.read():
if c not in letter_dict:
continue
c = c.lower()
if previous is None:
previous = c
continue
letter_dict[previous][c] += 1
previous = c
probs = { c: { d: letter_dict[c][d]/sum(letter_dict[c].values())
for d in letters } for c in letters }
return probs
if __name__ == '__main__':
import sys
if len(sys.argv) != 3:
print("Usage: %s filename letter" % sys.argv[0])
sys.exit(-1)
filename = sys.argv[1]
letter = sys.argv[2]
probs = file2prob(filename)
print(probs[letter])
This diff is collapsed.
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment