Ejemplo n.º 1
0
 def runGuess(self, numGuesses):
     """ Gets and evaluates one guess and increases the score """
     guess = self.screen.guess(numGuesses)                     # Get initial guess
     if WordValidator.correctWord(guess):
         self.score = self.score + WordGuessGame.SCORES[numGuesses]
         self.screen.winRound(guess, self.score)
         return True
         
     response = WordValidator.validate(guess)                   # Validate the guess
     self.screen.respond(response)                                    # Has Screen display the response to the guess
     
     
Ejemplo n.º 2
0
class TextFilesParser(TextDataParser):
    def __init__(self, textDataFormatter, textResultsMapper):
        self.textResultsMapper=textResultsMapper
        self.textFormatter=textDataFormatter
        self.wordValidator=WordValidator()
        
    def Parse(self, textFiles):
        wordCounts = {}
        wordDocuments={}
        wordSentences={}
        
        wordTokenizer=RegexpTokenizer(r'\w+')
        
        for fileName in textFiles:
            with open( fileName, "r" ) as fileObject:
                rawText=fileObject.read()
                formattedRawText=self.textFormatter.Format(rawText)
                sentences = tokenize.sent_tokenize(formattedRawText.decode('utf-8')) 
                for sentence in sentences:
                    words = wordTokenizer.tokenize(sentence)
                    for word in words:
                        if (self.wordValidator.Validate(word.lower())):
                            if (word in wordCounts.keys()):
                                wordCounts[word]=wordCounts[word]+1
                                wordSentences[word].add(sentence)
                                wordDocuments[word].add(fileName)
                            else:
                                wordCounts[word]=1
                                wordSentences[word]={sentence}
                                wordDocuments[word]={fileName}
        
        output = self.textResultsMapper.MapTextResults(wordCounts, wordDocuments, wordSentences)
        
        return output
Ejemplo n.º 3
0
 def start(self):
     """ Starts the game, runs until told to exit """
     self.exit = False
     WordValidator.target = None
     
     while not self.exit:
         if not WordValidator.nextWord():
             break
         self.gameLoop()
         
     self.screen.winGame()
Ejemplo n.º 4
0
 def guess(self, numGuesses):
     """ Prompts for a guess, and then reads a guess """
     while True:
         print "Enter your", len(WordValidator.target), "letter guess!"
         print "Guess correctly to score %d points!" % WordGuessGame.SCORES[numGuesses]
         if numGuesses == 0:
             """ Let user know no penalty for very first guess """
             print "First try! You get a freebe!"
         elif numGuesses == WordGuessGame.ATTEMPTS -1:
             """ Let user know its the last try """
             print "Last try!"
         else:
             """ Tell user the number of guesses they have left """
             print "You have %d guesses left" % (WordGuessGame.ATTEMPTS - numGuesses)
         guess = raw_input()
         response = WordValidator.invalidGuess(guess)
         if not response:
             return guess.lower()
         print response
Ejemplo n.º 5
0
 def testValidateGuess_Valid_Capitals(self):
     """ Recognize valid guesses with Capital letters """
     guess = self.word.upper()
     assert not WordValidator.invalidGuess(guess), "Should be valid guess"
Ejemplo n.º 6
0
 def testCheckMatch_incorrect(self):
     """ Recognize an incorrect match of characters """
     assert not WordValidator.match('a', 'b'), "Should not be a match"
Ejemplo n.º 7
0
 def testValidateGuess_Valid_Lower(self):
     """ Recognize valid guesses with Lowercase letters """
     guess = self.word
     assert not WordValidator.invalidGuess(guess), "Should be valid guess"
Ejemplo n.º 8
0
 def __init__(self, textDataFormatter, textResultsMapper):
     self.textResultsMapper=textResultsMapper
     self.textFormatter=textDataFormatter
     self.wordValidator=WordValidator()
Ejemplo n.º 9
0
 def testCheckClose_closeDown(self):
     """ Recognize a character within 5 characters down of the target """
     assert WordValidator.close("f", "a"), "Should be close"
Ejemplo n.º 10
0
 def testCheckClose_notClose(self):
     """ Recognize a character within 5 characters down of the target """
     assert not WordValidator.close("a", "z"), "Should not be close"
Ejemplo n.º 11
0
 def testCheckUp_down(self):
     """ Recognize target character is down in the alphabet """
     assert not WordValidator.up("a", "b"), "Should be down in the alphabet"
Ejemplo n.º 12
0
 def testCheckClose_closeUp(self):
     """ Recognize a character within 5 characters up of the target """
     assert WordValidator.close("a", "f"), "Should be close"
Ejemplo n.º 13
0
 def testValidateGuess_Invalid_IncorrectLength(self):
     """ Recognize invalid guesses with incorrect length """
     guess = ""
     assert WordValidator.invalidGuess(guess) == WordValidator.INVALID_LENGTH, "Should be invalid guess - >invalid length"
Ejemplo n.º 14
0
 def testCheckUp_up(self):
     """ Recognize target character is up in the alphabet """
     assert WordValidator.up("b", "a"), "Should be up in the alphabet"
Ejemplo n.º 15
0
 def testCheckMatch_correct(self):
     """ Recognize a correct match of characters """
     assert WordValidator.match('a', 'a'), "Should be a match"
Ejemplo n.º 16
0
 def testCorrectWord_incorrect(self):
     """ Recognize incorrect matches between target and guess  """
     WordValidator.correctWord("abc"),  "Should be incorrect word"
Ejemplo n.º 17
0
 def testCorrectWord_correct(self):
     """ Recognize correct matches between target and guess """
     assert WordValidator.correctWord("sky") , "Should be the correct word"
Ejemplo n.º 18
0
 def testValidateGuess_Invalid_InvalidCharacters_Etc(self):
     """ Recognize invalid guesses with other characters """
     guess = "sk!"
     assert WordValidator.invalidGuess(guess) == WordValidator.INVALID_CHARACTERS, "Should be invalid guess - >invalid characters(etc)" 
Ejemplo n.º 19
0
 def testValidateGuess_Invalid_InvalidCharacters_Numbers(self):
     """ Recognize invalid guesses with numbers """
     guess = "sk1"
     assert WordValidator.invalidGuess(guess) == WordValidator.INVALID_CHARACTERS, "Should be invalid guess - >invalid characters(numbers)"