def rhyme_sound(sentence): tagged_sentence = tag(sentence) without_punctuation = filter(compose(is_alpha_tag, second), tagged_sentence) ws = list(map(first, without_punctuation)) if is_empty(ws): return None last_word = last(ws) phonemes = word_to_phonemes(last_word) if is_empty(phonemes): return None return "".join(phonemes[-3:])
def expand_multiclauses(sentences): # TODO consider itertools split = [] for sentence in sentences: tagged_sentence = tag(sentence) if not is_empty(tagged_sentence): split += split_multiclause(sentence, tagged_sentence) return split
def map_letters_to_sounds(conn: Connection, corpus_id: int, template, sound_cache=None): letters = list(set(pluck(template, "rhyme"))) if is_empty(letters): cache = {} else: sounds = sound_cache if sound_cache is not None else unique_sounds(conn, corpus_id) cache = dict(map(lambda l: [l, choice(sounds)], letters)) return cache
def map_letters_to_sounds(db, template, sound_cache=None): letters = list(set(pluck(template, "rhyme"))) if is_empty(letters): cache = {} else: sounds = sound_cache if sound_cache is not None else build_sound_cache(db) cache = dict(map(lambda l: [l, choice(sounds)], letters)) return cache
def ruleset_to_line(db, ruleset): line = None while not line: lines = list(db.find(ruleset.to_query())) if is_empty(lines): ruleset.weaken() else: line = choice(lines) return line
def map_letters_to_sounds(db, template, sound_cache=None): letters = list(set(pluck(template, "rhyme"))) if is_empty(letters): cache = {} else: sounds = sound_cache if sound_cache is not None else build_sound_cache( db) cache = dict(map(lambda l: [l, choice(sounds)], letters)) return cache
def ruleset_to_line(conn, corpus_id, ruleset) -> str: if ruleset.contains(dogma.BlankRule): return ("",) line = None while not line: sql = ruleset.to_query(conn) lines = conn.execute(sa.text(sql).params(corpus_id=corpus_id)).fetchall() if is_empty(lines): ruleset.weaken() else: line = choice(lines) return line
def prime_cache(self, conn, corpus_id): log.debug('building phrase cache') sql = """ select p.line_no, p.source_id from phrases p join corpora_sources cs on p.source_id = cs.source_id where corpus_id = :corpus_id and p.stems @> ARRAY[:keyword] """ self.phrase_cache = conn.execute(sa.text(sql)\ .params(keyword=self.keyword, corpus_id=corpus_id))\ .fetchall() if is_empty(self.phrase_cache): self.strength = 0
def first_consonant_sound(word): phonemes = word_to_phonemes(word) if not is_empty(phonemes): return find_first(is_consonant_phoneme, phonemes) else: return first(consonant_re.findall(word.upper()))
def prime_cache(self, db): print('building phrase cache') self.phrase_cache = list(db.find({'stems': self.keyword})) if is_empty(self.phrase_cache): self.strength = 0