-
Notifications
You must be signed in to change notification settings - Fork 0
/
newsreader.py
69 lines (49 loc) · 1.64 KB
/
newsreader.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
from typing import Dict
import pandas as pd
from auxfunctions import all_news_filereader, read_input
from auxfunctions import parse_input
from auxfunctions import ranker
from td import Model
from td import Tokenizer
def build_models(df: pd.DataFrame, usecols: list) -> Dict:
"""
For each column build a model.
Adds each model to a dictionary
:param df: original dataframe with all data
:param usecols: columns to build a model
:return: dictionary with models for each column
"""
models = {}
tokenizer = Tokenizer()
for col in usecols:
print(f'Tokenizing and building model for {col}...')
model = Model(tokenizer)
corpus = df[col].astype('unicode').apply(lambda x: model.build_indexes(x))
model.build_similarity(list(corpus), model='tfidf')
models[col] = model
return models
def main(filename):
# read file
df = all_news_filereader(filename)
# df = df[0:1000]
models = build_models(df, usecols=['title', 'publication', 'author', 'content'])
# prettier print
pd.set_option('display.max_rows', 500)
pd.set_option('display.max_columns', 500)
pd.set_option('display.width', 1000)
while True:
s = read_input()
parsed_query = parse_input(s)
results = ranker(df, models, parsed_query)
print(results)
print()
if __name__ == "__main__":
try:
import sys
if len(sys.argv) != 2:
print('Usage: python newsreader.py <filename>')
exit(1)
# collect filename to read from arguments
main(sys.argv[1])
except KeyboardInterrupt:
print('exiting')