Browse Source

Added words.py and poetry.py plus jinja templates

master
crunk 4 years ago
parent
commit
a85c9fc517
  1. 14
      poetry.py
  2. 6
      search.py
  3. 9
      start.py
  4. 2
      templates/index.html
  5. 4
      templates/manifesto.html
  6. 3
      templates/poetry.html
  7. 10
      templates/words.html
  8. 28
      words.py

14
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)

6
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)
print('result:', result)

9
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('/<filename>')
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']
# methods=['GET', 'POST']

2
templates/index.html

@ -8,4 +8,4 @@
<a href="{{ manifesto }}">{{ manifesto }}</a><br>
{% endfor%}
{% endblock %}
{% endblock %}

4
templates/manifesto.html

@ -4,10 +4,8 @@
<h1>{{ filename }}</h1>
<!-- <div>{{ text }}</div> -->
{% for line in lines %}
<div>{{ line }}. </div>
{% endfor %}
{% endblock %}
{% endblock %}

3
templates/poetry.html

@ -2,4 +2,5 @@
{% block main %}
<h1>poetry</h1>
{% endblock %}
<p>{{ result }}</p>
{% endblock %}

10
templates/words.html

@ -2,4 +2,12 @@
{% block main %}
<h1>words</h1>
{% endblock %}
<table>
{% for key, value in countedwords.items() %}
<tr>
<td>{{ key }}</td>
<td>{{ value }}</td>
</tr>
{% endfor %}
</table>
{% endblock %}

28
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)
Loading…
Cancel
Save