簡體   English   中英

Python:使用 TextBlob NLTK 讀取文本文件並檢測語言

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

我是 Python 和編碼的新手,所以請多多包涵。

我將 TextBlob 插件安裝到我的 IDE 中,它在檢測字符串語言時就像一個魅力。 請參閱下面的代碼和底部的 output。

我需要做的是讓它檢測文本文件的語言,而不僅僅是我輸入的字符串。 所以基本上我需要用任何語言的文本文件替換文本行,並添加代碼來打開/讀取文件並讓 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
>>>

想通了,以防將來有人問這個問題。 最后非常簡單,並提供與以前相同的 output,但我的字符串位於文本文件中,而不是輸入。

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)

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM