|
| 1 | +#!/usr/bin/env python |
| 2 | +# -*- coding: UTF-8 -*- |
| 3 | + |
| 4 | +import os |
| 5 | +import uuid |
| 6 | +import random |
| 7 | +import shutil |
| 8 | +from concurrent.futures import ThreadPoolExecutor |
| 9 | +from flask import Flask, jsonify |
| 10 | + |
| 11 | +app = Flask(__name__) |
| 12 | + |
| 13 | + |
| 14 | +def validate(word, sentence): |
| 15 | + return word in sentence |
| 16 | + |
| 17 | + |
| 18 | +def generate_article(): |
| 19 | + s_nouns = [ |
| 20 | + "A dude", "My mom", "The king", "Some guy", "A cat with rabies", |
| 21 | + "A sloth", "Your homie", "This cool guy my gardener met yesterday", |
| 22 | + "Superman" |
| 23 | + ] |
| 24 | + p_nouns = [ |
| 25 | + "These dudes", "Both of my moms", "All the kings of the world", |
| 26 | + "Some guys", "All of a cattery's cats", |
| 27 | + "The multitude of sloths living under your bed", "Your homies", |
| 28 | + "Like, these, like, all these people", "Supermen" |
| 29 | + ] |
| 30 | + s_verbs = [ |
| 31 | + "eats", "kicks", "gives", "treats", "meets with", "creates", "hacks", |
| 32 | + "configures", "spies on", "retards", "meows on", "flees from", |
| 33 | + "tries to automate", "explodes" |
| 34 | + ] |
| 35 | + infinitives = [ |
| 36 | + "to make a pie.", "for no apparent reason.", |
| 37 | + "because the sky is green.", "for a disease.", |
| 38 | + "to be able to make toast explode.", "to know more about archeology." |
| 39 | + ] |
| 40 | + sentence = '{} {} {} {}'.format( |
| 41 | + random.choice(s_nouns), random.choice(s_verbs), |
| 42 | + random.choice(s_nouns).lower() or random.choice(p_nouns).lower(), |
| 43 | + random.choice(infinitives)) |
| 44 | + return '\n'.join([sentence for i in range(50000)]) |
| 45 | + |
| 46 | + |
| 47 | +@app.route('/') |
| 48 | +def hello_world(): |
| 49 | + return 'hello world' |
| 50 | + |
| 51 | + |
| 52 | +@app.route("/popularity/<word>") |
| 53 | +def word_popularity(word): |
| 54 | + dir_path = '/tmp/{}'.format(uuid.uuid1()) |
| 55 | + count = 0 |
| 56 | + sample_size = 1000 |
| 57 | + |
| 58 | + def save_to_file(file_name, content): |
| 59 | + with open(file_name, 'w') as f: |
| 60 | + f.write(content) |
| 61 | + |
| 62 | + try: |
| 63 | + # initial directory firstly |
| 64 | + os.mkdir(dir_path) |
| 65 | + |
| 66 | + # save article to files |
| 67 | + for i in range(sample_size): |
| 68 | + file_name = '{}/{}.txt'.format(dir_path, i) |
| 69 | + article = generate_article() |
| 70 | + save_to_file(file_name, article) |
| 71 | + |
| 72 | + # count word popularity |
| 73 | + for root, dirs, files in os.walk(dir_path): |
| 74 | + for file_name in files: |
| 75 | + with open('{}/{}'.format(dir_path, file_name)) as f: |
| 76 | + if validate(word, f.read()): |
| 77 | + count += 1 |
| 78 | + finally: |
| 79 | + # clean files |
| 80 | + shutil.rmtree(dir_path, ignore_errors=True) |
| 81 | + |
| 82 | + return jsonify({'popularity': count / sample_size * 100, 'word': word}) |
| 83 | + |
| 84 | + |
| 85 | +@app.route("/popular/<word>") |
| 86 | +def word_popular(word): |
| 87 | + count = 0 |
| 88 | + sample_size = 1000 |
| 89 | + articles = [] |
| 90 | + |
| 91 | + try: |
| 92 | + for i in range(sample_size): |
| 93 | + articles.append(generate_article()) |
| 94 | + |
| 95 | + for article in articles: |
| 96 | + if validate(word, article): |
| 97 | + count += 1 |
| 98 | + finally: |
| 99 | + pass |
| 100 | + |
| 101 | + return jsonify({'popularity': count / sample_size * 100, 'word': word}) |
| 102 | + |
| 103 | + |
| 104 | +if __name__ == '__main__': |
| 105 | + app.run(debug=True, host='0.0.0.0', port=80) |
0 commit comments