【问题标题】:Python: Using TextBlob NLTK to read a text file and detect the languagePython:使用 TextBlob NLTK 读取文本文件并检测语言
【发布时间】:2020-12-04 08:38:31
【问题描述】:

我是 Python 和编码的新手,所以请多多包涵。

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

我需要做的是让它检测文本文件的语言,而不仅仅是我输入的字符串。所以基本上我需要用任何语言的文本文件替换文本行,并添加代码来打开/读取文件并让 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
>>>

【问题讨论】:

    标签: python text-files textblob language-detection


    【解决方案1】:

    想出来以防将来有人查询。最后非常简单,提供与以前相同的输出,但我的字符串位于文本文件中,而不是输入出来。

    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)
    

    【讨论】:

      猜你喜欢
      • 2011-03-12
      • 1970-01-01
      • 2017-06-22
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2016-10-14
      • 1970-01-01
      相关资源
      最近更新 更多