예제 #1
0
def line_handler(db: Database, line_queue: Queue, error_queue: Queue,
                 source_id: int) -> None:

    session = get_session(db)
    source = session.query(Source).filter(Source.id == source_id).one()

    while True:
        try:
            line_pair = line_queue.get()
            if line_pair == DONE_READING:
                break

            line_no, line = line_pair
            stems = nlp.stem_sentence(line)
            rhyme_sound = nlp.rhyme_sound(line)
            syllables = nlp.count_syllables(line)
            alliteration = nlp.has_alliteration(line)

            phrase = Phrase(stems=stems,
                            raw=line,
                            alliteration=alliteration,
                            rhyme_sound=rhyme_sound,
                            syllables=syllables,
                            line_no=line_no,
                            source=source)

            session.add(phrase)
        except Exception as e:
            error_queue.put(e)
            log.error('Died while processing text, rolling back')
            session.rollback()
            session.close()
            return

    session.commit()
예제 #2
0
def line_handler(db: Database,
                 line_queue: Queue,
                 error_queue: Queue,
                 source_id: int) -> None:

    session = get_session(db)
    source = session.query(Source).filter(Source.id == source_id).one()

    while True:
        try:
            line_pair = line_queue.get()
            if line_pair == DONE_READING:
                break

            line_no, line = line_pair
            stems = nlp.stem_sentence(line)
            rhyme_sound = nlp.rhyme_sound(line)
            syllables = nlp.count_syllables(line)
            alliteration = nlp.has_alliteration(line)

            phrase = Phrase(stems=stems, raw=line, alliteration=alliteration,
                            rhyme_sound=rhyme_sound,
                            syllables=syllables, line_no=line_no, source=source)

            session.add(phrase)
        except Exception as e:
            error_queue.put(e)
            log.error('Died while processing text, rolling back')
            session.rollback()
            session.close()
            return

    session.commit()
예제 #3
0
def process_sentence(sentence, source_name, line_no):
    phonemes = list(map(nlp.word_to_phonemes, nlp.words(sentence)))
    return {'stems': nlp.stem_sentence(sentence),
            'source': source_name,
            'tagged': nlp.tag(sentence),
            'rhyme_sound': nlp.rhyme_sound(sentence),
            'phonemes': phonemes,
            'num_syllables': nlp.count_syllables(sentence),
            'line_no': line_no,
            'alliteration': nlp.has_alliteration(sentence),
            'raw': sentence,
            'blank': False,
    }
예제 #4
0
def process_sentence(sentence, source_name, line_no):
    phonemes = list(map(nlp.word_to_phonemes, nlp.words(sentence)))
    return {
        'stems': nlp.stem_sentence(sentence),
        'source': source_name,
        'tagged': nlp.tag(sentence),
        'rhyme_sound': nlp.rhyme_sound(sentence),
        'phonemes': phonemes,
        'num_syllables': nlp.count_syllables(sentence),
        'line_no': line_no,
        'alliteration': nlp.has_alliteration(sentence),
        'raw': sentence,
        'blank': False,
    }