Reputation: 21
I'm new in python programming and I'd like to make an sentiment analysis by word2vec based on amazon reviews. My problem is that I create three functions because I have to take the comment of the reviews and to split the words.
import pandas as pd
import numpy as np
from sklearn.model_selection import train_test_split
import matplotlib.pyplot as plt
from bs4 import BeautifulSoup
import nltk
from nltk.corpus import stopwords
from nltk.stem.porter import PorterStemmer
from nltk.stem import SnowballStemmer
data = pd.read_csv('Reviews.csv')
print(data.head(4))
data['pos'] = np.where(data['Score'] > 3, 1, 0)
#suddivion the file that analyzed by the score ( from 1 to 5)
X_train, X_test, y_train, y_test = train_test_split(data['Text'],data['pos'], test_size = 0.2, random_state=0)
nltk.download()
# now start the problems
def pulitoretesto (prim_testo, rmv_stpwrds=False, stemming=False, split_testo=False):
testo = BeautifulSoup(prim_testo, 'lxml').get_text()
lett = re.sub("[^a-zA-Z]", " ",testo)
pr = lett.lower().split()
if rmv_stpwrds:
stop = set(stopwords.words("english"))
pr= [a for a in pr if not w in stop]
if stemming==True:
eliminsuf = SnowballStemmer('english')
pr = [eliminsuf.stem(a) for a in pr]
if split_testo==True:
return (pr)
return( " ".join(pr))
X_train_nuovo = []
for x in X_train:
X_train_nuovo.append(pulitoretesto(x))
print(' nuova X_train :\n', X_train_nuovo)
X_test_nuovo = []
for x in X_test:
X_test_nuovo.append(pulitoretesto(x))
tokenizer=nltk.data.load('tokenizers/punkt/english.pickle')
def parsfrasi(revi,tokenizer, rmv_stpwrds=False):
prmtv_frs=tokenizer.tokenize(revi.strip())
frasi = []
for prmtv_frs1 in prmtv_frs :
if len(prmtv_frs1) > 0 :
frasi.pulitoretesto(prmtv_frs1, rmv_stpwrds, split_testo=True )
return frasi
frasi = []
for revi in X_train_nuovo:
frasi += parsfrasi(revi, tokenizer)
when I fit the code there this: AttributeError: 'list' object has no attribute 'pulitoretesto' Thanks to everybody =)
Upvotes: 2
Views: 335
Reputation: 1580
I'm not sure what exactly is happening, but one of your lines in the function is
frasi = []
and then you do
frasi.pulitoretesto(prmtv_frs1, rmv_stpwrds, split_testo=True )
which cannot be done as frasi
is declared as a list
If you want to call the function 'pulitoretesto' and add it to frasi, do this:
frasi.append(pulitoretesto(prmtv_frs1, rmv_stpwrds, split_testo=True))
Upvotes: 1