Python:使用TextBlob NLTK读取文本文件并检测语言

2024-09-25 08:30:27 发布

您现在位置:Python中文网/ 问答频道 /正文

我是Python和编码的新手,所以请耐心听我说

我将TextBlob插件安装到我的IDE中,当检测字符串的语言时,它就像一个符咒。请参阅下面底部的代码和输出

我需要做的是让它检测文本文件的语言,而不仅仅是我输入的字符串。所以本质上我需要用任何语言的文本文件替换文本行,并添加代码来打开/读取文件,让TextBlob完成它的工作

有什么想法吗

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
>>>

Tags: 字符串代码语言编码delanguageunprint
1条回答
网友
1楼 · 发布于 2024-09-25 08:30:27

如果将来有人质疑这一点,我会想办法解决的。最后非常简单,并提供了与以前相同的输出,但我的字符串位于文本文件中,而不是打印出来的

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)

相关问题 更多 >