added a few extra 'views'
This commit is contained in:
parent
94e41bde90
commit
602115b755
17149
index.json
17149
index.json
File diff suppressed because it is too large
Load Diff
47
start.py
47
start.py
@ -10,8 +10,16 @@ def get_index():
|
||||
return index
|
||||
|
||||
def get_results(query):
|
||||
results, index = tfidf.request_results(query)
|
||||
return results, index
|
||||
results, files = tfidf.request_results(query)
|
||||
return results, files
|
||||
|
||||
def get_ordered():
|
||||
results, files = tfidf.request_ordered()
|
||||
return results, files
|
||||
|
||||
def get_ordered_all():
|
||||
results, files = tfidf.request_ordered_all()
|
||||
return results, files
|
||||
|
||||
# Create the application.
|
||||
APP = flask.Flask(__name__)
|
||||
@ -25,14 +33,45 @@ def index():
|
||||
|
||||
if request.args.get('q', ''):
|
||||
query = request.args.get('q', '')
|
||||
results, index = get_results(query)
|
||||
files = [manifesto for manifesto, _ in index.items()]
|
||||
results, files = get_results(query)
|
||||
return flask.render_template('results.html', query=query, results=results, files=files)
|
||||
else:
|
||||
index = get_index()
|
||||
files = [manifesto for manifesto, _ in index.items()]
|
||||
return flask.render_template('index.html', files=files)
|
||||
|
||||
@APP.route('/lists', methods=['GET', 'POST'])
|
||||
def lists():
|
||||
""" Displays the index page accessible at '/lists'
|
||||
"""
|
||||
query = None
|
||||
results = None
|
||||
|
||||
if request.args.get('q', ''):
|
||||
query = request.args.get('q', '')
|
||||
results, index = get_results(query)
|
||||
files = [manifesto for manifesto, _ in index.items()]
|
||||
return flask.render_template('results_lists.html', query=query, results=results, files=files)
|
||||
else:
|
||||
index = get_index()
|
||||
files = [manifesto for manifesto, _ in index.items()]
|
||||
return flask.render_template('index.html', files=files)
|
||||
|
||||
@APP.route('/ordered', methods=['GET', 'POST'])
|
||||
def ordered():
|
||||
results, files = get_ordered()
|
||||
return flask.render_template('ordered.html', files=files, results=results)
|
||||
|
||||
@APP.route('/ordered/all', methods=['GET', 'POST'])
|
||||
def ordered_all():
|
||||
results, files = get_ordered_all()
|
||||
return flask.render_template('ordered.all.html', files=files, results=results)
|
||||
|
||||
# @APP.route('/ordered.all', methods=['GET', 'POST'])
|
||||
# def ordered():
|
||||
# results, files = get_ordered_all()
|
||||
# return flask.render_template('ordered.all.html', files=files, results=results)
|
||||
|
||||
if __name__ == '__main__':
|
||||
if not 'index.json' in os.listdir('.'):
|
||||
tfidf.create_index()
|
||||
|
45
tfidf.py
45
tfidf.py
@ -1,4 +1,4 @@
|
||||
import os, json
|
||||
import os, json, re
|
||||
from math import log, exp
|
||||
from flask import Markup
|
||||
|
||||
@ -146,23 +146,22 @@ def request_results(query):
|
||||
# make a list of sentences that contain the query word
|
||||
# and shape results object
|
||||
for x, manifesto in results.items():
|
||||
sents = sentences[manifesto['name']]
|
||||
value = manifesto['tfidf'] * 50000
|
||||
result_sentences = []
|
||||
count = 0
|
||||
# count = 0
|
||||
for s in manifesto['sentences']:
|
||||
done = 'no'
|
||||
for word in tokenizer.tokenize(s):
|
||||
if word == query:
|
||||
if count < 3: # set to include a max 3 results/manifesto in the results list
|
||||
count += 1
|
||||
if done is not 'yes':
|
||||
sentence = s.replace(query, '<strong style="font-size:{}%;">{}</strong>'.format(100 + value, query))
|
||||
html = Markup(sentence)
|
||||
if count == 3:
|
||||
html = html + Markup('<div id="more">(...)<sup>*</sup></div>')
|
||||
result_sentences.append(html)
|
||||
done = 'yes'
|
||||
# if count < 3: # set to include a max 3 results/manifesto in the results list
|
||||
# count += 1
|
||||
if done is not 'yes':
|
||||
sentence = re.sub(r'[ .,;/\\*]'+query+r'[ ,.;/\\*]', '<strong style="font-size:{}%;"> {} </strong>'.format(100 + value, query), s)
|
||||
html = Markup(sentence)
|
||||
# if count == 3:
|
||||
# html = html + Markup('<div id="more">(...)<sup>*</sup></div>')
|
||||
result_sentences.append(html)
|
||||
done = 'yes'
|
||||
results[x]['sentences'] = result_sentences
|
||||
|
||||
print('*results returned*')
|
||||
@ -178,13 +177,15 @@ def request_ordered():
|
||||
results[manifesto] = words
|
||||
return results, files
|
||||
|
||||
# def request_ordered_all():
|
||||
# f = open('index.json').read()
|
||||
# index = json.loads(f)
|
||||
# files = [manifesto for manifesto, _ in index.items()]
|
||||
# results = []
|
||||
# i = 0
|
||||
# for manifesto, _ in index.items():
|
||||
# i += 1
|
||||
# [value, word, i] for word, value in index[manifesto]['words'].items()
|
||||
# return results, files
|
||||
def request_ordered_all():
|
||||
f = open('index.json').read()
|
||||
index = json.loads(f)
|
||||
files = [manifesto for manifesto, _ in index.items()]
|
||||
results = []
|
||||
i = 0
|
||||
for manifesto, _ in index.items():
|
||||
for word, value in index[manifesto]['words'].items():
|
||||
results.append([value, word, i])
|
||||
i += 1
|
||||
results = sorted(results)
|
||||
return results, files
|
||||
|
Loading…
Reference in New Issue
Block a user