HW4 -- Sentiment and Lies

STEP 1: Import the data

NOTE: May need to change delimiter based on the data file

In [54]:
import pandas as pd
df = pd.read_csv('deception_data_converted_final.csv',  sep='\t')
df[:5]
Out[54]:
lie,sentiment,review
0 f,n,'Mike\'s Pizza High Point, NY Service was ...
1 f,n,'i really like this buffet restaurant in M...
2 f,n,'After I went shopping with some of my fri...
3 f,n,'Olive Oil Garden was very disappointing. ...
4 f,n,'The Seven Heaven restaurant was never kno...

STEP 2: Pull out the labels

In [55]:
def get_labels(row):
    split_row = str(row).split(',')
    lie = split_row[0]
    sentiment = split_row[1]
    return [lie, sentiment, split_row[2:]]

df['all'] = df.apply(lambda row: get_labels(row['lie,sentiment,review']), axis=1)
df[:5]
Out[55]:
lie,sentiment,review all
0 f,n,'Mike\'s Pizza High Point, NY Service was ... [f, n, ['Mike\'s Pizza High Point, NY Service...
1 f,n,'i really like this buffet restaurant in M... [f, n, ['i really like this buffet restaurant ...
2 f,n,'After I went shopping with some of my fri... [f, n, ['After I went shopping with some of my...
3 f,n,'Olive Oil Garden was very disappointing. ... [f, n, ['Olive Oil Garden was very disappointi...
4 f,n,'The Seven Heaven restaurant was never kno... [f, n, ['The Seven Heaven restaurant was never...
In [56]:
df['lie'] = df.apply(lambda row: row['all'][0][0], axis=1)
df[:5]
Out[56]:
lie,sentiment,review all lie
0 f,n,'Mike\'s Pizza High Point, NY Service was ... [f, n, ['Mike\'s Pizza High Point, NY Service... f
1 f,n,'i really like this buffet restaurant in M... [f, n, ['i really like this buffet restaurant ... f
2 f,n,'After I went shopping with some of my fri... [f, n, ['After I went shopping with some of my... f
3 f,n,'Olive Oil Garden was very disappointing. ... [f, n, ['Olive Oil Garden was very disappointi... f
4 f,n,'The Seven Heaven restaurant was never kno... [f, n, ['The Seven Heaven restaurant was never... f
In [57]:
df['sentiment'] = df.apply(lambda row: row['all'][1][0], axis=1)
df[:5]
Out[57]:
lie,sentiment,review all lie sentiment
0 f,n,'Mike\'s Pizza High Point, NY Service was ... [f, n, ['Mike\'s Pizza High Point, NY Service... f n
1 f,n,'i really like this buffet restaurant in M... [f, n, ['i really like this buffet restaurant ... f n
2 f,n,'After I went shopping with some of my fri... [f, n, ['After I went shopping with some of my... f n
3 f,n,'Olive Oil Garden was very disappointing. ... [f, n, ['Olive Oil Garden was very disappointi... f n
4 f,n,'The Seven Heaven restaurant was never kno... [f, n, ['The Seven Heaven restaurant was never... f n
In [64]:
df['review'] = df.apply(lambda row: ''.join(row['all'][2]), axis=1)
df[:5]
Out[64]:
lie,sentiment,review all lie sentiment review
0 f,n,'Mike\'s Pizza High Point, NY Service was ... [f, n, ['Mike\'s Pizza High Point, NY Service... f n 'Mike\'s Pizza High Point NY Service was very ...
1 f,n,'i really like this buffet restaurant in M... [f, n, ['i really like this buffet restaurant ... f n 'i really like this buffet restaurant in Marsh...
2 f,n,'After I went shopping with some of my fri... [f, n, ['After I went shopping with some of my... f n 'After I went shopping with some of my friend ...
3 f,n,'Olive Oil Garden was very disappointing. ... [f, n, ['Olive Oil Garden was very disappointi... f n 'Olive Oil Garden was very disappointing. I ex...
4 f,n,'The Seven Heaven restaurant was never kno... [f, n, ['The Seven Heaven restaurant was never... f n 'The Seven Heaven restaurant was never known f...
In [65]:
clean_df = df.copy()
In [66]:
clean_df.drop(['lie,sentiment,review', 'all'], axis=1, inplace=True)
In [67]:
clean_df
Out[67]:
lie sentiment review
0 f n 'Mike\'s Pizza High Point NY Service was very ...
1 f n 'i really like this buffet restaurant in Marsh...
2 f n 'After I went shopping with some of my friend ...
3 f n 'Olive Oil Garden was very disappointing. I ex...
4 f n 'The Seven Heaven restaurant was never known f...
... ... ... ...
87 t p 'Pastablities is a locally owned restaurant in...
88 t p 'I like the Pizza at Dominoes for their specia...
89 t p 'It was a really amazing Japanese restaurant. ...
90 t p 'How do I even pick a best experience at Joe\'...
91 t p 'My sister and I ate at this restaurant called...

92 rows × 3 columns

STEP 3: Clean the data

In [68]:
def clean_rogue_characters(string):
    exclude = ['\\',"\'",'"']
    string = ''.join(string.split('\\n'))
    string = ''.join(ch for ch in string if ch not in exclude)
    return string

clean_df['review'] = clean_df['review'].apply( lambda x: clean_rogue_characters(x) )
clean_df['review'][0]
Out[68]:
'Mikes Pizza High Point NY Service was very slow and the quality was low. You would think they would know at least how to make good pizza not. Stick to pre-made dishes like stuffed pasta or a salad. You should consider dining else where.'

STEP 4: Export cleaned, formatted CSV

In [71]:
clean_df.to_csv('hw4_data.csv',index=False)
In [72]:
df = pd.read_csv('hw4_data.csv')
df[:5]
Out[72]:
lie sentiment review
0 f n Mikes Pizza High Point NY Service was very slo...
1 f n i really like this buffet restaurant in Marsha...
2 f n After I went shopping with some of my friend w...
3 f n Olive Oil Garden was very disappointing. I exp...
4 f n The Seven Heaven restaurant was never known fo...

STEP 5: Split df into data sets

LIE DFs

In [74]:
lie_df_f = df[df['lie'] == 'f']
lie_df_t = df[df['lie'] == 't']

SENTIMENT DFs

In [79]:
sent_df_n = df[df['sentiment'] == 'n']
sent_df_p = df[df['sentiment'] == 'p']
In [ ]: