forked from karpathy/arxiv-sanity-lite
-
Notifications
You must be signed in to change notification settings - Fork 0
/
compute.py
67 lines (53 loc) · 2.39 KB
/
compute.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
"""
Extracts tfidf features from all paper abstracts and saves them to disk.
"""
import argparse
from random import shuffle
import numpy as np
from sklearn.feature_extraction.text import TfidfVectorizer
from aslite.db import get_papers_db, save_features
# -----------------------------------------------------------------------------
if __name__ == '__main__':
parser = argparse.ArgumentParser(description='Arxiv Computor')
parser.add_argument('-n', '--num', type=int, default=20000, help='number of tfidf features')
parser.add_argument('--min_df', type=int, default=5, help='min df')
parser.add_argument('--max_df', type=float, default=0.1, help='max df')
parser.add_argument('--max_docs', type=int, default=-1, help='maximum number of documents to use when training tfidf, or -1 to disable')
args = parser.parse_args()
print(args)
v = TfidfVectorizer(input='content',
encoding='utf-8', decode_error='replace', strip_accents='unicode',
lowercase=True, analyzer='word', stop_words='english',
token_pattern=r'(?u)\b[a-zA-Z_][a-zA-Z0-9_]+\b',
ngram_range=(1, 2), max_features=args.num,
norm='l2', use_idf=True, smooth_idf=True, sublinear_tf=True,
max_df=args.max_df, min_df=args.min_df)
pdb = get_papers_db(flag='r')
def make_corpus(training: bool):
assert isinstance(training, bool)
# determine which papers we will use to build tfidf
if training and args.max_docs > 0 and args.max_docs < len(pdb):
# crop to a random subset of papers
keys = list(pdb.keys())
shuffle(keys)
keys = keys[:args.max_docs]
else:
keys = pdb.keys()
# yield the abstracts of the papers
for p in keys:
d = pdb[p]
author_str = ' '.join([a['name'] for a in d['authors']])
yield ' '.join([d['title'], d['summary'], author_str])
print("training tfidf vectors...")
v.fit(make_corpus(training=True))
print("running inference...")
x = v.transform(make_corpus(training=False)).astype(np.float32)
print(x.shape)
print("saving to features to disk...")
features = {
'pids': list(pdb.keys()),
'x': x,
'vocab': v.vocabulary_,
'idf': v._tfidf.idf_,
}
save_features(features)