Gensim-creating-a-bag-of-words-corpus

提供:Dev Guides
移動先:案内検索

Gensim-単語の袋(BoW)コーパスの作成

ドキュメントのリストとテキストファイル(1つだけでなく複数)から辞書を作成する方法を理解しました。 次に、このセクションでは、bag-of-words(BoW)コーパスを作成します。 Gensimと連携するためには、Gensimを理解する必要がある最も重要なオブジェクトの1つです。 基本的に、各ドキュメントにidという単語とその頻度が含まれているのはコーパスです。

弓コーパスの作成

議論したように、Gensimでは、コーパスのすべてのドキュメントに単語idとその頻度が含まれています。 ドキュメントの単純なリストとテキストファイルからBoWコーパスを作成できます。 必要なのは、トークン化された単語のリストを* Dictionary.doc2bow()*という名前のオブジェクトに渡すことです。 それでは、最初に、ドキュメントの簡単なリストを使用してBoWコーパスを作成することから始めましょう。

文の単純なリストから

次の例では、3つの文を含む単純なリストからBoWコーパスを作成します。

まず、次のように必要なすべてのパッケージをインポートする必要があります-

import gensim
import pprint
from gensim import corpora
from gensim.utils import simple_preprocess

次に、文章を含むリストを提供します。 リストには3つの文があります-

doc_list = [
   "Hello, how are you?", "How do you do?",
   "Hey what are you doing? yes you What are you doing?"
]

次に、次のように文のトークン化を行います-

doc_tokenized = [simple_preprocess(doc) for doc in doc_list]

次のように* corpora.Dictionary()*のオブジェクトを作成します-

dictionary = corpora.Dictionary()

次のように、これらのトークン化された文をdictionary.doc2bow()オブジェクトに渡します-

BoW_corpus = [dictionary.doc2bow(doc, allow_update=True) for doc in doc_tokenized]

ようやく、単語コーパスのバッグを印刷できます−

print(BoW_corpus)

出力

[
   [(0, 1), (1, 1), (2, 1), (3, 1)],
   [(2, 1), (3, 1), (4, 2)], [(0, 2), (3, 3), (5, 2), (6, 1), (7, 2), (8, 1)]
]

上記の出力は、id = 0の単語が最初のドキュメントに1回出現することを示しています(出力に(0,1)があるため)。

上記の出力は、人間が読むことはどういうわけか不可能です。 これらのIDを単語に変換することもできますが、そのためには次のように辞書で変換を行う必要があります-

id_words = [[dictionary[id], count) for id, count in line] for line in BoW_corpus]
print(id_words)

出力

[
   [('are', 1), ('hello', 1), ('how', 1), ('you', 1)],
   [('how', 1), ('you', 1), ('do', 2)],
   [('are', 2), ('you', 3), ('doing', 2), ('hey', 1), ('what', 2), ('yes', 1)]
]

上の出力は、どういうわけか人間が読める形式になっています。

完全な実装例

import gensim
import pprint
from gensim import corpora
from gensim.utils import simple_preprocess
doc_list = [
   "Hello, how are you?", "How do you do?",
   "Hey what are you doing? yes you What are you doing?"
]
doc_tokenized = [simple_preprocess(doc) for doc in doc_list]
dictionary = corpora.Dictionary()
BoW_corpus = [dictionary.doc2bow(doc, allow_update=True) for doc in doc_tokenized]
print(BoW_corpus)
id_words = [[dictionary[id], count) for id, count in line] for line in BoW_corpus]
print(id_words)

テキストファイルから

次の例では、テキストファイルからBoWコーパスを作成します。 このため、前の例で使用したドキュメントを* doc.txt。*という名前のテキストファイルに保存しました。

Gensimはファイルを1行ずつ読み取り、 simple_preprocess を使用して一度に1行ずつ処理します。 この方法では、ファイル全体を一度にメモリにロードする必要はありません。

実装例

最初に、次のように必要なパッケージと必要なパッケージをインポートします-

import gensim
from gensim import corpora
from pprint import pprint
from gensim.utils import simple_preprocess
from smart_open import smart_open
import os

次に、次のコード行は、doc.txtからドキュメントを読み取り、トークン化します-

doc_tokenized = [
   simple_preprocess(line, deacc =True) for line in open(‘doc.txt’, encoding=’utf-8’)
]
dictionary = corpora.Dictionary()

次に、これらのトークン化された単語を* dictionary.doc2bow()*オブジェクトに渡す必要があります(前の例で行ったように)

BoW_corpus = [
   dictionary.doc2bow(doc, allow_update=True) for doc in doc_tokenized
]
print(BoW_corpus)

出力

[
   [(9, 1), (10, 1), (11, 1), (12, 1), (13, 1), (14, 1), (15, 1)],
   [
      (15, 1), (16, 1), (17, 1), (18, 1), (19, 1), (20, 1), (21, 1),
      (22, 1), (23, 1), (24, 1)
   ],
   [
      (23, 2), (25, 1), (26, 1), (27, 1), (28, 1), (29, 1),
      (30, 1), (31, 1), (32, 1), (33, 1), (34, 1), (35, 1), (36, 1)
   ],
   [(3, 1), (18, 1), (37, 1), (38, 1), (39, 1), (40, 1), (41, 1), (42, 1), (43, 1)],
   [
      (18, 1), (27, 1), (31, 2), (32, 1), (38, 1), (41, 1), (43, 1),
      (44, 1), (45, 1), (46, 1), (47, 1), (48, 1), (49, 1), (50, 1), (51, 1), (52, 1)
   ]
]
*doc.txt* ファイルには次の内容があります-

以前は計算ネットワークツールキットとして知られていたCNTKは、ディープラーニングアルゴリズムをトレーニングして人間の脳のように学習できる、無料で使用できるオープンソースの商用グレードのツールキットです。

finddevguides.comで無料のチュートリアルを見つけることができます。AI深層学習機械学習などのテクノロジーに関する最高のテクニカルチュートリアルも無料で提供しています。

完全な実装例

import gensim
from gensim import corpora
from pprint import pprint
from gensim.utils import simple_preprocess
from smart_open import smart_open
import os
doc_tokenized = [
   simple_preprocess(line, deacc =True) for line in open(‘doc.txt’, encoding=’utf-8’)
]
dictionary = corpora.Dictionary()
BoW_corpus = [dictionary.doc2bow(doc, allow_update=True) for doc in doc_tokenized]
print(BoW_corpus)

Gensimコーパスの保存と読み込み

次のスクリプトを使用してコーパスを保存できます-

corpora.MmCorpus.serialize(‘/Users/Desktop/BoW_corpus.mm’, bow_corpus)

#コーパスのパスと名前を提供します。 コーパスの名前はBoW_corpusで、Matrix Market形式で保存しました。

同様に、次のスクリプトを使用して、保存されたコーパスをロードできます-

corpus_load = corpora.MmCorpus(‘/Users/Desktop/BoW_corpus.mm’)
for line in corpus_load:
print(line)