-
Notifications
You must be signed in to change notification settings - Fork 0
/
Glove.py
60 lines (50 loc) · 2.32 KB
/
Glove.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
import os
import utils
import nltk
from loguru import logger
from pathlib import Path
class Glove():
def __init__(self, dataset_name) -> None:
input_file_path = 'input/' + dataset_name + '/raw.txt'
if not os.path.isfile(input_file_path):
raise Exception('Could not find ' + input_file_path)
self.dataset_name = dataset_name
self.input_file_path = input_file_path
self.dataset_path = 'input/' + dataset_name
self.corpus_file_path = self.dataset_path + '/corpus_for_glove.txt'
if not os.path.isfile('lib/glove/build/glove'):
self.setup_glove()
def setup_glove(self):
os.system('cd lib && git clone https://github.com/stanfordnlp/glove && cd glove && make')
os.system('chmod +x lib/glove-for-rhymes.sh')
def generate_vectors(self, vectors_file_path):
if not os.path.isfile(self.corpus_file_path):
self.__generate_corpus_file()
self.write_vectors(vectors_file_path)
def __generate_corpus_file(self):
logger.info('Reading corpus file and preprocessing')
lines = utils.read_file(self.input_file_path).split("\n")
last_document_lines = []
documents = []
for line in lines:
if not line.strip():
document = " ".join(last_document_lines)
documents.append(document)
last_document_lines = []
continue
if self.dataset_name == 'quran':
words = [word.lower() for word in nltk.word_tokenize(line) if word]
else:
words = [word.lower() for word in nltk.word_tokenize(line) if word.isalpha()]
last_document_lines.append(" ".join(words))
if len(last_document_lines):
document = " ".join(last_document_lines)
documents.append(document)
utils.write_file(self.corpus_file_path, "\n\n".join(documents))
logger.info('Finished. Saving sentences for later use in Glove')
return True
def write_vectors(self, vectors_file_path):
input_dir = os.path.realpath(self.dataset_path)
output_dir = os.path.realpath(os.path.dirname(vectors_file_path))
script_path = 'cd lib && ./glove-for-rhymes.sh ' + input_dir + ' ' + output_dir
os.system(script_path)