From 98aa542556fd852e8860d5fb05487f47aa2b00d5 Mon Sep 17 00:00:00 2001 From: PeiYuan Tsai Date: Wed, 7 Dec 2022 22:31:31 +0800 Subject: [PATCH] =?UTF-8?q?Create=2004=5F=F0=9F=8C=AESpanish.py,=201st=20v?= =?UTF-8?q?ersion=20with=20corpus?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .DS_Store | Bin 0 -> 6148 bytes README.md | 1 + app.py | 4 +- es_vocab.csv | 1001 +++++++++++++++++++++++++ models.json | 3 +- "pages/04_\360\237\214\256Spanish.py" | 218 ++++++ requirements.txt | 1 + 7 files changed, 1225 insertions(+), 3 deletions(-) create mode 100644 .DS_Store create mode 100644 es_vocab.csv create mode 100644 "pages/04_\360\237\214\256Spanish.py" diff --git a/.DS_Store b/.DS_Store new file mode 100644 index 0000000000000000000000000000000000000000..51707bc4eafe75f6d920425142e71d07f06b518f GIT binary patch literal 6148 zcmeH~J&pn~427RrkdU^a+%gRZ;06(5PrwD(1w@0yN@)8WooB}l12r0r2wyw<3^dk@$ KG>E`U3ETkUhZBha literal 0 HcmV?d00001 diff --git a/README.md b/README.md index dea2505..1154e96 100644 --- a/README.md +++ b/README.md @@ -5,3 +5,4 @@ This repo hosts a [Streamlit Web APP](https://share.streamlit.io/howard-haowen/s - Mandarin - English - Japanese +- Spanish diff --git a/app.py b/app.py index 505fd6f..64ef540 100644 --- a/app.py +++ b/app.py @@ -6,8 +6,8 @@ ## Language Learning Assisted by AI Models -- 開啟左側選單可選擇語言,目前支援華語、日語和英語。 - - Select a language from the sidebar. Supported languages include Mandarin, Japanese, and English. +- 開啟左側選單可選擇語言,目前支援華語、日語、英語和西語。 + - Select a language from the sidebar. Supported languages include Mandarin, Japanese, English, and Spanish. - 選單自動隱藏時,點選左上角 > 符號以開啟選單。 - If the sidebar is hidden, click on the > symbol in the upper left corner to open it. diff --git a/es_vocab.csv b/es_vocab.csv new file mode 100644 index 0000000..b6109d9 --- /dev/null +++ b/es_vocab.csv @@ -0,0 +1,1001 @@ +es,en +como,as +I,I +su,his +que,that +él,he +era,was +para,for +en,on +son,are +con,with +ellos,they +ser,be +en,at +uno,one +tener,have +este,this +desde,from +por,by +caliente,hot +palabra,word +pero,but +qué,what +algunos,some +es,is +lo,it +usted,you +o,or +tenido,had +la,the +de,of +a,to +y,and +un,a +en,in +nos,we +lata,can +fuera,out +otros,other +eran,were +que,which +hacer,do +su,their +tiempo,time +si,if +lo hará,will +cómo,how +dicho,said +un,an +cada,each +decir,tell +hace,does +conjunto,set +tres,three +querer,want +aire,air +así,well +también,also +jugar,play +pequeño,small +fin,end +poner,put +casa,home +leer,read +mano,hand +puerto,port +grande,large +deletrear,spell +añadir,add +incluso,even +tierra,land +aquí,here +debe,must +grande,big +alto,high +tal,such +siga,follow +acto,act +por qué,why +preguntar,ask +hombres,men +cambio,change +se fue,went +luz,light +tipo,kind +fuera,off +necesitará,need +casa,house +imagen,picture +tratar,try +nosotros,us +de nuevo,again +animal,animal +punto,point +madre,mother +mundo,world +cerca,near +construir,build +auto,self +tierra,earth +padre,father +cualquier,any +nuevo,new +trabajo,work +parte,part +tomar,take +conseguir,get +lugar,place +hecho,made +vivir,live +donde,where +después,after +espalda,back +poco,little +sólo,only +ronda,round +hombre,man +año,year +vino,came +show,show +cada,every +buena,good +me,me +dar,give +nuestro,our +bajo,under +nombre,name +muy,very +a través de,through +sólo,just +forma,form +frase,sentence +gran,great +pensar,think +decir,say +ayudar,help +bajo,low +línea,line +ser distinto,differ +"a su vez,",turn +causa,cause +mucho,much +significará,mean +antes,before +movimiento,move +derecho,right +niño,boy +viejo,old +demasiado,too +misma,same +ella,she +todo,all +hay,there +cuando,when +hasta,up +uso,use +su,your +camino,way +acerca,about +muchos,many +entonces,then +ellos,them +escribir,write +haría,would +como,like +así,so +éstos,these +su,her +largo,long +hacer,make +cosa,thing +ver,see +él,him +dos,two +tiene,has +buscar,look +más,more +día,day +podía,could +ir,go +venir,come +hizo,did +número,number +sonar,sound +no,no +más,most +personas,people +mi,my +sobre,over +saber,know +agua,water +que,than +llamada,call +primero,first +que,who +puede,may +abajo,down +lado,side +estado,been +ahora,now +encontrar,find +cabeza,head +de pie,stand +propio,own +página,page +debería,should +país,country +encontrado,found +respuesta,answer +escuela,school +crecer,grow +estudio,study +todavía,still +aprender,learn +planta,plant +cubierta,cover +alimentos,food +sol,sun +cuatro,four +entre,between +estado,state +mantener,keep +ojo,eye +nunca,never +último,last +dejar,let +pensado,thought +ciudad,city +árbol,tree +cruzar,cross +granja,farm +duro,hard +inicio,start +podría,might +historia,story +sierra,saw +ahora,far +mar,sea +dibujar,draw +izquierda,left +tarde,late +ejecutar,run +no,don’t +mientras,while +prensa,press +Cerrar,close +noche,night +reales,real +vida,life +pocos,few +norte,north +libro,book +llevar,carry +tomó,took +ciencia,science +comer,eat +habitación,room +amigo,friend +comenzó,began +gusta,idea +peces,fish +montaña,mountain +Deténgase,stop +una vez,once +base de,base +escuchar,hear +caballo,horse +cortada,cut +seguro,sure +ver,watch +colores,color +cara,face +madera,wood +principal,main +abierta,open +parecer,seem +juntos,together +próximo,next +blanco,white +niños,children +comenzar,begin +conseguido,got +caminar,walk +ejemplo,example +aliviar,ease +papel,paper +grupo,group +siempre,always +música,music +los,those +ambos,both +marca,mark +menudo,often +carta,letter +hasta,until +milla,mile +río,river +coche,car +pies,feet +cuidado,care +segundo,second +suficiente,enough +llano,plain +chica,girl +habitual,usual +joven,young +listo,ready +por encima de,above +nunca,ever +rojo,red +lista,list +aunque,though +sentir,feel +charla,talk +pájaro,bird +pronto,soon +cuerpo,body +perro,dog +familia,family +directa,direct +plantear,pose +dejar,leave +canción,song +medir,measure +puerta,door +producto,product +negro,black +corto,short +numeral,numeral +clase,class +viento,wind +pregunta,question +suceder,happen +completo,complete +buque,ship +zona,area +medio,half +roca,rock +orden,order +fuego,fire +sur,south +problema,problem +pieza,piece +dicho,told +sabía,knew +pasar,pass +desde,since +cima,top +todo,whole +rey,king +calle,street +pulgadas,inch +multiplicar,multiply +nada,nothing +curso,course +quedarse,stay +rueda,wheel +completo,full +fuerza,force +azul,blue +objeto,object +decidir,decide +superficie,surface +profunda,deep +luna,moon +isla,island +pie,foot +sistema,system +ocupado,busy +prueba,test +registro,record +barco,boat +común,common +oro,gold +posible,possible +plano,plane +lugar,stead +seco,dry +maravilla,wonder +risa,laugh +mil,thousand +hace,ago +corrió,ran +comprobar,check +juego,game +forma,shape +equiparar,equate +caliente,hot +señorita,miss +traído,brought +calor,heat +nieve,snow +neumáticos,tire +traer,bring +sí,yes +distante,distant +llenar,fill +al este,east +pintar,paint +idioma,language +entre,among +unidad,unit +potencia,power +ciudad,town +fina,fine +cierto,certain +volar,fly +caer,fall +conducir,lead +grito,cry +oscuro,dark +máquina,machine +nota,note +espere,wait +plan de,plan +figura,figure +estrella,star +caja,box +sustantivo,noun +campo,field +resto,rest +correcta,correct +capaz,able +libra,pound +hecho,done +belleza,beauty +unidad,drive +destacado,stood +contener,contain +delante,front +enseñar,teach +semana,week +último,final +dio,gave +verde,green +oh,oh +rápido,quick +desarrollar,develop +océano,ocean +caliente,warm +libre,free +minuto,minute +fuerte,strong +especial,special +mente,mind +detrás,behind +claro,clear +cola,tail +Produce,produce +hecho,fact +espacio,space +oído,heard +mejor,best +horas,hour +mejor,better +verdadero,TRUE +durante,during +cien,hundred +cinco,five +recordar,remember +paso,step +temprana,early +mantenga,hold +oeste,west +suelo,ground +interés,interest +llegar,reach +rápido,fast +verbo,verb +cantar,sing +escuchar,listen +seis,six +mesa,table +viajes,travel +menos,less +mañana,morning +diez,ten +sencilla,simple +varios,several +vocal,vowel +hacia,toward +guerra,war +sentar,lay +contra,against +patrón,pattern +lenta,slow +centro,center +amar,love +persona,person +dinero,money +servir,serve +aparecerá,appear +carretera,road +mapa,map +lluvia,rain +regla,rule +gobernar,govern +Halar,pull +frío,cold +aviso,notice +voz,voice +energía,energy +caza,hunt +probable,probable +cama,bed +hermano,brother +huevo,egg +paseo,ride +celular,cell +creer,believe +quizás,perhaps +recoger,pick +repentina,sudden +contar,count +plaza,square +razón,reason +longitud,length +representar,represent +arte,art +sujeto,subject +región,region +tamaño,size +variar,vary +resolver,settle +hablar,speak +peso,weight +general,general +hielo,ice +materia,matter +círculo,circle +par,pair +incluir,include +brecha,divide +sílaba,syllable +sentido,felt +gran,grand +bola,ball +aún,yet +ola,wave +caer,drop +corazón,heart +am,am +presente,present +pesada,heavy +danza,dance +motor,engine +posición,position +brazo,arm +amplio,wide +vela,sail +materiales,material +fracción,fraction +bosque,forest +sentarse,sit +carrera,race +ventana,window +tienda,store +verano,summer +tren,train +sueño,sleep +demostrar,prove +solitario,lone +pierna,leg +ejercicio,exercise +pared,wall +capturas,catch +monte,mount +desear,wish +cielo,sky +bordo,board +alegría,joy +invierno,winter +satélite,sat +escrito,written +salvaje,wild +instrumento,instrument +guardado,kept +vidrio,glass +hierba,grass +vaca,cow +trabajo,job +borde,edge +signo,sign +visita,visit +pasado,past +suave,soft +diversión,fun +brillante,bright +gas,gas +tiempo,weather +mes,month +millones,million +soportar,bear +acabado,finish +feliz,happy +esperanza,hope +flor,flower +vestir,clothe +extraño,strange +se ha ido,gone +comercio,trade +melodía,melody +viaje,trip +oficina,office +recibir,receive +fila,row +boca,mouth +exacta,exact +símbolo,symbol +morir,die +menos,least +problema,trouble +grito,shout +excepto,except +escribió,wrote +semilla,seed +tono,tone +unirse,join +sugerir,suggest +limpia,clean +rotura,break +dama,lady +patio,yard +aumentando,rise +mal,bad +golpe,blow +aceite,oil +sangre,blood +tocar,touch +creció,grew +ciento,cent +mezclar,mix +equipo,team +alambre,wire +costo,cost +perdido,lost +marrón,brown +desgaste,wear +jardín,garden +igual,equal +enviado,sent +elegir,choose +cayó,fell +encajar,fit +fluir,flow +justo,fair +banco,bank +recoger,collect +guardar,save +el control,control +decimal,decimal +oído,ear +demás,else +bastante,quite +rompió,broke +caso,case +medio,middle +matar,kill +hijo,son +lago,lake +momento,moment +escala,scale +fuerte,loud +primavera,spring +observar,observe +niño,child +recta,straight +consonante,consonant +nación,nation +diccionario,dictionary +leche,milk +velocidad,speed +método,method +órgano,organ +pagar,pay +edad,age +sección,section +vestido,dress +nube,cloud +sorpresa,surprise +tranquila,quiet +piedra,stone +pequeño,tiny +ascenso,climb +fresco,cool +diseño,design +pobre,poor +mucho,lot +experimento,experiment +inferior,bottom +clave,key +hierro,iron +sola,single +palillo,stick +plana,flat +veinte,twenty +piel,skin +sonrisa,smile +pliegue,crease +agujero,hole +saltar,jump +bebé,baby +ocho,eight +pueblo,village +se reúnen,meet +raíz,root +comprar,buy +aumentar,raise +resolver,solve +de metal,metal +si,whether +empujar,push +siete,seven +párrafo,paragraph +tercero,third +deberá,shall +en espera,held +pelo,hair +describir,describe +cocinero,cook +piso,floor +ya sea,either +resultado,result +quemar,burn +colina,hill +seguro,safe +gato,cat +siglo,century +considerar,consider +tipo,type +ley,law +bit,bit +costa,coast +copia,copy +frase,phrase +silencio,silent +de altura,tall +arena,sand +suelo,soil +rollo,roll +temperatura,temperature +dedo,finger +industria,industry +valor,value +lucha,fight +mentira,lie +batir,beat +excitar,excite +naturales,natural +vista,view +sentido,sense +de capital,capital +no lo hará,won’t +silla,chair +peligro,danger +fruta,fruit +rica,rich +de espesor,thick +soldado,soldier +proceso,process +operar,operate +práctica,practice +separada,separate +difícil,difficult +médico,doctor +por favor,please +proteger,protect +mediodía,noon +de cultivos,crop +moderno,modern +elemento,element +golpear,hit +estudiante,student +esquina,corner +partido,party +suministro,supply +cuya,whose +localizar,locate +anillo,ring +carácter,character +insecto,insect +capturado,caught +período,period +indicar,indicate +Radio,radio +habló,spoke +átomo,atom +humana,human +historia,history +efecto,effect +eléctrica,electric +esperar,expect +hueso,bone +ferrocarril,rail +imaginar,imagine +proporcionar,provide +acuerdo,agree +"por tanto,",thus +suave,gentle +mujer,woman +capitán,captain +adivinar,guess +necesario,necessary +agudo,sharp +ala,wing +crear,create +vecino,neighbor +lavado,wash +bate,bat +más bien,rather +multitud,crowd +maíz,corn +comparar,compare +poema,poem +cadena,string +campana,bell +dependerá,depend +carne,meat +frotar,rub +tubo,tube +famoso,famous +dólar,dollar +corriente,stream +miedo,fear +vista,sight +delgado,thin +triángulo,triangle +planeta,planet +prisa,hurry +jefe,chief +colonia,colony +reloj,clock +mina,mine +empate,tie +entrar,enter +importante,major +fresco,fresh +búsqueda,search +enviar,send +amarillo,yellow +pistola,gun +permitir,allow +print,print +muerto,dead +lugar,spot +desierto,desert +traje,suit +actual,current +ascensor,lift +rosa,rose +llegar,arrive +master,master +pista,track +padre,parent +orilla,shore +división,division +hoja,sheet +sustancia,substance +favorecer,favor +conectar,connect +mensaje,post +pasar,spend +acorde,chord +grasa,fat +contento,glad +originales,original +cuota,share +estación,station +papá,dad +pan,bread +cobrar,charge +adecuada,proper +barra,bar +oferta,offer +segmento,segment +esclavo,slave +pato,duck +instantánea,instant +mercado,market +grado,degree +poblar,populate +polluelo,chick +querido,dear +enemigo,enemy +responder,reply +bebida,drink +producirse,occur +apoyo,support +discurso,speech +naturaleza,nature +alcance,range +vapor,steam +movimiento,motion +camino,path +líquido,liquid +log,log +significado,meant +cociente,quotient +dientes,teeth +concha,shell +cuello,neck +oxígeno,oxygen +azúcar,sugar +muerte,death +bastante,pretty +habilidad,skill +mujeres,women +temporada,season +solución,solution +imán,magnet +plata,silver +gracias,thank +rama,branch +partido,match +sufijo,suffix +especialmente,especially +higo,fig +miedo,afraid +enorme,huge +hermana,sister +acero,steel +discutir,discuss +adelante,forward +similar,similar +guiar,guide +experiencia,experience +puntuación,score +manzana,apple +comprado,bought +llevado,led +pitch,pitch +abrigo,coat +masa,mass +tarjeta,card +banda,band +cuerda,rope +deslizamiento,slip +ganar,win +soñar,dream +noche,evening +condición,condition +pienso,feed +herramienta,tool +totales,total +básico,basic +olor,smell +valle,valley +ni,nor +doble,double +asiento,seat +continuar,continue +bloque,block +tabla,chart +sombrero,hat +vender,sell +éxito,success +empresa,company +restar,subtract +evento,event +"particular,",particular +acuerdo,deal +nadar,swim +plazo,term +opuesta,opposite +esposa,wife +zapato,shoe +hombro,shoulder +propagación,spread +organizar,arrange +campamento,camp +inventar,invent +algodón,cotton +nacido,born +determinar,determine +cuarto de galón,quart +nueve,nine +camión,truck +ruido,noise +nivel,level +oportunidad,chance +reunir,gather +tienda,shop +tramo,stretch +lanzar,throw +brillo,shine +propiedad,property +columna,column +molécula,molecule +seleccionar,select +mal,wrong +gris,gray +repita,repeat +exigir,require +amplio,broad +preparar,prepare +sal,salt +nariz,nose +plurales,plural +cólera,anger +reclamación,claim +continente,continent \ No newline at end of file diff --git a/models.json b/models.json index fdf68ab..533f1f5 100644 --- a/models.json +++ b/models.json @@ -1,5 +1,6 @@ { "zh_core_web_sm":"Chinese (zh_core_web_sm)", "en_core_web_sm":"English (en_core_web_sm)", - "ja_core_news_sm":"Japanese (ja_core_news_sm)" + "ja_core_news_sm":"Japanese (ja_core_news_sm)", + "es_core_news_sm":"Spanish (es_core_news_sm)" } diff --git "a/pages/04_\360\237\214\256Spanish.py" "b/pages/04_\360\237\214\256Spanish.py" new file mode 100644 index 0000000..fdba81e --- /dev/null +++ "b/pages/04_\360\237\214\256Spanish.py" @@ -0,0 +1,218 @@ +import pandas as pd +import re +import requests +import spacy +from spacy_streamlit import visualize_ner, visualize_tokens +#from spacy.language import Language +from spacy.tokens import Doc +import spacy_ke +import streamlit as st + +# Global variables +DEFAULT_TEXT = """Viví así, solo, nadie con quien poder hablar verdaderamente, hasta cuando hace seis años tuve una avería en el desierto de Sahara. Algo se había estropeado en el motor. Como no llevaba conmigo ni mecánico ni pasajero alguno, me dispuse a realizar, yo solo, una reparación difícil. Era para mí una cuestión de vida o muerte, pues apenas tenía agua de beber para ocho días. + +La primera noche me dormí sobre la arena, a unas mil millas de distancia del lugar habitado más próximo. Estaba más aislado que un náufrago en una balsa en medio del océano. Imagínense, pues, mi sorpresa cuando al amanecer me despertó una extraña vocecita que decía: +— ¡Por favor... píntame un cordero! +—¿Eh? +—¡Píntame un cordero! + +EL PRINCIPITO +""" +DESCRIPTION = "AI模型輔助語言學習:西語" +TOK_SEP = " | " +MODEL_NAME = "es_core_news_sm" +API_LOOKUP = {} +MAX_SYM_NUM = 5 +es_vocab_df = pd.read_csv("./es_vocab.csv",encoding="utf-8") + +# External API caller +def free_dict_caller(word): + req = requests.get(f"https://api.dictionaryapi.dev/api/v2/entries/en/{word}") + try: + result = req.json()[0] + if word not in API_LOOKUP: + API_LOOKUP[word] = result + except: + pass + +def show_definitions(word, pos): + if pos == 'ADV': + pos = 'adverb' + if word not in API_LOOKUP: + free_dict_caller(word) + + result = API_LOOKUP.get(word) + if result: + meanings = result.get('meanings') + if meanings: + definitions = [] + for meaning in meanings: + if meaning['partOfSpeech'] == pos.lower(): + definitions = meaning.get('definitions') + + if len(definitions) > 3: + definitions = definitions[:3] + + for definition in definitions: + df = definition.get("definition") + st.markdown(f" - {df}") + st.markdown("---") + + else: + st.info("Found no matching result on Free Dictionary!") + +def get_synonyms(word, pos): + if word not in API_LOOKUP: + free_dict_caller(word) + + result = API_LOOKUP.get(word) + if result: + meanings = result.get('meanings') + if meanings: + synonyms = [] + for meaning in meanings: + if meaning['partOfSpeech'] == pos.lower(): + synonyms = meaning.get('synonyms') + return synonyms + +# Utility functions +def create_eng_df(tokens): + seen_texts = [] + filtered_tokens = [] + for tok in tokens: + if tok.lemma_ not in seen_texts: + filtered_tokens.append(tok) + + df = pd.DataFrame( + { + "單詞": [tok.text.lower() for tok in filtered_tokens], + "詞類": [tok.pos_ for tok in filtered_tokens], + "原形": [tok.lemma_ for tok in filtered_tokens], + } + ) + st.dataframe(df) + csv = df.to_csv().encode('utf-8') + st.download_button( + label="下載表格", + data=csv, + file_name='span_forms.csv', + ) + +def filter_tokens(doc): + clean_tokens = [tok for tok in doc if tok.pos_ not in ["PUNCT", "SYM"]] + clean_tokens = [tok for tok in clean_tokens if not tok.like_email] + clean_tokens = [tok for tok in clean_tokens if not tok.like_url] + clean_tokens = [tok for tok in clean_tokens if not tok.like_num] + clean_tokens = [tok for tok in clean_tokens if not tok.is_punct] + clean_tokens = [tok for tok in clean_tokens if not tok.is_space] + return clean_tokens + +def create_kw_section(doc): + st.markdown("## 關鍵詞分析") + kw_num = st.slider("請選擇關鍵詞數量", 1, 10, 3) + kws2scores = {keyword: score for keyword, score in doc._.extract_keywords(n=kw_num)} + kws2scores = sorted(kws2scores.items(), key=lambda x: x[1], reverse=True) + count = 1 + for keyword, score in kws2scores: + rounded_score = round(score, 3) + st.write(f"{count} >>> {keyword} ({rounded_score})") + count += 1 + +# Page setting +st.set_page_config( + page_icon="🤠", + layout="wide", + initial_sidebar_state="auto", +) +st.markdown(f"# {DESCRIPTION}") + +# Load the language model +nlp = spacy.load(MODEL_NAME) + +# Add pipelines to spaCy +nlp.add_pipe("yake") # keyword extraction +# nlp.add_pipe("merge_entities") # Merge entity spans to tokens + +# Page starts from here +st.markdown("## 待分析文本") +st.info("請在下面的文字框輸入文本並按下Ctrl + Enter以更新分析結果") +text = st.text_area("", DEFAULT_TEXT, height=200) +doc = nlp(text) +st.markdown("---") + +st.info("請勾選以下至少一項功能") +keywords_extraction = st.checkbox("關鍵詞分析", False) +# analyzed_text = st.checkbox("增強文本", True) +defs_examples = st.checkbox("單詞解析", True) +morphology = st.checkbox("詞形變化", False) +ner_viz = st.checkbox("命名實體", True) +tok_table = st.checkbox("斷詞特徵", False) + +if keywords_extraction: + create_kw_section(doc) + +# if analyzed_text: +# st.markdown("## 分析後文本") +# for idx, sent in enumerate(doc.sents): +# enriched_sentence = [] +# for tok in sent: +# if tok.pos_ != "VERB": +# enriched_sentence.append(tok.text) +# else: +# synonyms = get_synonyms(tok.text, tok.pos_) +# if synonyms: +# if len(synonyms) > MAX_SYM_NUM: +# synonyms = synonyms[:MAX_SYM_NUM] +# added_verbs = " | ".join(synonyms) +# enriched_tok = f"{tok.text} (cf. {added_verbs})" +# enriched_sentence.append(enriched_tok) +# else: +# enriched_sentence.append(tok.text) + +# display_text = " ".join(enriched_sentence) +# st.write(f"{idx+1} >>> {display_text}") + +if defs_examples: + st.markdown("## 單詞解釋") + clean_tokens = filter_tokens(doc) + ez_token = [] + words = [] + for token in clean_tokens: + if len(es_vocab_df.index[es_vocab_df['es'] == token.text])!=0: # not in 1000 list + ez_token.append(token) + words.append(es_vocab_df.iloc[es_vocab_df.index[es_vocab_df['es'] == token.text][0],1]) + if len(ez_token)!=0: + num_pattern = re.compile(r"[0-9]") + clean_tokens = [tok for tok in clean_tokens if tok in ez_token] + selected_pos = ["VERB", "NOUN", "ADJ", "ADV"] + clean_tokens = [tok for tok in clean_tokens if tok.pos_ in selected_pos] + tokens_lemma_pos = [tok.lemma_ + " | " + tok.pos_ for tok in clean_tokens] + vocab = list(set(tokens_lemma_pos)) + if vocab: + selected_words = st.multiselect("請選擇要查詢的單詞: ", vocab, vocab[0:3]) + for w in selected_words: + word_pos = w.split("|") + token = word_pos[0].strip() + word = es_vocab_df.iloc[es_vocab_df.index[es_vocab_df['es'] == token][0],1] + + pos = word_pos[1].strip() + st.write(f"### {w}") + with st.expander("點擊 + 檢視結果"): + st.markdown(f"Like *{word}* in English") + show_definitions(word, pos) + else: + st.markdown("- no Elementary Spelling Words") + +if morphology: + st.markdown("## 詞形變化") + # Collect inflected forms + inflected_forms = [tok for tok in doc if tok.text.lower() != tok.lemma_.lower()] + if inflected_forms: + create_eng_df(inflected_forms) + +if ner_viz: + ner_labels = nlp.get_pipe("ner").labels + visualize_ner(doc, labels=ner_labels, show_table=False, title="命名實體") + +if tok_table: + visualize_tokens(doc, attrs=["text", "pos_", "tag_", "dep_", "head"], title="斷詞特徵") diff --git a/requirements.txt b/requirements.txt index da32e1d..97573aa 100644 --- a/requirements.txt +++ b/requirements.txt @@ -13,6 +13,7 @@ nltk # sapCy models https://github.com/explosion/spacy-models/releases/download/zh_core_web_sm-3.2.0/zh_core_web_sm-3.2.0.tar.gz#egg=zh_core_web_sm https://github.com/explosion/spacy-models/releases/download/en_core_web_sm-3.2.0/en_core_web_sm-3.2.0.tar.gz#egg=en_core_web_sm +https://github.com/explosion/spacy-models/releases/download/es_core_news_sm-3.4.0/es_core_news_sm-3.4.0.tar.gz#egg=es_core_news_sm # conversion between hanzi and transcriptions dragonmapper