Spaces:
Sleeping
Sleeping
| import gradio as gr | |
| import pandas as pd | |
| import os | |
| from pyvi import ViTokenizer, ViPosTagger | |
| pos_mapping = { | |
| "A": ("Adjective", "Tính từ"), | |
| "C": ("Coordinating conjunction", "Liên từ kết hợp"), | |
| "E": ("Preposition", "Giới từ"), | |
| "I": ("Interjection", "Thán từ"), | |
| "L": ("Determiner", "Hạn định từ"), | |
| "M": ("Numeral", "Số từ"), | |
| "N": ("Common noun", "Danh từ chung"), | |
| "Nc": ("Noun Classifier", "Lượng từ"), | |
| "Ny": ("Noun abbreviation", "Từ viết tắt danh từ"), | |
| "Np": ("Proper noun", "Danh từ riêng"), | |
| "Nu": ("Unit noun", "Danh từ đơn vị"), | |
| "P": ("Pronoun", "Đại từ"), | |
| "R": ("Adverb", "Trạng từ"), | |
| "S": ("Subordinating conjunction", "Liên từ phụ thuộc"), | |
| "T": ("Auxiliary, modal words", "Trợ từ, động từ tình thái"), | |
| "V": ("Verb", "Động từ"), | |
| "X": ("Unknown", "Không xác định"), | |
| "F": ("Filtered out (punctuation)", "Bị loại bỏ (dấu câu)") | |
| } | |
| def change(sentence): | |
| resutlt = ViPosTagger.postagging(ViTokenizer.tokenize(sentence)) | |
| print(resutlt) | |
| words, pos_tags = resutlt | |
| pos_tags_engs = [] | |
| pos_tags_vies = [] | |
| for i in pos_tags: | |
| print(pos_mapping[i][0]) | |
| print(pos_mapping[i][1]) | |
| pos_tags_engs.append(pos_mapping[i][0]) | |
| pos_tags_vies.append(pos_mapping[i][1]) | |
| # final_result = '''''' | |
| # for word, pos_tag, pos_tags_eng, pos_tags_vie in zip(words, pos_tags, pos_tags_engs, pos_tags_vies): | |
| # final_result += f"Từ: {word}, Loại từ: {pos_tag} ({pos_tags_eng} - {pos_tags_vie})\n" | |
| # return final_result | |
| header = f"{'Từ':<15} {'Loại từ':<10} {'English POS':<20} {'Tiếng Việt POS'}\n" | |
| header += "-" * 60 + "\n" | |
| rows = [f"{word:<15} {pos_tag:<10} {pos_tags_eng:<20} {pos_tags_vie}" | |
| for word, pos_tag, pos_tags_eng, pos_tags_vie in zip(words, pos_tags, pos_tags_engs, pos_tags_vies)] | |
| final_result = header + "\n".join(rows) | |
| return final_result | |
| demo_interface = gr.Interface( | |
| fn=change, | |
| inputs="text", | |
| outputs="text", | |
| examples=eval(os.environ['DES_EXAMPLE']), | |
| description=os.environ['DES_DEMO'] | |
| ) | |
| intro_interface = gr.HTML(os.environ['DES_INTERFACE']) | |
| tabbed_interface = gr.TabbedInterface( | |
| interface_list=[demo_interface, intro_interface], | |
| tab_names=["Demo", "Giới thiệu"] | |
| ) | |
| tabbed_interface.launch() | |