-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathapp.py
More file actions
53 lines (45 loc) · 1.59 KB
/
Copy pathapp.py
File metadata and controls
53 lines (45 loc) · 1.59 KB
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
from flask import Flask, request, jsonify
import json
from flask_cors import CORS
from nltk.corpus import wordnet as wn
def get_definition(word):
synsets = wn.synsets(word)
combos = []
for synset in synsets:
lemma, definition, example = synset.lemma_names(), synset.definition(), synset.examples()
if definition == None:
continue
if example == None:
example = ""
combos.append((synset.definition(), synset.examples()))
return combos
def replace_synonyms_and_antonyms(data):
for definitions in data.values():
synonyms = definitions[1].get("Synonyms")
if synonyms:
synonym_definitions = {}
for synonym in synonyms:
synonym_definitions[synonym] = get_definition(synonym)
definitions[1]["Synonyms"] = synonym_definitions
antonyms = definitions[1].get("Antonyms")
if antonyms:
antonym_definitions = {}
for antonym in antonyms:
antonym_definitions[antonym] = get_definition(antonym)
definitions[1]["Antonyms"] = antonym_definitions
app = Flask(__name__)
CORS(app)
@app.route('/add-word', methods=['POST'])
def add_word():
try:
new_data = request.get_json()
replace_synonyms_and_antonyms(new_data)
new_data = ","+json.dumps(new_data, indent=2)[1:]
with open('updated_words.json', 'rb+') as file:
file.seek(-2, 2)
file.write(new_data.encode('utf-8'))
return jsonify(success=True)
except Exception as e:
return jsonify(success=False, error=str(e)+" shit!!")
if __name__ == '__main__':
app.run(debug=True)