Spaces:
Sleeping
Sleeping
File size: 3,198 Bytes
a88bbe0 |
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 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 |
import streamlit as st
import requests
import csv
from io import StringIO
def load_dictionary_from_csv(file_path):
dictionary = {}
with open(file_path, newline='', encoding='utf-8') as csvfile:
reader = csv.reader(csvfile, delimiter=';')
for row in reader:
lampung_word = row[0]
indonesian_translations = row[1].split(',')
dictionary[lampung_word] = indonesian_translations
return dictionary
def load_dictionary_from_url(url):
response = requests.get(url)
data = response.text
csvfile = StringIO(data)
dictionary = {}
reader = csv.reader(csvfile, delimiter=';')
for row in reader:
lampung_word = row[0]
indonesian_translations = row[1].split(',')
dictionary[lampung_word] = indonesian_translations
return dictionary
def check_missing_words(text, dictionary):
words = text.split()
missing_words = []
for word in words:
if word not in dictionary:
missing_words.append(word)
return missing_words
# Fungsi untuk menerjemahkan teks dari bahasa Lampung ke bahasa Indonesia
def translate_indonesia_to_lampung(text, dictionary):
words = text.split()
translated_words = []
for word in words:
possible_translations = dictionary.get(word, [word]) # Gunakan kata asli jika tidak ditemukan di kamus
best_translation = min(possible_translations, key=len) # Pilih terjemahan terpendek
translated_words.append(best_translation)
translated_text = ' '.join(translated_words)
return translated_text
# Fungsi untuk menerjemahkan teks dari bahasa Indonesia ke bahasa Lampung
def translate_lampung_to_indonesia(text, dictionary):
words = text.split()
translated_words = []
for word in words:
lampung_word = None
for key, translations in dictionary.items():
if word in translations:
lampung_word = key
break
translated_words.append(lampung_word if lampung_word else word)
translated_text = ' '.join(translated_words)
return translated_text
# Memuat kamus dari URL berkas CSV
csv_url = 'https://raw.githubusercontent.com/meizano/lampung/master/db/parser/Indonesia-Lampung.csv'
dictionary = load_dictionary_from_url(csv_url)
# Judul dan deskripsi
st.set_theme('default')
st.title("Penerjemah Indonesia - Lampung")
st.write("Masukkan teks dalam bahasa Indonesia di bawah dan klik tombol 'Terjemahkan'.")
# Sidebar untuk tombol navigasi
menu_selection = st.sidebar.radio("Pilih Arah Terjemahan", ("Lampung ke Indonesia", "Indonesia ke Lampung"))
# Widget untuk memasukkan teks
input_text = st.text_area("Masukkan Teks", "")
# Tombol untuk memicu penerjemahan
if st.button("Terjemahkan"):
if input_text:
if menu_selection == "Lampung ke Indonesia":
translated_text = translate_lampung_to_indonesian(input_text, dictionary)
elif menu_selection == "Indonesia ke Lampung":
translated_text = translate_indonesian_to_lampung(input_text, dictionary)
st.write("Hasil Terjemahan:")
st.write(translated_text)
else:
st.write("Silakan masukkan teks terlebih dahulu.")
|