From a85c9fc517523f6a5dfbb4084a2836ce4c0be8ce Mon Sep 17 00:00:00 2001 From: crunk Date: Sun, 1 Mar 2020 14:35:50 +0100 Subject: [PATCH] Added words.py and poetry.py plus jinja templates --- poetry.py | 14 ++++++++++++++ search.py | 6 +++--- start.py | 9 ++++++--- templates/index.html | 2 +- templates/manifesto.html | 4 +--- templates/poetry.html | 3 ++- templates/words.html | 10 +++++++++- words.py | 28 ++++++++++++++++++++++++++++ 8 files changed, 64 insertions(+), 12 deletions(-) create mode 100644 poetry.py create mode 100644 words.py diff --git a/poetry.py b/poetry.py new file mode 100644 index 0000000..203d630 --- /dev/null +++ b/poetry.py @@ -0,0 +1,14 @@ +def poetry(): + filename = '2013_Wages_for_Facebook_[EN].txt' + path = './manifestos/'+filename + file = open(path, 'r') + manifesto = file.read() + manifesto = manifesto.replace('\n', ' ') + manifesto = manifesto.lower() + words = manifesto.split(' ') + + return result + +if __name__ == '__main__': + result = poetry() + print('result:', result) diff --git a/search.py b/search.py index da83acf..743b22c 100644 --- a/search.py +++ b/search.py @@ -1,5 +1,5 @@ def search(query): - + query = query.strip() query = query.lower() @@ -20,7 +20,7 @@ def search(query): return result if __name__ == '__main__': - query = 'danny' + query = 'test' result = search(query) print('query:', query) - print('result:', result) \ No newline at end of file + print('result:', result) diff --git a/start.py b/start.py index 7e11f19..98a488b 100644 --- a/start.py +++ b/start.py @@ -3,6 +3,8 @@ import flask from flask import request, render_template from search import * +from poetry import * +from words import * APP = flask.Flask(__name__) @@ -14,7 +16,7 @@ def index(): return render_template('index.html', manifestos=manifestos) @APP.route('/') -def read_manifesto(filename): +def read_manifesto(filename): f = open(folder+filename, 'r') text = f.read() lines = text.split('. ') @@ -28,7 +30,8 @@ def search_manifesto(): @APP.route('/words') def index_words(): - return render_template('words.html') + countedwords = words() + return render_template('words.html', countedwords=countedwords) @APP.route('/poetry') def create_poetry(): @@ -39,4 +42,4 @@ if __name__ == '__main__': APP.run(port=5000) # string = request.args.get('plaintext', '') -# methods=['GET', 'POST'] \ No newline at end of file +# methods=['GET', 'POST'] diff --git a/templates/index.html b/templates/index.html index 10cb372..c8735f9 100644 --- a/templates/index.html +++ b/templates/index.html @@ -8,4 +8,4 @@ {{ manifesto }}
{% endfor%} -{% endblock %} \ No newline at end of file +{% endblock %} diff --git a/templates/manifesto.html b/templates/manifesto.html index 07e34d1..78e82e1 100644 --- a/templates/manifesto.html +++ b/templates/manifesto.html @@ -4,10 +4,8 @@

{{ filename }}

- - {% for line in lines %}
{{ line }}.
{% endfor %} -{% endblock %} \ No newline at end of file +{% endblock %} diff --git a/templates/poetry.html b/templates/poetry.html index 9d00188..f07ab7a 100644 --- a/templates/poetry.html +++ b/templates/poetry.html @@ -2,4 +2,5 @@ {% block main %}

poetry

-{% endblock %} \ No newline at end of file +

{{ result }}

+{% endblock %} diff --git a/templates/words.html b/templates/words.html index c1bdfb5..c4febe9 100644 --- a/templates/words.html +++ b/templates/words.html @@ -2,4 +2,12 @@ {% block main %}

words

-{% endblock %} \ No newline at end of file + + {% for key, value in countedwords.items() %} + + + + + {% endfor %} +
{{ key }}{{ value }}
+{% endblock %} diff --git a/words.py b/words.py new file mode 100644 index 0000000..9b365ab --- /dev/null +++ b/words.py @@ -0,0 +1,28 @@ +from collections import OrderedDict +from operator import itemgetter + +def words(): + filename = '2013_Wages_for_Facebook_[EN].txt' + path = './manifestos/'+filename + file = open(path, 'r') + manifesto = file.read() + manifesto = manifesto.replace('\n', ' ') + manifesto = manifesto.lower() + manifesto = manifesto.strip() + words = manifesto.split(' ') + new_dict = dict() + + for word in words: + if word in new_dict: + # Present so add 1 to the count of word. + new_dict[word] = new_dict[word] + 1 + else: + # Add the word to dictionary with count 1 + new_dict[word] = 1 + + result = OrderedDict(sorted(new_dict.items(), key=itemgetter(1), reverse=True)) + return result + +if __name__ == '__main__': + result = words() + print('result:', result)