1

コミュニティメンバーの皆様、こんにちは。

現在、Word2Vec アルゴリズムを実装しています。

まず、データ (文) を抽出し、文を分割してトークン (単語) に分割し、句読点を削除して、トークンを 1 つのリストに格納します。リストには基本的に単語が含まれています。次に、単語の頻度を計算し、頻度で出現回数を計算しました。リストになります。

次に、gensim を使用してモデルをロードしようとしています。しかし、私は問題に直面しています。問題は約the word is not in the vocabularyです。私が試したコードスニペットは次のとおりです。

import nltk, re, gensim
import string
from collections import Counter
from string import punctuation
from nltk.tokenize import word_tokenize
from gensim.models import Word2Vec
from nltk.corpus import gutenberg, stopwords

def preprocessing():
    raw_data = (gutenberg.raw('shakespeare-hamlet.txt'))
    tokens = word_tokenize(raw_data)
    tokens = [w.lower() for w in tokens]
    table = str.maketrans('', '', string.punctuation)
    stripped = [w.translate(table) for w in tokens]
    global words
    words = [word for word in stripped if word.isalpha()]
    sw = (stopwords.words('english'))
    sw1= (['.', ',', '"', '?', '!', ':', ';', '(', ')', '[', ']', '{', '}'])
    sw2= (['for', 'on', 'ed', 'es', 'ing', 'of', 'd', 'is', 'has', 'have', 'been', 'had', 'was', 'are', 'were', 'a', 'an', 'the', 't', 's', 'than', 'that', 'it', '&', 'and', 'where', 'there', 'he', 'she', 'i', 'and', 'with', 'it', 'to', 'shall', 'why', 'ham'])
    stop=sw+sw1+sw2
    words = [w for w in words if not w in stop]
preprocessing()

def freq_count():
    fd = nltk.FreqDist(words)
    print(fd.most_common())
    freq_count()
def word_embedding():
    for i in range(len(words)):
        model = Word2Vec(words, size = 100, sg = 1, window = 3, min_count = 1, iter = 10, workers = 4)
        model.init_sims(replace = True)
        model.save('word2vec_model')
        model = Word2Vec.load('word2vec_model')
        similarities = model.wv.most_similar('hamlet')
        for word, score in similarities:
            print(word , score)
word_embedding()

注: Windows OS で Python 3.7 を使用しています。から、syntax of gensim文を使用してトークンに分割し、同じものを適用してモデルを構築およびトレーニングすることが提案されています。私の質問は、単語のみを含む単一のリストを持つコーパスに同じことを適用する方法です。モデルのトレーニング中に、リスト、つまり [words] も使用して単語を指定しました。

4

2 に答える 2