-2

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

Derek O
  • 16,770
  • 4
  • 24
  • 43
Sabx2x1x
  • 31
  • 6
  • can you provide a sample of your dataframe so we can reproduce your error? you can copy and paste the output from `reviews.head().to_dict()` into your question – Derek O Jan 06 '23 at 06:56
  • Please read [ask] and [mre], and show errors [as text, not an image](https://meta.stackoverflow.com/questions/285551) by copying and pasting the text and formatting it like code. – Karl Knechtel Jan 30 '23 at 02:25
  • Please see: https://stackoverflow.com/questions/67958138 – Karl Knechtel Jan 30 '23 at 02:30

2 Answers2

0

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

Baris Ozensel
  • 433
  • 1
  • 3
  • 11
  • 1
    although you're correct that `translator = Translator()` should be in the code, this is not the reason for the error `AttributeError: 'NoneType' object has no attribute 'group'`. i am getting this same error even when i run your code, and i believe it has to do with some bug in `googletrans`. see [this question](https://stackoverflow.com/questions/52446811/why-googletrans-translator-suddenly-stopped-working). are you using a different version of the googletrans library, or did you modify your local version? – Derek O Jan 06 '23 at 07:05
  • 3
    I agree with you that the 'AttributeError' is not the reason of this. To understand what is wrong in DF, better to have a piece of the DF mentioned in the question. I remember that I needed to downgrade googletrans once to be able to make my code work due to some httpx errors, not sure your case is the same case as well. The googletrans version in the above code is 3.1.0a0. Just wanted to mention what is missed while using googletrans in my update. – Baris Ozensel Jan 06 '23 at 07:21
  • 1
    @BarisOzensel, downgrading my googletrans version, worked. Thank you! – Sabx2x1x Jan 06 '23 at 08:41
-1

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

yunwoong
  • 27
  • 2