2011-04-18, 07:01 PM
I've been having some trouble trying to figure what I'm supposed to do for a portion of my program. More specifically, my program needs to be able to do "Markov Analysis."
If the term doesn't seem familiar, it essentially means my program needs to be able to dissect a sentence (or paragraph) and reassemble it using its given prefixes and suffixes. For example, the sample sentence I put up:
A prefix would be the word before the next word and the suffix is the one coming after. Ex. "be" is the prefix and "or" or "that" could be the potential suffix. Which is the part I'm having problems with, I don't really have an idea of how the reassembling part should be like.
Here's my code thus far:
Disregard what I currently have under "def generate" since my code doesn't work but it's what I have thought up so far. "def analyze" should be already complete (but hey, if you can optimize it further go ahead!).
If my explanation sucks butt, here's copypasta from the TA for the objective:
Start with the given prefix
Select at random a possible suffix that comes after this prefix (use random.choice([collection goes here]) to pick at random)
Print or store the new word and use it to form a new prefix
Repeat
Now I don't think we're required to have it work with punctuation as well, but if you guys can get that to work too, I'd be really thankful. It'd really help with my learning process
If the term doesn't seem familiar, it essentially means my program needs to be able to dissect a sentence (or paragraph) and reassemble it using its given prefixes and suffixes. For example, the sample sentence I put up:
Quote:to be or not to be that is the question
A prefix would be the word before the next word and the suffix is the one coming after. Ex. "be" is the prefix and "or" or "that" could be the potential suffix. Which is the part I'm having problems with, I don't really have an idea of how the reassembling part should be like.
Here's my code thus far:
Code:
# Lab 9 - Markov Analysis
# CMPUT 174 W11
import random
import sys
def shift (prefix, word):
"""
Pops a word out of a tuple and pushes another word in
"""
return prefix[1:] + (word,)
def readWords(fin):
"""
Reads the file into the words list
"""
wordsList = []
for line in fin:
wordsList.extend(line.split(' '))
return wordsList
def analyze(wordsList,suffixes):
"""
Perform the Markov Analysis
"""
possibleSuffixes = dict()
# Your code goes here
prefix = []
prefix.append((wordsList[0],wordsList[1]))
for i in range(len(wordsList)-2):
if prefix[i] not in possibleSuffixes:
possibleSuffixes[prefix[i]] = [wordsList[i+2]]
else:
possibleSuffixes[prefix[i]].append(wordsList[i+2])
prefix.append((wordsList[i+1],wordsList[i+2]))
print 'possible suffixes:',possibleSuffixes
print 'prefixes', prefix
return possibleSuffixes
def generate(suffixes, prefix):
"""
Generate text based on the known suffixes
"""
generatedText = prefix[0] + " " + prefix[1]
prefix = prefix[0],prefix[1]
print "First pair: ", prefix
# Your code goes here
i = 1
suffix = prefix[1]
print suffix
while i != 0:
if suffix not in prefix[i]:
i = 1
exit()
else:
suffix = suffixes.get(prefix)
if len(suffix) > 1:
suffix = [random.choice(suffix)]
print "Random choosen:",suffix
prefix = shift(prefix,suffix[0])
print "Prefixes after:",prefix
print "Suffix:", suffix
i += 1
generatedText = generatedText + " " + suffix[0]
return generatedText
if __name__ == "__main__":
# The input file as a list of words in order
#filename = sys.argv[1]
filename = "Input.txt"
fin = open(filename)
words = readWords(fin);
suffixes = dict()
# Do the analysis and return the dictionary that maps every tupple into a prefix word
suffixes = analyze(words, suffixes)
# Pick a starting prefix at random
startingPrefix = random.choice(suffixes.keys())
# Generate the text and print it
print generate(suffixes, startingPrefix)Disregard what I currently have under "def generate" since my code doesn't work but it's what I have thought up so far. "def analyze" should be already complete (but hey, if you can optimize it further go ahead!).
If my explanation sucks butt, here's copypasta from the TA for the objective:
Markov Analysis
[Spoiler=General format of how "Generate" should work]Start with the given prefix
Select at random a possible suffix that comes after this prefix (use random.choice([collection goes here]) to pick at random)
Print or store the new word and use it to form a new prefix
Repeat
Now I don't think we're required to have it work with punctuation as well, but if you guys can get that to work too, I'd be really thankful. It'd really help with my learning process

