Skip to content

Commit b72caf3

Browse files
committed
Add view with clean text function
0 parents  commit b72caf3

File tree

4 files changed

+162
-0
lines changed

4 files changed

+162
-0
lines changed

Pipfile

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
[[source]]
2+
name = "pypi"
3+
url = "https://pypi.org/simple"
4+
verify_ssl = true
5+
6+
[dev-packages]
7+
8+
[packages]
9+
flask = "*"
10+
nltk = "*"
11+
black = "*"
12+
13+
[requires]
14+
python_version = "3.7"

Pipfile.lock

Lines changed: 104 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

__init__.py

Whitespace-only changes.

app.py

Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,44 @@
1+
import json
2+
3+
from flask import Flask
4+
from flask import request, jsonify
5+
from flask.views import MethodView
6+
7+
from utils import STOP_WORDS
8+
9+
app = Flask(__name__)
10+
11+
12+
def clean_text(text):
13+
unique_word_list = set(re.sub(r'\s*\b(\d+\w*)', '', ''.join(text).lower()).translate(str.maketrans('', '', string.punctuation)).split())
14+
filtered_unique_words_list = set([i for i in unique_word_list if i not in STOP_WORDS])
15+
phrases_list = [re.sub(r'\s*\b(\d+\w*)['+string.punctuation+ ']', ' ', ''.join(i).lower()).split() for i in text]
16+
17+
return phrases_list, sorted(filtered_unique_words_list)
18+
19+
20+
class WordListView(MethodView):
21+
22+
def post(self):
23+
text_data_dict = json.loads(request.data)
24+
phrases_list, unique_word_list = clean_text(text_data_dict['texts'])
25+
word_counter = {}
26+
27+
for index, phrase in enumerate(phrases_list):
28+
word_counter[index] = ([phrase.count(word) for word in unique_word_list])
29+
30+
31+
json_data = {
32+
"unique_words": unique_word_list,
33+
"word_count": word_counter
34+
}
35+
return jsonify(json_data)
36+
37+
38+
39+
word_list_view = WordListView.as_view('word_list_view')
40+
app.add_url_rule(
41+
'/', view_func=word_list_view, methods=['POST']
42+
)
43+
if __name__ == '__main__':
44+
app.run(host='127.0.0.1', port='5000', debug=True)

0 commit comments

Comments
 (0)