Translating comments using google translate api python

278 Views Asked by At

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

Will need some help, thank you

2

There are 2 best solutions below

3
On

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:

enter image description here

0
On

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

Release history