Demo entry 6365845

fgf

   

Submitted by anonymous on May 21, 2017 at 14:53
Language: Python 3. Code size: 1.0 kB.

import nltk
import sqlite3

cutoff = int(len(nltk.corpus.brown.tagged_sents()) * 0.8)
tagged_sentences = nltk.corpus.brown.tagged_sents(tagset='universal')[:cutoff]


def get_tag(pair_word_tag):
    return pair_word_tag[1]


def get_word(pair_word_tag):
    return pair_word_tag[0]

with sqlite3.connect('brown_corpus.db') as conn:
    c = conn.cursor()
    for sentence in tagged_sentences:
        for index in range(len(sentence)):
            prev_tag_2 = None if index == 0 or index == 1 else get_tag(sentence[index-2])
            prev_tag_1 = None if index == 0 else get_tag(sentence[index-1])
            next_tag = None if index == len(sentence) - 1 else get_tag(sentence[index + 1])
            word = get_word(sentence[index])
            tag = get_tag(sentence[index])
            c.execute("INSERT INTO large_word_context_1(prev_tag_2, prev_tag_1, word, tag, next_tag)"
                      " VALUES (?, ?, ?, ?, ?)", (prev_tag_2, prev_tag_1, word, tag, next_tag))

This snippet took 0.00 seconds to highlight.

Back to the Entry List or Home.

Delete this entry (admin only).