-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathutils.py
153 lines (104 loc) · 3.95 KB
/
utils.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
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
# Objective: load and tokenize data with spaCy
import json
import spacy
from gensim.parsing.preprocessing import remove_stopwords, strip_tags
from gensim.utils import simple_preprocess
def get_data_folder():
folder_name = 'data/'
return folder_name
def get_raw_data_file_name():
file_name = get_data_folder() + 'aggregate_prettyprint.json'
return file_name
def get_token_file_name():
file_name = get_data_folder() + 'tokens.json'
return file_name
def get_word_model_file_name():
file_name = get_data_folder() + 'word2vec.model'
return file_name
def get_doc_model_file_name():
file_name = get_data_folder() + 'doc2vec.model'
return file_name
def load_raw_data(verbose=False):
if verbose:
print('Loading raw data')
with open(get_raw_data_file_name()) as f:
steam_sentences = json.load(f)
return steam_sentences
def load_game_names(include_genres=True, include_categories=True):
steam_sentences = load_raw_data()
game_names = {}
game_tags = {}
for app_id in steam_sentences:
game_names[app_id] = steam_sentences[app_id]['name']
game_tags[app_id] = []
if include_genres:
try:
game_tags[app_id] += steam_sentences[app_id]['genres']
except KeyError:
pass
if include_categories:
try:
game_tags[app_id] += steam_sentences[app_id]['categories']
except KeyError:
pass
return game_names, game_tags
def load_tokens():
print('Loading tokens')
with open(get_token_file_name()) as f:
steam_tokens = json.load(f)
return steam_tokens
def compute_tokens(steam_sentences=None, save_to_disk=False, use_spacy=False):
print('Computing tokens')
if steam_sentences is None:
steam_sentences = load_raw_data()
counter = 0
num_games = len(steam_sentences)
steam_tokens = {}
# You need to have downloaded the model first. Reference: https://spacy.io/models/en#section-en_core_web_lg
nlp = spacy.load('en_core_web_lg')
for app_id in steam_sentences:
game_data = steam_sentences[app_id]
counter += 1
if (counter % 1000) == 0:
print(
'[{}/{}] appID = {} ({})'.format(
counter,
num_games,
app_id,
game_data['name'],
),
)
if use_spacy:
original_str = str(strip_tags(game_data['text']))
original_str = original_str.replace('\t', ' ')
# Reference: https://nicschrading.com/project/Intro-to-NLP-with-spaCy/
original_str = original_str.strip().replace('\n', ' ').replace('\r', ' ')
original_str = (
original_str.replace('&', 'and')
.replace('>', '>')
.replace('<', '<')
)
doc = nlp(original_str)
ents = [str(entity).strip() for entity in doc.ents] # Named entities.
# Keep only words (no numbers, no punctuation).
# Lemmatize tokens, remove punctuation and remove stopwords.
doc = [
token.lemma_ for token in doc if token.is_alpha and not token.is_stop
]
# Add named entities, but only if they are a compound of more than word.
relevant_entities = [str(entity) for entity in ents if len(entity) > 1]
doc.extend(relevant_entities)
game_tokens = doc
else:
game_tokens = simple_preprocess(
remove_stopwords(strip_tags(game_data['text'])),
deacc=True,
min_len=3,
)
steam_tokens[app_id] = list(game_tokens)
if save_to_disk:
with open(get_token_file_name(), 'w') as f:
json.dump(steam_tokens, f)
return steam_tokens
if __name__ == '__main__':
compute_tokens(save_to_disk=True, use_spacy=True)