-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.py
117 lines (107 loc) · 3.86 KB
/
main.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
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
from flask import Flask, render_template, request
import os
import requests
import json
from utils import vectorize_text
from utils import similarity
from utils import correlate_text
from cluster import group_sentences
from utils import correlate_text
from articles import Article
from utils import create_title
app = Flask(__name__)
@app.route('/', methods=["POST","GET"])
def generate_article():
keywords = request.form.get("topic")
if keywords == None:
return render_template("home.html")
else:
keywords = keywords.split(" ")
kwords = []
for word in keywords:
kwords.append(word.lower())
keywords = kwords
articles = []
for file in os.listdir("articles/"):
if file.endswith(".txt"):
text = open(os.path.join("articles/", file), "r").read()
source = file[:file.index("-")]
articles.append(Article(text, source))
weighted_articles = []
for art in articles:
weighted_articles.append((similarity(art.vector, keywords), art))
weighted_articles = sorted(weighted_articles, key=lambda x: -x[0])
temp = []
for pair in weighted_articles:
if pair[0] > 0:
temp.append(pair)
weighted_articles = temp
if len(weighted_articles) >= 3:
model = weighted_articles[0:3]
else:
model = weighted_articles
articles = []
for pair in model:
art = pair[1]
articles.append(art)
generated_article, sources = group_sentences(articles)
title = ""
art_text = ""
for sentence in generated_article:
art_text += sentence[0] + " "
if len(generated_article) > 0:
title = create_title(art_text)
else:
title = "Sorry, we couldn't find any related articles!"
#generate the text and display some how
tit_text = title.decode('utf8')
art_text = art_text.decode('utf8')
return render_template("home.html", title=tit_text, article=art_text)
@app.route('/post', methods=["POST","GET"])
def generate_article_slack():
keywords = request.form.get("text")
if keywords == None:
return render_template("home.html")
else:
keywords = keywords.split(" ")
kwords = []
for word in keywords:
kwords.append(word.lower())
articles = []
for file in os.listdir("articles/"):
if file.endswith(".txt"):
text = open(os.path.join("articles/", file), "r").read()
source = file[:file.index("-")]
articles.append(Article(text, source))
weighted_articles = []
for art in articles:
weighted_articles.append((similarity(art.vector, keywords), art))
weighted_articles = sorted(weighted_articles, key=lambda x: -x[0])
temp = []
for pair in weighted_articles:
if pair[0] > 0:
temp.append(pair)
weighted_articles = temp
if len(weighted_articles) >= 3:
model = weighted_articles[0:3]
else:
model = weighted_articles
articles = []
for pair in model:
art = pair[1]
articles.append(art)
generated_article, sources = group_sentences(articles)
title = ""
art_text = ""
for sentence in generated_article:
art_text += sentence[0] + " "
if len(generated_article) > 0:
title = create_title(art_text)
else:
title = "Sorry, we couldn't find any related articles!"
#generate the text and display some how
tit_text = title.decode('utf8')
art_text = art_text.decode('utf8')
return tit_text + ": "+ art_text
if __name__ == '__main__':
app.run(host="127.0.0.1", port=8080, debug=True)