-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathapp.py
134 lines (103 loc) · 3.78 KB
/
app.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
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
"""
ye olden luanerizer - a slack slash-command for luanerizing
"""
import string
import os
from flask import Flask, request, jsonify
from cloudevents.http import CloudEvent, to_binary, from_http
import requests
app = Flask(__name__)
NOUNTAGS = set(["PRP", "NNP", "NN", "NNPS", "NNS", "NOUN"])
LUAN = os.environ.get("LUAN", ":luan:")
BROKER_URL = os.environ.get("BROKER_URL", "https://lolol.org")
@app.route("/luanize", methods=["POST"])
def luanize_post_sync():
"""
takes a form-encoded "text" field and returns some json for the slackbot
"""
return jsonify(response(request.form["text"]))
@app.route("/luanize_async", methods=["POST"])
def luanize_post_async():
"""
takes a form-encoded "text" field & response_url and enqueues a event to luanerize the text
"""
attributes = {
"type": "dev.cwlbraa.luanerizer",
"source": request.base_url,
}
data = {"text": request.form["text"], "response_url": request.form["response_url"]}
headers, body = to_binary(CloudEvent(attributes, data))
requests.post(BROKER_URL, data=body, headers=headers)
return jsonify({"response_type": "in_channel"})
@app.route("/", methods=["POST"])
def home():
"""
route for processing cloud event emitted by post_async
"""
event = from_http(request.headers, request.get_data())
requests.post(event.data["response_url"], json=response(event.data["text"]))
return "", 204
@app.route("/healthz", methods=["GET"])
def healthz():
"""
health check
"""
return jsonify({"success": "true"})
def response(text):
"""
returns the json dict with, luanerized text to send to slack
"""
return {"text": luanerize(text), "response_type": "in_channel"}
def luanerize(text):
"""
takes text, finds the nouns, and turns them into luans
"""
nouns = set(find_nouns(text))
def is_noun(chunk):
return chunk.strip(string.punctuation) in nouns
def luanerize_if_noun(word):
return luanerize_word(word) if is_noun(word) else word
space_delimited_lines = [line.split() for line in text.splitlines()]
result = ""
for line in space_delimited_lines:
luanerized_words = map(luanerize_if_noun, line)
result += " ".join(luanerized_words) + "\n"
return result
def find_nouns(text):
"""
returns an iterator over all the nouns in the text
"""
import nltk # pylint: disable=import-outside-toplevel
tagged = nltk.pos_tag(nltk.word_tokenize(text.strip()), tagset="universal")
tagged_nouns = filter(tagged_word_is_noun, tagged)
return map(lambda tagged: tagged[0], tagged_nouns)
def tagged_word_is_noun(word_with_tag):
"""
returns true when a tagged word has a nouny part-of-speech
"""
return word_with_tag[1] in NOUNTAGS
def luanerize_word(word):
"""
replace the lemma (ie the non-modifier part of the word)
with luan
"""
import nltk # pylint: disable=import-outside-toplevel
wnl = nltk.stem.WordNetLemmatizer()
lemma = wnl.lemmatize(word.strip(string.punctuation))
return word.replace(lemma, LUAN)
OZY = """I MET a Traveler from an antique land,
Who said, "Two vast and trunkless legs of stone
Stand in the desert. Near them, on the sand,
Half sunk, a shattered visage lies, whose frown,
And wrinkled lip, and sneer of cold command,
Tell that its sculptor well those passions read,
Which yet survive, stamped on these lifeless things,
The hand that mocked them and the heart that fed:
And on the pedestal these words appear:
"My name is OZYMANDIAS, King of Kings."
Look on my works ye Mighty, and despair
No thing beside remains. Round the decay
Of that Colossal Wreck, boundless and bare,
The lone and level sands stretch far away."""
if __name__ == "__main__":
app.run(debug=True, host="0.0.0.0", port=int(os.getenv("PORT", "5000")))