简体   繁体   中英

Gensim word2vec online training

I am training word2vec model in gensim using the sentences in a csv file as follows:

import string
import gensim
import csv
import nltk

path = '/home/neel/Desktop/csci544_proj/test/sample.csv'
translator = str.maketrans({key: None for key in string.punctuation})

class gen(object):

    def __init__(self, path):
        self.path = path

    def __iter__(self):
        with open(path) as infile:
            reader = csv.reader(infile)
            for row in reader:
                rev = row[4]
                l = nltk.sent_tokenize(rev)
                for sent in l:
                    sent = sent.translate(translator)
                    yield sent.lower().split()

sentences = [path]
for p in gen(path):
    model = gensim.models.Word2Vec(p, min_count=1, iter=1)

print(model.vocab.keys())

I get the following result: (['b', 'u', 'm', 'h', 'e', 'n', 'r', 'v', 'i', 'a', 't', 's', 'k', 'w', 'o', 'l'])

The result I am get is not words but the characters. Where is the program going wrong?

I fix your code

import string
import gensim
import csv
import nltk

path = '/home/neel/Desktop/csci544_proj/test/sample.csv'
translator = str.maketrans({key: None for key in string.punctuation})

class Generator(object):
    def __init__(self, pathes):
        self.pathes = pathes

    def __iter__(self):
        for path in self.pathes:
            with open(path) as infile:
                for row in csv.reader(infile):
                    for sent in nltk.sent_tokenize(row[4]):
                        yield sent.translate(translator).lower().split()


corpus = Generator([path])
model = gensim.models.Word2Vec(min_count=1, iter=1)
model.build_vocab(corpus)
model.train(corpus, total_examples=model.corpus_count, epochs=2)
model.wv.vocab.keys()

You get dict_keys(['wassup', 'where', 'fresh', 'new', 'about', 'juice', 'whats', 'are', 'im', 'hello', 'wtf', 'd', 'hi', 'you', 'world', 'bro', 'friend'])

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM