miyachun's picture
Update app.py
bcbb702
import spacy
import gradio as gr
import json
nlp = spacy.load('zh_core_web_md')
data = open('F-C0032-001.json',"r",encoding="utf-8")
output = json.load(data)
location=output['records']['location']
ansA=[]
def getFinal(city_n):
for i in location:
city = i['locationName']
if city==city_n:
wx = i['weatherElement'][0]['time'][0]['parameter']['parameterName']
maxtT = i['weatherElement'][4]['time'][0]['parameter']['parameterName']
mintT = i['weatherElement'][2]['time'][0]['parameter']['parameterName']
ansA.append(city)
ansA.append(wx)
ansA.append(mintT)
ansA.append(maxtT)
return ansA
def appMain(sentence):
statement1 = nlp("想查詢哪個城市的天氣?")
statement2 = nlp("想詢問的城市"+sentence)
#print(statement1.similarity(statement2))
ansA.clear()
if statement1.similarity(statement2) >= 0.80:
for ent in statement2.ents:
if ent.label_ == "GPE":
sentence = ent.text
if sentence=='台北' or sentence=='臺北'or sentence=='臺北市':
getFinal('臺北市')
return ansA
elif sentence=='新北' or sentence=='新北市':
getFinal('新北市')
return ansA
elif sentence=='嘉義' or sentence=='嘉義市':
getFinal('嘉義市')
return ansA
elif sentence=='新竹' or sentence=='新竹市':
getFinal('新竹市')
return ansA
elif sentence=='新竹縣':
getFinal('新竹縣')
return ansA
elif sentence=='台南' or sentence=='臺南'or sentence=='臺南市':
getFinal('臺南市')
return ansA
else:
return '-查無資料-'
else:
return '-查無資料-'
title = "Spacy Similarity"
desc = "目前設定->(台北、新北、嘉義、新竹、台南)"
demo = gr.Interface(fn=appMain, title=title, inputs="text", description=desc, outputs="text")
demo.launch()
if __name__ == "__main__":
demo.launch(share=True)