def test_rhyme(self): self.assertTrue( Rhymes.is_rhyme(StressedWord("тишь", {Stress(1)}), StressedWord("грустишь", {Stress(5)}))) self.assertFalse( Rhymes.is_rhyme(StressedWord("наизусть", {Stress(4)}), StressedWord("сестра", {Stress(5)})))
def test_rhyme(self): self.assertTrue(Rhymes.is_rhyme(Word(0, 4, "тишь", [Syllable(0, 4, 0, "тишь", 1)]), Word(0, 8, "грустишь", [Syllable(0, 3, 0, "гру"), Syllable(3, 8, 1, "стишь", 5)]))) self.assertFalse(Rhymes.is_rhyme(Word(0, 8, "наизусть", [Syllable(0, 2, 0, "на"), Syllable(2, 4, 1, "из"), Syllable(4, 8, 2, "усть", 4)]), Word(0, 6, "сестра", [Syllable(0, 3, 0, "сест"), Syllable(3, 6, 1, "ра", 5)])))
def is_rhyme(self, word1: str, word2: str) -> bool: """ :param word1: первое слово. :param word2: второе слово. :return: рифмуются ли слова. """ markup_word1 = self.get_markup(word1).lines[0].words[0] markup_word1.set_stresses(self.get_stresses(word1)) markup_word2 = self.get_markup(word2).lines[0].words[0] markup_word2.set_stresses(self.get_stresses(word2)) return Rhymes.is_rhyme(markup_word1, markup_word2)
def _filter_word_by_rhyme(self, word: StressedWord) -> bool: if len(word.syllables) <= 1: return False rhyming_words = self.letters_to_rhymes[self.rhyme_pattern[ self.rhyme_position]] if len(rhyming_words) == 0: return True first_word = list(rhyming_words)[0] is_rhyme = Rhymes.is_rhyme( first_word, word, score_border=self.score_border, syllable_number_border=2) and first_word.text != word.text return is_rhyme
def filter_word(self, word: StressedWord) -> bool: """ Фильтрация слова по рифме в текущей позиции. :param word: слово. :return: подходит слово или нет. """ if len(word.syllables) <= 1: return False if len(self.letters_to_rhymes[self.rhyme_pattern[self.position]]) == 0: return True first_word = list( self.letters_to_rhymes[self.rhyme_pattern[self.position]])[0] is_rhyme = Rhymes.is_rhyme(first_word, word, score_border=self.score_border, syllable_number_border=2, word_form_vocabulary=self.word_form_vocabulary) and \ first_word.text != word.text return is_rhyme
def get_word_rhymes(self, word: str, vocab_dump_path: str, markup_path: str = None) -> List[str]: """ Поиск рифмы для данного слова. :param word: слово. :param vocab_dump_path: путь, куда сохраняется словарь. :param markup_path: путь к разметкам. :return: список рифм. """ markup_word = self.get_markup(word).lines[0].words[0] markup_word.set_stresses(self.get_stresses(word)) rhymes = [] vocabulary = self.get_vocabulary(vocab_dump_path, markup_path) for i in range(vocabulary.size()): if Rhymes.is_rhyme(markup_word, vocabulary.get_word(i)): rhymes.append(vocabulary.get_word(i).text.lower()) return rhymes