forked from CornellNLP/4300-Template-Spring-2023
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmore_preprocessing.py
109 lines (54 loc) · 1.49 KB
/
more_preprocessing.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
#!/usr/bin/env python
# coding: utf-8
# In[15]:
import numpy as np
import pandas as pd
# In[16]:
import re
import json
from glob import glob
import os
from io import StringIO
from itertools import groupby
import pickle
import numpy as np
import bs4
get_ipython().run_line_magic('matplotlib', 'inline')
import matplotlib.pyplot as plt
# In[17]:
cleancoffee_df = pd.read_csv("data/archive/scraped-categories-2.csv")
# In[18]:
reviews = cleancoffee_df['review'].values.tolist()
# In[19]:
def tokenize(text):
return re.findall('[a-z]+', text.lower())
# In[20]:
def tokenize_reviews(tokenize_method,input_review):
final = []
for i in range(0, len(input_review)):
text = input_review[i]
final = final + tokenize(text)
return final
# In[21]:
review_tokens = tokenize_reviews(tokenize, reviews)
# In[23]:
review_tokens_distinct = []
for i in range(0, len(review_tokens)):
if review_tokens[i] not in review_tokens_distinct:
review_tokens_distinct.append(review_tokens[i])
# In[24]:
tokens_count = {}
for i in range(0, len(review_tokens)):
if review_tokens[i] in tokens_count:
tokens_count[review_tokens[i]]+=1
else:
tokens_count[review_tokens[i]]=1
# In[25]:
keys = list(tokens_count.keys())
for key in keys:
tokens_count[key] = tokens_count[key]/len(review_tokens)
# In[26]:
tokens_count = dict(sorted(tokens_count.items(), key=lambda item: item[1], reverse = True))
# In[28]:
tokens_count
# In[ ]: