-
Notifications
You must be signed in to change notification settings - Fork 7
/
Copy pathquery_embedding.py
65 lines (51 loc) · 2.11 KB
/
query_embedding.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
import gensim
from pathlib import Path
import csv
import datetime
import os
from pprint import pprint
import argparse
parser = argparse.ArgumentParser(description="Querying the Twitch embedding")
parser.add_argument("--model_dir", dest="model_dir", default="embedding/embedding", action="store", type=str, help="models main folder")
args = parser.parse_args()
model_dir = Path(args.model_dir)
print("loading embedding")
model = gensim.models.word2vec.Word2Vec.load(str(args.model_dir))
print("embedding loaded")
word_vectors = model.wv
del model
###############################################################################
# In the following there are two examples on how you can query the embedding. #
# Words need to be lowercased. Emotes not. #
###############################################################################
#######################################
## Task 1: Detection of the odd word ##
#######################################
print("Detection of the odd word")
print("=========================")
print("youtube, twitch, instagram:")
pprint(word_vectors.doesnt_match("youtube twitch instagram".split(" ")))
print("\n")
###############################################
## Task 2: Words that fit in a given context ##
###############################################
print("Words that fit in a given context")
print("=================================")
print("monday, tuesday, wednesday:")
pprint(word_vectors.most_similar_cosmul("monday tuesday wednesday".split(" ")))
print("\n")
############################
## Task 3: Word relations ##
############################
print("Word relations")
print("==============")
print("Man relates to Woman as King to ...:")
pprint(word_vectors.most_similar_cosmul(positive=['king', 'woman'], negative=['man']))
print("\n")
###################################
## Task 4: Emote intensification ##
###################################
print("Emote intensifications")
print("======================")
print("LUL relates to OMEGALUL as FeelsGoodMan to ...:")
pprint(word_vectors.most_similar_cosmul(positive=['OMEGALUL', 'FeelsGoodMan'], negative=['LUL']))