-
Notifications
You must be signed in to change notification settings - Fork 8
/
wordfish.py
64 lines (51 loc) · 2.13 KB
/
wordfish.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
from helpers import io_helper
from wfcode import corpus
from wfcode import scaler
import argparse
import os
from datetime import datetime
parser = argparse.ArgumentParser(description='Trains a model for classifying lexico-semantic relations.')
parser.add_argument('datadir', help='A path to the directory containing the input text files for scaling (one score will be assigned per file).')
parser.add_argument('output', help='A file path to which to store the scaling results.')
parser.add_argument('--stopwords', help='A file to the path containing stopwords')
parser.add_argument('-f', '--freqthold', type=int, help='A frequency threshold -- all words appearing less than -ft times will be ignored (default 2)')
parser.add_argument('-l', '--learnrate', type=float, help='Learning rate value (default = 0.00001)')
parser.add_argument('-t', '--trainiters', type=int, help='Number of optimization iterations (default = 5000)')
args = parser.parse_args()
if args.trainiters:
niter = args.trainiters
else:
niter = 5000
if args.learnrate:
lr = args.learnrate
else:
lr = 0.00001
if args.freqthold:
ft = args.freqthold
else:
ft = 2
if not os.path.isdir(os.path.dirname(args.datadir)):
print("Error: Directory containing the input files not found.")
exit(code = 1)
if not os.path.isdir(os.path.dirname(args.output)) and not os.path.dirname(args.output) == "":
print("Error: Directory of the output file does not exist.")
exit(code = 1)
if args.stopwords and not os.path.isfile(args.stopwords):
print("Error: File containing stopwords not found.")
exit(code = 1)
if args.stopwords:
stopwords = io_helper.load_file_lines(args.stopwords)
else:
stopwords = None
files = io_helper.load_all_files(args.datadir)
corp = corpus.Corpus(files)
corp.tokenize(stopwords = stopwords, freq_treshold = ft)
corp.build_occurrences()
wf_scaler = scaler.WordfishScaler(corp)
wf_scaler.initialize()
wf_scaler.train(learning_rate = lr, num_iters = niter)
print(datetime.now().strftime('%Y-%m-%d %H:%M:%S') + " WordFish scaling completed.", flush = True)
scale = []
for x in corp.results:
scale.append(str(x) + "\t" + str(corp.results[x]))
io_helper.write_list(args.output, scale)