Python TextBlob 翻译问题

标签 python nltk sentiment-analysis textblob

我正在使用 Python、TextBlob 和 NLTK 做一个快速的情绪分析控制台应用程序。

目前我正在使用西类牙语 wiki 文章的链接,所以我不需要翻译它并且我可以使用 nltk 西类牙语停用词列表,但是如果我想让这段代码适用于不同的语言链接怎么办?

如果我使用 textFinal=TextBlob(texto) 下方的 TextFinal=TextFinal.translate(to="es") 行(下面的代码),我得到一个错误,因为它无法将西类牙语翻译成西类牙语。

我可以仅通过使用 try/catch 来阻止这种情况吗?有没有一种方法可以让代码尝试根据我提供给应用程序的链接的语言翻译成不同的语言(以及使用不同的停用词列表)?

import nltk
nltk.download('stopwords')
from nltk import  word_tokenize
from nltk.corpus import stopwords
import string
from textblob import TextBlob, Word
import urllib.request
from bs4 import BeautifulSoup

response = urllib.request.urlopen('https://es.wikipedia.org/wiki/Valencia')
html = response.read()

soup = BeautifulSoup(html,'html5lib')
text = soup.get_text(strip = True)


tokens = word_tokenize(text)
tokens = [w.lower() for w in tokens]

table = str.maketrans('', '', string.punctuation)
stripped = [w.translate(table) for w in tokens]
words = [word for word in stripped if word.isalpha()]

stop_words = set(stopwords.words('spanish'))

words = [w for w in words if not w in stop_words]

with open('palabras.txt', 'w') as f:
    for word in words:
        f.write(" " + word)

with open('palabras.txt', 'r') as myfile:
    texto=myfile.read().replace('\n', '')


textFinal=TextBlob(texto)

print (textFinal.sentiment)

freq = nltk.FreqDist(words)

freq.plot(20, cumulative=False)

最佳答案

看一下 langdetect 包。您可以检查您输入的页面的语言,如果页面语言与翻译语言匹配,则跳过翻译。类似于以下内容:

import string
import urllib.request

import nltk
from bs4 import BeautifulSoup
from langdetect import detect
from nltk import word_tokenize
from nltk.corpus import stopwords
from textblob import TextBlob, Word

nltk.download("stopwords")
# nltk.download("punkt")

response = urllib.request.urlopen("https://es.wikipedia.org/wiki/Valencia")
html = response.read()

soup = BeautifulSoup(html, "html5lib")
text = soup.get_text(strip=True)
lang = detect(text)

tokens = word_tokenize(text)
tokens = [w.lower() for w in tokens]

table = str.maketrans("", "", string.punctuation)
stripped = [w.translate(table) for w in tokens]
words = [word for word in stripped if word.isalpha()]

stop_words = set(stopwords.words("spanish"))

words = [w for w in words if w not in stop_words]

with open("palabras.txt", "w", encoding="utf-8") as f:
    for word in words:
        f.write(" " + word)

with open("palabras.txt", "r", encoding="utf-8") as myfile:
    texto = myfile.read().replace("\n", "")


textFinal = TextBlob(texto)

translate_to = "es"
if lang != translate_to:
    textFinal = textFinal.translate(to=translate_to)

print(textFinal.sentiment)

freq = nltk.FreqDist(words)

freq.plot(20, cumulative=False)

关于Python TextBlob 翻译问题,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/55168908/

相关文章:

python - 子树之前有多少片叶子?

python - 从 python 列表中提取所有可能的表情符号

nlp - 如何使用斯坦福情感分析数据集

python - Pandas 中按指定组大小对索引进行分组

python - 如何在 pandas 数据帧上使用 sklearn TFIdfVectorizer

python - 使用 NLTK 在 Python 中的文件的特定区域中使用 sent_tokenize?

python - 如何在数据框上应用散点图和折线图进行比特币情绪分析?

Python错误: TypeError: Expected string or bytes-like object

python - 如何从索引中删除数据点

Python:使用 win32com 打开 excel 工作簿时出错