-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathtext2index.py
76 lines (58 loc) · 2.17 KB
/
text2index.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
#!/usr/bin/env python
from scipy import *
import itertools
from optparse import OptionParser
import sys
import numpy as np
import pickle
parser = OptionParser()
parser.add_option("-v", "--verbosity", action="store_true", dest="verbosity", help="Verbose?")
(options, args) = parser.parse_args()
if (len(args)<2):
print "usage: -options input_filename vector_datafile output_filename"
print options
sys.exit(2)
inputFilename = args[0]
inputVocab = args[1]
outFilename = args[2]
print "input file", inputFilename
print "vocabulary file (for instance, generated by SRILM)", inputVocab
print "output file", outFilename
vocab_size = -1
print "reading vocabulary file"
inputVocablines = file(inputVocab).readlines()
print "Vocabulary size:",len(inputVocablines)
print "reading file data"
inputFile = file(inputFilename).readlines()
print "Corpus size:",len(inputFile)
outputFile = open(outFilename+'.dic', "wb" )
print "Building vocabulary dictionary"
i = 0
words_dict = {}
for line in inputVocablines:
words_dict[line.split("\n")[0]] = i+1
i+=1
words_dict['<unk>'] = 0
vocab_size=len(inputVocablines)
corpus_size=len(inputFile)
print "Building input data"
s_index=1
corpus = np.array([],dtype=int32)
for line in inputFile :
s_line = line[:-1].split()
sentence = np.empty([len(s_line)],dtype=int32)
w_index = 0 #We begin at the start of line
for w in s_line :
if w in words_dict.keys() :
index = words_dict[w]
else :
index = words_dict['<unk>']
sentence[w_index] = index
w_index+=1
corpus = np.append(corpus,sentence)
s_index+=1
if s_index % 1000 == 0 :
print "Processed line", s_index,"out of", corpus_size
np.save(outFilename,corpus)
pickle.dump(words_dict, outputFile)
outputFile.close()