from flask import Flask, render_template, request, redirect, url_for import os import json from datetime import datetime import pytz import random from gtts import gTTS import requests # from nltk.corpus import wordnet as wn from nltk.tokenize import SyllableTokenizer from nltk import word_tokenize import requests from bs4 import BeautifulSoup app = Flask(__name__) datap = "" app = Flask(__name__) text = None str1='' la='' token=[] collec=[] bunch=[] @app.route("/dictionary/") def wordnet(): with open("database.json", "r") as f: #close automatically json_file = f.read() database = json.loads(json_file) # bunch = list(chosenword.keys()) if request.args.get("word"): response = None clicked_word = request.args.get("word") SSP = SyllableTokenizer() if clicked_word in database: response = database[clicked_word] stories = response["stories"] a=stories[0]["A"] b=stories[0]["B"] word1 = SSP.tokenize(a) word2 = SSP.tokenize(b) remove1 = [] remove2 = [] pick='' what = ["noun", "verb", "adjective", "adverb", "conjunction", "pronoun", "preposition", "exclamation", "determiner"] pick='(potentially) ' + random.choice(what) for i in range(len(word1)): print(i) print(word1[i]) if word1[i] in word2: remove1.append(word1[i]) remove2.append(word1[i]) print("remove1", remove1) print("remove2", remove2) for j in remove1: word1.remove(j) for k in remove1: word2.remove(k) print("word1", word1) print("word2", word2) if not len(word1)==0: frab1=word1[0] else: frab1="" if not len(word2)==0: frab2=word2[-1] else: frab2="" frab = frab1 + frab2 return render_template("sh_response.html", clicked_word = clicked_word, database = database, stories=stories, a=a, b=b, word1=word1, word2=word2, frab=frab, pick=pick) else: return render_template("sh_first.html", database = database) @app.route('/game2/', methods =['GET', 'POST']) def game2(): with open("database.json", "r") as word: #close automatically json_file = word.read() chosenword = json.loads(json_file) if request.method == 'GET': bunch = list(chosenword.keys()) ranword = random.choice(bunch) response = None print(ranword) if ranword in chosenword: response = chosenword[ranword] stories = response["stories"] print(stories) a=stories[0]["A"] b=stories[0]["B"] print(a, b) frab=stories[0]["frab"] example=stories[0]["example"] example2=stories[0]["example2"] return render_template("sh_secondgame1.html", stories=stories, example=example, frab=frab, ranword=ranword, example2=example2, a=a, b=b) else: frab = request.args.get("word") a= request.args.get("a") b= request.args.get("b") guess1 = request.form.get('guess1') guess2 = request.form.get('guess2') SSP = SyllableTokenizer() word1 = SSP.tokenize(guess1) word2 = SSP.tokenize(guess2) remove1 = [] remove2 = [] frab1="" frab2="" for i in range(len(word1)): print(i) print(word1[i]) if word1[i] in word2: remove1.append(word1[i]) remove2.append(word1[i]) print("remove1", remove1) print("remove2", remove2) for j in remove1: word1.remove(j) for k in remove1: word2.remove(k) print("word1", word1) print("word2", word2) # word2.remove(word1[i]) # word1.remove(word1[i]) if not len(word1)==0: frab1=word1[0] else: frab1="" if not len(word2)==0: frab2=word2[-1] else: frab2="" newfrab = frab1 + frab2 print(a, b) return render_template("sh_secondgame2.html", guess1=guess1, guess2=guess2, frab1=frab1, frab2=frab2, newfrab=newfrab, frab=frab, a=a, b=b) @app.route('/translation/', methods =['GET', 'POST']) def trad(): if os.path.exists("voice.mp3"): os.remove("voice.mp3") text = None str1='' la='' token=[] collec=[] mw='' mw2=[] text2 = '' with open("database.json", "r") as word: #close automatically json_file = word.read() data = json.loads(json_file) for i in range(len(data)): first=list(data.values())[i]['stories'][0] mw = mw + first['A'] + ", " + first['B'] + ", " if ' ' in first['A']: mw2.append(first['A']) if ' ' in first['B']: mw2.append(first['B']) i+=1 if request.args.get('text'): text = request.args.get('text').lower() first={} for i in mw2: if i in text: i2 = i.replace(' ', '_') text = text.replace(i, i2) token = word_tokenize(text) for i in range(len(data)): first=list(data.values())[i]['stories'][0] for j in range(len(token)): if '_' not in token[j]: if token[j]==first['A'] or token[j]==first['B']: token[j]=first['frab'] collec.append(token[j]) elif '_' in token[j]: token[j]=token[j].replace('_', ' ') if token[j]==first['A'] or token[j]==first['B']: token[j]=first['frab'] collec.append(token[j]) i+=1 print(token) str1 = ' '.join(token) lan=["af", "ar", "bg", "bn", "bs", "ca", "cs", "da", "de", "el", "en", "es", "et", "fi", "fr", "gu", "hi", "hr", "hu", "id", "is", "it", "ja", "jw", "km", "kn", "ko", "la", "lv", "ml", "mr", "my", "ne", "nl", "no", "pl", "pt", "ro", "ru", "si", "sk", "sq", "sr", "su", "sv", "sw", "ta", "te", "th", "tl", "tr", "uk", "ur", "vi", "zh"] la=random.choice(lan) print(la) tts = gTTS(str1, lang=la, tld='com.au') tts.save('voice.mp3') return render_template('sh_transform.html', data=data, text=text, str1=str1, collec=collec, token=token, mw=mw) @app.route('/submit/', methods =['GET','POST']) def register(): if request.method == 'GET': return render_template("sh_submit.html") else: d={} tagtag='' tagtag2='' example="" time="" tz_Amsterdam = pytz.timezone('Europe/Amsterdam') datetime_Amsterdam = datetime.now(tz_Amsterdam) time=datetime_Amsterdam.strftime("%d/%m/%Y %H:%M:%S") + "(Amsterdam time)" name = request.form.get('name') w1 = request.form.get('w1').lower() w2 = request.form.get('w2').lower() story = request.form.get('story') d['User']=name d['A']=w1 d['B']=w2 d['story']=story d['Time']=time #Making Frabjousish word SSP = SyllableTokenizer() word1 = SSP.tokenize(w1) word2 = SSP.tokenize(w2) remove1 = [] remove2 = [] print("word1", word1, len(word1), type(word1)) print("word2", word2, len(word2), type(word2)) # word1 ['screw', 'dri', 'ver'] # word2 ['dri', 'ver'] for i in range(len(word1)): print(i) print(word1[i]) if word1[i] in word2: remove1.append(word1[i]) remove2.append(word1[i]) print("remove1", remove1) print("remove2", remove2) for j in remove1: word1.remove(j) for k in remove1: word2.remove(k) print("word1", word1) print("word2", word2) # word2.remove(word1[i]) # word1.remove(word1[i]) if not len(word1)==0: frab1=word1[0] else: frab1="" if not len(word2)==0: frab2=word2[-1] else: frab2="" frab = frab1 + frab2 d['frab']=frab #example line (with wordnet -> not enough examples :() # if wn.synsets(w1): # selec='' # line = wn.synsets(w1)[0].examples() # #check if the example key includes the word # for x in line: # if w1 in x: # selec=x # break # else: # selec = selec # line2 = selec.replace(w1, frab) # d['example'] = line2 # else: # d['example'] = '' #example line with beautifulsoup w3='' if ' ' in w1: w3=w1.replace(" ","%20") else: w3=w1 url = "https://lengusa.com/sentence-examples/" + w3 example = requests.get(url) html = example.content try: scraped = BeautifulSoup(html, 'html.parser') p_tag = scraped.find_all("p", class_="text-black mt-1 font-serif")[0].get_text() p_tag2 = scraped.find_all("p", class_="text-black mt-1 font-serif")[1].get_text() p_tag3 = scraped.find_all("p", class_="text-black mt-1 font-serif")[2].get_text() if w1 in p_tag: tagtag = p_tag.replace(w1, frab) d['example']=tagtag elif w1.capitalize() in p_tag: tagtag = p_tag.replace(w1.capitalize(), frab) d['example']=tagtag elif w1 in p_tag2: tagtag = p_tag2.replace(w1, frab) d['example']=tagtag elif w1.capitalize() in p_tag2: tagtag = p_tag2.replace(w1.capitalize(), frab) d['example']=tagtag elif w1 in p_tag3: tagtag = p_tag3.replace(w1, frab) d['example']=tagtag elif w1.capitalize() in p_tag3: tagtag = p_tag3.replace(w1.capitalize(), frab) d['example']=tagtag else: d['example']='' except Exception as e: print("1st", e,"word is not found!") d['example']='' #second example line with beautifulsoup w4='' if ' ' in w2: w4=w2.replace(" ","%20") else: w4=w2 url2 = "https://lengusa.com/sentence-examples/" + w4 example2 = requests.get(url2) html2 = example2.content try: scraped2 = BeautifulSoup(html2, 'html.parser') p_tag4 = scraped2.find_all("p", class_="text-black mt-1 font-serif")[0].get_text() p_tag5 = scraped2.find_all("p", class_="text-black mt-1 font-serif")[1].get_text() p_tag6 = scraped2.find_all("p", class_="text-black mt-1 font-serif")[2].get_text() if w2 in p_tag4: tagtag2 = p_tag4.replace(w2, frab) d['example2']=tagtag2 elif w2.capitalize() in p_tag4: tagtag2 = p_tag4.replace(w2.capitalize(), frab) d['example2']=tagtag2 elif w2 in p_tag5: tagtag2 = p_tag5.replace(w2, frab) d['example2']=tagtag2 elif w2.capitalize() in p_tag5: tagtag2 = p_tag5.replace(w2.capitalize(), frab) d['example2']=tagtag2 elif w2 in p_tag6: tagtag2 = p_tag6.replace(w2, frab) d['example2']=tagtag2 elif w2.capitalize() in p_tag6: tagtag2 = p_tag6.replace(w2.capitalize(), frab) d['example2']=tagtag2 else: d['example2']='' except Exception as e: print("2nd", e ,"word is not found!") d['example2']='' if not (name and w1 and w2 and story and time) : return "Please write everyting" else: with open("database.json", "r+") as jsonFile: data = json.load(jsonFile) if frab not in data: data[frab] = { "stories" : [] } data[frab]["stories"].append(d) #data.update(set) with open("database.json", "w") as jsonFile: json.dump(data, jsonFile, indent=2) #shift + tap -> further function appears return render_template("sh_thanks.html") @app.route('/game_end/') def end(): return render_template("sh_thanks2.html") @app.route('/game/') def gamemenu(): return render_template("sh_g.html") if __name__ == "__main__": app.run(debug=True, port=5006)