Exemplo n.º 1
0
    def run(self):
        characters = list(string.ascii_uppercase)
        print(characters)

        for character in characters:
            # for character in ['B']:
            character_words = Word.begin_with(self.word_list, character)
            total_words = len(character_words)
            if total_words == 0:
                continue
            self.print_character(character, total_words)

            word_list = Word.sorted(character_words)
            for word in word_list:
                self.print_word(word)
Exemplo n.º 2
0
    def print_word(word):
        word_chars = 12
        word_length = len(Word.strip_accents(word.pinyin))
        word_str = word.pinyin + " " * (word_chars - word_length)

        word_type_chars = 10
        word_type = word.word_meanings[0].word_type.book_type
        word_type_str = word_type + " " * (word_type_chars - len(word_type))

        space_str = (" " if len(str(word.lesson_number)) == 1 else "")
        lesson_str = space_str + str(word.lesson_number)

        print word_str + " " + word_type_str + " " + lesson_str
Exemplo n.º 3
0
    def __process_row(self, row, word_number):
        row_length = len(row)
        lesson_number = None
        if self.lesson_number == 'hsk4':
            assert (row_length == 8)
            hanzi, pinyin, definition1, word_type1, definition2, word_type2, lesson_number, word_number = row
            lesson_number = int(lesson_number)
        elif self.lesson_number == 'hsk3':
            assert (row_length == 11)
            hanzi, pinyin, definition1, word_type1, definition2, word_type2, _, _, _, _, _ = row
        else:
            assert (row_length == 8)
            hanzi, pinyin, definition1, word_type1, definition2, word_type2, _, _ = row

        word_meanings = [WordMeaning(word_type1, definition1)]
        if len(definition2) > 0:
            word_meanings.append(WordMeaning(word_type2, definition2))

        return Word(pinyin, hanzi, definition1, word_meanings, word_number,
                    lesson_number)
Exemplo n.º 4
0
class TestWord(unittest.TestCase):
    def setUp(self):
        self.word = Word("stellen", "mettre")

    def test_word1_creation(self):
        self.assertEqual("stellen", self.word.spelling["de"])
        self.assertEqual("mettre", self.word.spelling["fr"])

    def test_word1_as_no_options(self):
        self.assertEqual(3, len(self.word.options))
        self.assertEqual("", self.word.options["comment"])
        self.assertEqual("", self.word.options["inflection"])
        self.assertEqual("", self.word.options["plural"])

    def test_adding_an_option(self):
        self.word.setAnOption("comment", "comment")
        self.assertEqual("comment", self.word.options["comment"])
        self.word.setAnOption("inflection", "inflection")
        self.assertEqual("inflection", self.word.options["inflection"])
        self.word.setAnOption("plural", "plural")
        self.assertEqual("plural", self.word.options["plural"])

    def test_trying_to_add_option_with_wrong_key_value(self):
        self.word.setAnOption("test", "comment")
        self.assertRaises(KeyError)

    def test_semanticity_of_entity(self):
        copy = Word("stellen", "mettre")
        self.assertEqual(copy, self.word)
        self.assertNotEqual(copy, "coucou")

    def test_should_raise_exception_if_constructed_from_empty_string(self):
        with self.assertRaises(Exception) as context:
            Word("", "mettre")
        self.assertTrue(
            "Cannot build a word from an empty data" in str(context.exception))
Exemplo n.º 5
0
def main():
    user = User(screen_name="@gean_dreson")
    user.get_tweets()

    words = Word()
    words.pre_processing(tweets_collection=user.tweets_collection)
    words.get_word_frequencies()

    with open('data/tweets.json', 'w') as file:
        json.dump(user.tweets_collection, file, indent=3, ensure_ascii=False)

    with open('data/word_frequencies.json', 'w') as file:
        json.dump(words.word_frequencies, file, indent=3, ensure_ascii=False)

    wordcloud = WordCloud(max_words=4000).generate_from_frequencies(
        words.word_frequencies)
    plt.imshow(wordcloud, interpolation='bilinear')
    plt.axis("off")
    plt.show()
Exemplo n.º 6
0
 def get_word(self, i):  # retorna a palavra na posição i
     if i < 0 or i >= len(self.words):
         return Word(
             '_', self.probability
         )  # caso o indice não exista, retornar '_' (palavra finalizadora)
     return self.words[i]
Exemplo n.º 7
0
 def __init__(self, wrds=(), p=1):
     self.probability = p
     self.words.clear()
     for w in wrds:  # cria uma Word para cada palavra do texto
         self.words.append(Word(w, self.probability))
Exemplo n.º 8
0
def reloadIfGreater(guesses, diff):
    new_wordlist = Word()
    while (guesses < diff):
        hangman(new_wordlist.loadWords().lower())
Exemplo n.º 9
0
            print('Oops! You have already guessed that letter: ', guessed)
        elif letter in secretWord:
            lettersGuessed.append(letter)

            guessed = letterInWord(secretWord, lettersGuessed)

            print('Good Guess: ', guessed)
        else:
            guesses -= 1
            lettersGuessed.append(letter)

            guessed = letterInWord(secretWord, lettersGuessed)

            print('Oops! That letter is not in my word: ', guessed)

        print('------------')

    else:
        if isWordGuessed(secretWord, lettersGuessed) == True:
            print('Congratulations, you won!')
        else:
            print('Sorry, you ran out of guesses. The word was ', secretWord,
                  '.')
    quit()


start_word = Word()
start_word.iftxtFile()
hangman(start_word.loadWords().lower())
Exemplo n.º 10
0
 def setUp(self):
     self.word = Word("stellen", "mettre")
Exemplo n.º 11
0
 def test_should_raise_exception_if_constructed_from_empty_string(self):
     with self.assertRaises(Exception) as context:
         Word("", "mettre")
     self.assertTrue(
         "Cannot build a word from an empty data" in str(context.exception))
Exemplo n.º 12
0
 def test_semanticity_of_entity(self):
     copy = Word("stellen", "mettre")
     self.assertEqual(copy, self.word)
     self.assertNotEqual(copy, "coucou")