简体   繁体   中英

Translating comments using google translate api python

I am trying to translate malay comments to english.

import numpy as np
import pandas as pd
import matplotlib.pyplot as plt
import seaborn as sns
import re


from bs4 import BeautifulSoup
from nltk.stem.porter import PorterStemmer
from nltk.corpus import stopwords
from wordcloud import WordCloud, STOPWORDS
from sklearn.feature_extraction.text import CountVectorizer
from vaderSentiment.vaderSentiment import SentimentIntensityAnalyzer
from sklearn.model_selection import train_test_split
from sklearn.metrics import accuracy_score, confusion_matrix, classification_report

%matplotlib inline
reviews = pd.read_csv("/SentimentAnalysis/PROJECT_SENTIMENTANALYSIS/shopee_review.csv")
# There are 2943 duplicates
reviews[reviews.duplicated(['comment'])].shape

# Drop duplicates as we only want unique reviews
reviews.drop_duplicates(['comment'], inplace=True)

# Reindex the dataframe
reviews.reset_index(drop=True, inplace=True)

from googletrans import Translator
def translatee(txt):
    translation = translator.translate(txt, dest="en")
    
    
    return translation

reviews['tranlated']=reviews['comment'].map(translatee).text

but i'm getting the error: AttributeError: 'NoneType' object has no attribute 'group' . Screenshot attached below. 错误

Will need some help, thank you

I don't see any translator = Translator() in your code. It would be one of the reason. See an example with googletrans below which works without an issue.

from googletrans import Translator

translator = Translator()

def translatee(txt):
    translation = translator.translate(txt, dest="en")
    
    
    return translation

text = "Bu bir elmadır"

print(translatee(text))

Here is the code's answer:

在此处输入图像描述

Installing using PyPI will install version 3.0.0, which has some errors. Later versions have been fixed. Installing version 3.1.0a0 or 4.0.0rc1 will solve the problem. Please uninstall and then specify the version to install.

pip uninstall googletrans
pip install googletrans==4.0.0rc1

发布历史

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM