Python: Using TextBlob NLTK to read a text file and detect the language

2.7k views Asked by At

I'm brand new to Python and coding so please bear with me.

I installed the TextBlob plugin to my IDE and it works like a charm when detecting the language of a string. See below code and output at the bottom.

What I need to do is have it detect the language of a text file, not just a string I have typed out. So essentially I need replace the lines of text with text files of whatever languages and add code to open/read the files and have TextBlob do its thing.

Any ideas?

from textblob import TextBlob

text1 = TextBlob('I looked for Mary and Samantha at the bus station')
a = text1.detect_language()
print(a)

text2 = TextBlob('Appliquer un nom , une dénomination , un mot , une phrase à une personne , à une chose')
b = text2.detect_language()
print(b)

text3 = TextBlob('Escribe un ejemplo para mostrar el significado de la palabra de vocabulario.')
c = text3.detect_language()
print(c)


>>> %Run 'NLP TextBlob.py'
en
fr
es
>>>
1

There are 1 answers

0
cantyus99 On

Figured it out in case anyone queries this in the future. Pretty simple in the end and provides the same output as before, but my strings are located in the text files instead of typed out.

from textblob import TextBlob

with open('1.txt', 'r') as text1:
    content = text1.read()
blob = TextBlob(content)

a = blob.detect_language()
print(a)

with open('2.txt', 'r') as text2:
    content = text2.read()
blob = TextBlob(content)

b = blob.detect_language()
print(b)

with open('3.txt', 'r') as text3:
    content = text3.read()
blob = TextBlob(content)

c = blob.detect_language()
print(c)