Spaces:
Sleeping
Sleeping
Create app.py
Browse files
app.py
ADDED
@@ -0,0 +1,87 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
import streamlit as st
|
2 |
+
|
3 |
+
import requests
|
4 |
+
import csv
|
5 |
+
from io import StringIO
|
6 |
+
|
7 |
+
def load_dictionary_from_csv(file_path):
|
8 |
+
dictionary = {}
|
9 |
+
with open(file_path, newline='', encoding='utf-8') as csvfile:
|
10 |
+
reader = csv.reader(csvfile, delimiter=';')
|
11 |
+
for row in reader:
|
12 |
+
lampung_word = row[0]
|
13 |
+
indonesian_translations = row[1].split(',')
|
14 |
+
dictionary[lampung_word] = indonesian_translations
|
15 |
+
return dictionary
|
16 |
+
|
17 |
+
def load_dictionary_from_url(url):
|
18 |
+
response = requests.get(url)
|
19 |
+
data = response.text
|
20 |
+
csvfile = StringIO(data)
|
21 |
+
dictionary = {}
|
22 |
+
reader = csv.reader(csvfile, delimiter=';')
|
23 |
+
for row in reader:
|
24 |
+
lampung_word = row[0]
|
25 |
+
indonesian_translations = row[1].split(',')
|
26 |
+
dictionary[lampung_word] = indonesian_translations
|
27 |
+
return dictionary
|
28 |
+
|
29 |
+
def check_missing_words(text, dictionary):
|
30 |
+
words = text.split()
|
31 |
+
missing_words = []
|
32 |
+
for word in words:
|
33 |
+
if word not in dictionary:
|
34 |
+
missing_words.append(word)
|
35 |
+
return missing_words
|
36 |
+
|
37 |
+
# Fungsi untuk menerjemahkan teks dari bahasa Lampung ke bahasa Indonesia
|
38 |
+
def translate_indonesia_to_lampung(text, dictionary):
|
39 |
+
words = text.split()
|
40 |
+
translated_words = []
|
41 |
+
for word in words:
|
42 |
+
possible_translations = dictionary.get(word, [word]) # Gunakan kata asli jika tidak ditemukan di kamus
|
43 |
+
best_translation = min(possible_translations, key=len) # Pilih terjemahan terpendek
|
44 |
+
translated_words.append(best_translation)
|
45 |
+
translated_text = ' '.join(translated_words)
|
46 |
+
return translated_text
|
47 |
+
|
48 |
+
# Fungsi untuk menerjemahkan teks dari bahasa Indonesia ke bahasa Lampung
|
49 |
+
def translate_lampung_to_indonesia(text, dictionary):
|
50 |
+
words = text.split()
|
51 |
+
translated_words = []
|
52 |
+
for word in words:
|
53 |
+
lampung_word = None
|
54 |
+
for key, translations in dictionary.items():
|
55 |
+
if word in translations:
|
56 |
+
lampung_word = key
|
57 |
+
break
|
58 |
+
translated_words.append(lampung_word if lampung_word else word)
|
59 |
+
translated_text = ' '.join(translated_words)
|
60 |
+
return translated_text
|
61 |
+
|
62 |
+
# Memuat kamus dari URL berkas CSV
|
63 |
+
csv_url = 'https://raw.githubusercontent.com/meizano/lampung/master/db/parser/Indonesia-Lampung.csv'
|
64 |
+
dictionary = load_dictionary_from_url(csv_url)
|
65 |
+
|
66 |
+
# Judul dan deskripsi
|
67 |
+
st.set_theme('default')
|
68 |
+
st.title("Penerjemah Indonesia - Lampung")
|
69 |
+
st.write("Masukkan teks dalam bahasa Indonesia di bawah dan klik tombol 'Terjemahkan'.")
|
70 |
+
|
71 |
+
# Sidebar untuk tombol navigasi
|
72 |
+
menu_selection = st.sidebar.radio("Pilih Arah Terjemahan", ("Lampung ke Indonesia", "Indonesia ke Lampung"))
|
73 |
+
|
74 |
+
# Widget untuk memasukkan teks
|
75 |
+
input_text = st.text_area("Masukkan Teks", "")
|
76 |
+
|
77 |
+
# Tombol untuk memicu penerjemahan
|
78 |
+
if st.button("Terjemahkan"):
|
79 |
+
if input_text:
|
80 |
+
if menu_selection == "Lampung ke Indonesia":
|
81 |
+
translated_text = translate_lampung_to_indonesian(input_text, dictionary)
|
82 |
+
elif menu_selection == "Indonesia ke Lampung":
|
83 |
+
translated_text = translate_indonesian_to_lampung(input_text, dictionary)
|
84 |
+
st.write("Hasil Terjemahan:")
|
85 |
+
st.write(translated_text)
|
86 |
+
else:
|
87 |
+
st.write("Silakan masukkan teks terlebih dahulu.")
|