Skip to content

Commit 252c6c1

Browse files
committed
add language detector tutorial
1 parent 26cb9c4 commit 252c6c1

8 files changed

+132
-0
lines changed

README.md

+1
Original file line numberDiff line numberDiff line change
@@ -175,6 +175,7 @@ This is a repository of all the tutorials of [The Python Code](https://www.thepy
175175
- [How to Use Gmail API in Python](https://www.thepythoncode.com/article/use-gmail-api-in-python). ([code](general/gmail-api))
176176
- [How to Use YouTube API in Python](https://www.thepythoncode.com/article/using-youtube-api-in-python). ([code](general/youtube-api))
177177
- [Webhooks in Python with Flask](https://www.thepythoncode.com/article/webhooks-in-python-with-flask). ([code](https://github.com/bassemmarji/Flask_Webhook))
178+
- [How to Make a Language Detector in Python](https://www.thepythoncode.com/article/language-detector-in-python). ([code](general/language-detector))
178179

179180
- ### [Database](https://www.thepythoncode.com/topic/using-databases-in-python)
180181
- [How to Use MySQL Database in Python](https://www.thepythoncode.com/article/using-mysql-database-in-python). ([code](database/mysql-connector))

general/language-detector/README.md

+1
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
# [How to Make a Language Detector Using Python](https://www.thepythoncode.com/article/language-detector-in-python)
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
""""
2+
THIS SCRIPT IS USING langdetect
3+
"""
4+
5+
# import the detect function from langdetect
6+
from langdetect import detect
7+
8+
# openning the txt file in read mode
9+
sentences_file = open('sentences.txt', 'r')
10+
11+
# creating a list of sentences using the readlines() function
12+
sentences = sentences_file.readlines()
13+
14+
# a function for detection language
15+
def detect_langauage(sentence, n):
16+
"""try and except block for catching exception errors"""
17+
# the try will run when everything is ok
18+
try:
19+
# checking if the sentence[n] exists
20+
if sentences[n]:
21+
# creating a new variable, the strip() function removes newlines
22+
new_sentence = sentences[n].strip('\n')
23+
print(f'The language for the sentence "{new_sentence}" is {detect(new_sentence)}')
24+
# this will catch all the errors that occur
25+
except:
26+
print(f'Sentence does not exist')
27+
28+
# printing the the number of sentences in the sentences.txt
29+
print(f'You have {len(sentences)} sentences')
30+
31+
# this will prompt the user to enter an integer
32+
number_of_sentence = int(input('Which sentence do you want to detect?(Provide an integer please):'))
33+
34+
# calling the detect_langauage function
35+
detect_langauage(sentences_file, number_of_sentence)
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
""""
2+
THIS SCRIPT IS USING langid
3+
"""
4+
import langid
5+
6+
# openning the txt file in read mode
7+
sentences_file = open('sentences.txt', 'r')
8+
9+
# creating a list of sentences using the readlines() function
10+
sentences = sentences_file.readlines()
11+
12+
# looping through all the sentences in thesentences.txt file
13+
for sentence in sentences:
14+
# detecting the languages for the sentences
15+
lang = langid.classify(sentence)
16+
# formatting the sentence by removing the newline characters
17+
formatted_sentence = sentence.strip('\n')
18+
19+
print(f'The sentence "{formatted_sentence}" is in {lang[0]}')
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
""""
2+
THIS SCRIPT IS USING googletrans
3+
"""
4+
# importing the Translator function from googletrans
5+
from googletrans import Translator
6+
7+
# initializing the translator object
8+
translator = Translator()
9+
# openning the txt file in read mode
10+
sentences_file = open('sentences.txt', 'r')
11+
# creating a list of sentences using the readlines() function
12+
sentences = sentences_file.readlines()
13+
# a function for detection language
14+
def detect_langauage(sentence, n):
15+
"""try and except block for catching exception errors"""
16+
# the try will run when everything is ok
17+
try:
18+
# checking if the sentence[n] exists
19+
if sentences[n]:
20+
# creating a new variable, the strip() function removes newlines
21+
new_sentence = sentences[n].strip('\n')
22+
# detecting the sentence language using the translator.detect()
23+
# .lang extract the language code
24+
detected_sentence_lang = translator.detect(new_sentence).lang
25+
print(f'The language for the sentence "{new_sentence}" is {detected_sentence_lang}')
26+
# this will catch all the errors that occur
27+
except:
28+
print(f'Make sure the sentence exists or you have internet connection')
29+
30+
31+
print(f'You have {len(sentences)} sentences')
32+
# this will prompt the user to enter an integer
33+
number_of_sentence = int(input('Which sentence do you want to detect?(Provide an integer please):'))
34+
# calling the detect_langauage function
35+
detect_langauage(sentences_file, number_of_sentence)
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
""""
2+
THIS SCRIPT IS USING language_detector
3+
"""
4+
# importing the language detector
5+
from language_detector import detect_language
6+
7+
# definig the function for detecting the language
8+
# the function takes text as an argument
9+
def detectLanguage(text):
10+
# detecting the language using the detect_language function
11+
language = detect_language(text)
12+
print(f'"{text}" is written in {language}')
13+
14+
# an infinite while while loop
15+
while True:
16+
17+
# this will prompt the user to enter options
18+
option = input('Enter 1 to detect language or 0 to exit:')
19+
20+
if option == '1':
21+
# this will prompt the user to enter the text
22+
data = input('Enter your sentence or word here:')
23+
# calling the detectLanguage function
24+
detectLanguage(data)
25+
26+
# if option is 0 break the loop
27+
elif option == '0':
28+
print('Quitting........\nByee!!!')
29+
break
30+
# if option isnt 1 or 0 then its invalid
31+
else:
32+
print('Wrong input, try again!!!')
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
googletrans==3.1.0a0
2+
langdetect==1.0.9
3+
langid==1.1.6
4+
language-detector==5.0.2
+5
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
I love programming, Python is my favorite language.
2+
أحب البرمجة ، بايثون هي لغتي المفضلة.
3+
我喜欢编程,Python 是我最喜欢的语言。
4+
Me encanta programar, Python es mi lenguaje favorito.
5+
Eu amo programar, Python é minha linguagem favorita.

0 commit comments

Comments
 (0)