Skip to content

Commit 93bb646

Browse files
committed
Detect Language API usage
This program allow user to detect language for text input or also for .txt files
1 parent 8ddf528 commit 93bb646

File tree

7 files changed

+316
-0
lines changed

7 files changed

+316
-0
lines changed
Loading
Loading
Loading
Loading
Lines changed: 164 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,164 @@
1+
aa,Afar
2+
ab,Abkhazian
3+
af,Afrikaans
4+
ak,Akan
5+
am,Amharic
6+
ar,Arabic
7+
as,Assamese
8+
ay,Aymara
9+
az,Azerbaijani
10+
ba,Bashkir
11+
be,Belarusian
12+
bg,Bulgarian
13+
bh,Bihari
14+
bi,Bislama
15+
bn,Bengali
16+
bo,Tibetan
17+
br,Breton
18+
bs,Bosnian
19+
bug,Buginese
20+
ca,Catalan
21+
ceb,Cebuano
22+
chr,Cherokee
23+
co,Corsican
24+
crs,Seselwa
25+
cs,Czech
26+
cy,Welsh
27+
da,Danish
28+
de,German
29+
dv,Dhivehi
30+
dz,Dzongkha
31+
egy,Egyptian
32+
el,Greek
33+
en,English
34+
eo,Esperanto
35+
es,Spanish
36+
et,Estonian
37+
eu,Basque
38+
fa,Persian
39+
fi,Finnish
40+
fj,Fijian
41+
fo,Faroese
42+
fr,French
43+
fy,Frisian
44+
ga,Irish
45+
gd,Scots Gaelic
46+
gl,Galician
47+
gn,Guarani
48+
got,Gothic
49+
gu,Gujarati
50+
gv,Manx
51+
ha,Hausa
52+
haw,Hawaiian
53+
hi,Hindi
54+
hmn,Hmong
55+
hr,Croatian
56+
ht,Haitian Creole
57+
hu,Hungarian
58+
hy,Armenian
59+
ia,Interlingua
60+
id,Indonesian
61+
ie,Interlingue
62+
ig,Igbo
63+
ik,Inupiak
64+
is,Icelandic
65+
it,Italian
66+
iu,Inuktitut
67+
iw,Hebrew
68+
ja,Japanese
69+
jw,Javanese
70+
ka,Georgian
71+
kha,Khasi
72+
kk,Kazakh
73+
kl,Greenlandic
74+
km,Khmer
75+
kn,Kannada
76+
ko,Korean
77+
ks,Kashmiri
78+
ku,Kurdish
79+
ky,Kyrgyz
80+
la,Latin
81+
lb,Luxembourgish
82+
lg,Ganda
83+
lif,Limbu
84+
ln,Lingala
85+
lo,Laothian
86+
lt,Lithuanian
87+
lv,Latvian
88+
mfe,Mauritian Creole
89+
mg,Malagasy
90+
mi,Maori
91+
mk,Macedonian
92+
ml,Malayalam
93+
mn,Mongolian
94+
mr,Marathi
95+
ms,Malay
96+
mt,Maltese
97+
my,Burmese
98+
na,Nauru
99+
ne,Nepali
100+
nl,Dutch
101+
no,Norwegian
102+
nr,Ndebele
103+
nso,Pedi
104+
ny,Nyanja
105+
oc,Occitan
106+
om,Oromo
107+
or,Oriya
108+
pa,Punjabi
109+
pl,Polish
110+
ps,Pashto
111+
pt,Portuguese
112+
qu,Quechua
113+
rm,Rhaeto Romance
114+
rn,Rundi
115+
ro,Romanian
116+
ru,Russian
117+
rw,Kinyarwanda
118+
sa,Sanskrit
119+
sco,Scots
120+
sd,Sindhi
121+
sg,Sango
122+
si,Sinhalese
123+
sk,Slovak
124+
sl,Slovenian
125+
sm,Samoan
126+
sn,Shona
127+
so,Somali
128+
sq,Albanian
129+
sr,Serbian
130+
ss,Siswant
131+
st,Sesotho
132+
su,Sundanese
133+
sv,Swedish
134+
sw,Swahili
135+
syr,Syriac
136+
ta,Tamil
137+
te,Telugu
138+
tg,Tajik
139+
th,Thai
140+
ti,Tigrinya
141+
tk,Turkmen
142+
tl,Tagalog
143+
tlh,Klingon
144+
tn,Tswana
145+
to,Tonga
146+
tr,Turkish
147+
ts,Tsonga
148+
tt,Tatar
149+
ug,Uighur
150+
uk,Ukrainian
151+
ur,Urdu
152+
uz,Uzbek
153+
ve,Venda
154+
vi,Vietnamese
155+
vo,Volapuk
156+
war,Waray Philippines
157+
wo,Wolof
158+
xh,Xhosa
159+
yi,Yiddish
160+
yo,Yoruba
161+
za,Zhuang
162+
zh,Chinese Simplified
163+
zh-Hant,Chinese Traditional
164+
zu,Zulu
Lines changed: 138 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,138 @@
1+
import detectlanguage as dtl #Client Library for our API
2+
import tkinter as tk
3+
from tkinter import font
4+
from tkinter import *
5+
from tkinter import filedialog
6+
from tkinter import messagebox as mbox
7+
from tkinter import scrolledtext
8+
from tkinter import ttk
9+
import csv
10+
11+
dtl.Configuration.api_key = "YOUR__API__KEY" #Your API Key
12+
13+
##--------BUTTON CALLBACKS--------##
14+
def clear_input():
15+
entry_1.delete(0,END)
16+
17+
def activate_input():
18+
entry_1['state'] = "normal" #enables statement entry widget
19+
if(file_entry.get()!=""):#check if file_entry widget is having file_name in it
20+
file_entry.delete(0,END) #deletes file name if selected from file_entry widget
21+
file_entry['state'] = "disabled" #then disables file_entry widget
22+
23+
24+
def choose_file():
25+
global content
26+
if(entry_1.get()!=""): #checking if statement entry has text in it
27+
entry_1.delete(0,END) #deleting text from entry_1 widget
28+
entry_1['state'] = "disabled" #disabling the statement entry
29+
file_entry['state'] = "normal" #Making the file entry visible
30+
file = filedialog.askopenfile(initialdir="/",
31+
title="Choose File",
32+
filetypes=(("Text files","*.txt*"),
33+
("all files","*.*")),
34+
mode = 'r+')
35+
if file != None:
36+
content = file.read()
37+
if(file_entry.get()!=""):
38+
file_entry.delete(0,END)
39+
file_entry.insert(END,file.name)
40+
return content
41+
else:
42+
mbox.showerror("File Error", "File not selected!!")
43+
44+
def detect():
45+
if(entry_1['state']=='normal'):
46+
if entry_1.get()!= "": #check if the entry has no content
47+
content1 = entry_1.get()
48+
stxt['state'] = 'normal'
49+
if (stxt.get('1.0', 'end-1c') != ""):
50+
stxt.delete(1.0, END)
51+
stxt.insert(END,dtl.simple_detect(content1)) #simple_detect function just returns language code
52+
stxt['state'] = "disabled"
53+
else:
54+
mbox.showerror("Input Error","Input can't be empty!")
55+
else:
56+
if file_entry.get()!="": #check if the file is selected or not
57+
content1 = content
58+
if(content1==""): #check if file has content or not
59+
mbox.showerror("File Error","File has no content!")
60+
else:
61+
stxt['state'] = 'normal'
62+
if(stxt.get('1.0', 'end-1c')!=""):
63+
stxt.delete(1.0,END)
64+
stxt.insert(END,dtl.simple_detect(content1))
65+
stxt['state'] = "disabled"
66+
else:
67+
mbox.showerror("File Error","File not selected!")
68+
69+
##---------GUI STARTS HERE--------##
70+
window = tk.Tk()
71+
window.title("Detect Language")
72+
window.config(bg="white")
73+
window.geometry("1000x700")
74+
75+
## SECTION 1 : TAKING STATEMENT INPUT FROM USER
76+
l1 = Label(window, text="Enter your text here:", bg="white") #label 1
77+
l1.place(x=10,y=60,height=20,width=200)
78+
l1['font'] = font.Font(size=15)
79+
80+
entry_1 = Entry(window, bg="#9EF281") #entry_1
81+
entry_1.place(x=230, y=55, height=30, width=300)
82+
entry_1['font'] = font.Font(size=15)
83+
84+
clear_btn = Button(window, bg="yellow", text="Clear Input", command=clear_input) #clear input button
85+
clear_btn.place(x=280, y=95)
86+
87+
activate_btn = Button(window, bg="#FFCB64", text="Activate Input", command=activate_input) #clear input button
88+
activate_btn.place(x=390, y=95)
89+
90+
## SECTION 2 : TAKING FILE INPUT FROM USER
91+
l2 = Label(window, text="Detect from (.txt) file:", bg="white") #label2
92+
l2.place(x=10, y=180, height=20, width=200)
93+
l2['font'] = font.Font(size=15)
94+
95+
file_entry = Entry(window, bg="#9EF281") #entry_2
96+
file_entry.place(x=230, y=175, height=30, width=300)
97+
file_entry['font'] = font.Font(size=15)
98+
file_entry['state'] = "disabled"
99+
100+
file_btn = Button(window, bg="yellow", text="Choose File", command=choose_file) #choose file button
101+
file_btn.place(x=345, y=220)
102+
103+
## SECTION 3 : DETECT BUTTON & OUTPUT WINDOW
104+
detect_button = Button(window, bg="green", text="Detect Language", fg="white", command=detect) #detect lang button
105+
detect_button.place(x=750, y=55)
106+
stxt = scrolledtext.ScrolledText(window,
107+
wrap = tk.WORD,
108+
width = 40,
109+
height = 10,
110+
font = ("Times New Roman",
111+
15))
112+
stxt.place(x=650,y=100, height=140, width=300)
113+
stxt['state'] = "disabled"
114+
115+
## SECTION 4 : REFERENCE SECTION FOR LANGUAGE CODES
116+
frame = Frame(window)
117+
frame.place(x=35,y=350,height=260, width=930)
118+
119+
ref_label = Label(window,text="Reference for Language Codes: ", bg='white', fg='green')
120+
ref_label['font'] = font.Font(size=15)
121+
ref_label.place(x=20, y=300)
122+
123+
124+
# Constructing treeview for displaying codes from csv file in table format in tkinter
125+
with open(".\Related\languages.csv",newline='\n') as csvfile: #takes filename of languages.csv from "Related" folder
126+
data_rows = csv.reader(csvfile)
127+
tree = ttk.Treeview(frame,column=('col1','col2'), show='headings',height=10)
128+
tree.column("# 1", anchor=CENTER)
129+
tree.heading("# 1", text="CODE")
130+
tree.column("# 2", anchor=CENTER)
131+
tree.heading("# 2", text="LANGUAGE")
132+
133+
for row in data_rows:
134+
tree.insert('', 'end', text="1", values=(row[0],row[1]))
135+
tree.place(x=0,y=0,height=260, width=930)
136+
137+
window.mainloop()
138+
##------GUI ENDS HERE--------##
Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
About:
2+
This project allows you to use Detect Language API to detect the language of the statement input or the input in form of .txt file . The output is obtained in form of language codes, hence we have provided a reference for language codes which is read from csv file given in "Related folder".
3+
4+
Important library Imports:
5+
1) import detectlanguage (This acts as client library for the API)
6+
2) from tkinter import *
7+
3) from tkinter import ttk
8+
4) import csv
9+
10+
Important library/module downloads to be done:
11+
--> pip install detectlanguage
12+
13+
**NOTE: You need to generate your API key from the below given website
14+
https://detectlanguage.com/

0 commit comments

Comments
 (0)