トレーニングセットの前処理とワードベクトルの生成

元のトレーニングコーパスの形式は次のとおりです。

{
	"sentText": "But that spasm of irritation by a master intimidator was minor compared with what Bobby Fischer , the erratic former world chess champion , dished out in March at a news conference in Reykjavik , Iceland .",
	"articleId": "/m/vinci8/data1/riedel/projects/relation/kb/nyt1/docstore/nyt-2005-2006.backup/1677367.xml.pb",
	"relationMentions": [{
		"em1Text": "Bobby Fischer",
		"em2Text": "Iceland",
		"label": "/people/person/nationality"
	}, {
		"em1Text": "Iceland",
		"em2Text": "Reykjavik",
		"label": "/location/country/capital"
	}, {
		"em1Text": "Iceland",
		"em2Text": "Reykjavik",
		"label": "/location/location/contains"
	}, {
		"em1Text": "Bobby Fischer",
		"em2Text": "Reykjavik",
		"label": "/people/deceased_person/place_of_death"
	}],
	"entityMentions": [{
		"start": 0,
		"label": "PERSON",
		"text": "Bobby Fischer"
	}, {
		"start": 1,
		"label": "LOCATION",
		"text": "Reykjavik"
	}, {
		"start": 2,
		"label": "LOCATION",
		"text": "Iceland"
	}],
	"sentId": "1"
}

 ステートメントのみの形式に処理する必要があります。

But that spasm of irritation by a master intimidator was minor compared with what Bobby Fischer , the erratic former world chess champion , dished out in March at a news conference in Reykjavik , Iceland .

コードは次のように表示されます。

import json
import io
train = "./train.json"
result = './trainResult.txt'
fw = open(result, 'w')
with io.open(train, 'r', encoding='utf-8') as f:
	for line in f: 
		data = json.loads(line)
        	fw.write(data['sentText'])
		fw.write('\n')
fw.close()

単語ベクトルの入力ファイルを生成した後、次のステップは、各単語に対応する単語ベクトルを生成することです。word2vecツールを使用する必要があります。アドレスは次のとおりです。

https://github.com/dav/word2vec

コアコマンドはcreate-text8-vector-data.shファイルにあります

time $BIN_DIR/word2vec -train $TEXT_DATA -output $VECTOR_DATA -cbow 1 -size 300 -window 8 -negative 25 -hs 0 -sample 1e-4 -threads 20 -binary 0 -iter 15

-binary = 0は、出力がテキストワードベクトルであることを意味します。-binary= 1は、バイナリワードベクトルが生成されることを意味します。

おすすめ

転載: blog.csdn.net/u011939633/article/details/93871728