File size: 2,041 Bytes
ed64398
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
25b96ed
ed64398
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
import re

def correct_spellings(phrase):
  corrections = {
    'pricethe': 'price the',
    'loudnessand': 'loudness and',
    'muffeled': 'muffled',
    'expidite': 'expedite',
    'suerb': 'superb',
    'eeplaces': 'ear pieces',
    'exilent': 'excellent',
    'worthable': 'worth able',
    'soundaverage': 'sound average',
    'bukd': 'build',
    'breliant': 'brilliant',
    'dvsvinyl': 'dvd vinyl',
    'qudoes': 'kudos',
    'extarnal': 'external',
    'heaten': 'heats',
    'iseent': 'is not',
    'worth the prize': 'worth the price',
    "laptop's": 'laptop',
    "laptop’s": 'laptop',
    "aslo": "also",
    "qulity": "quality",
    "qaulity": "quality",
    "sable": "cable" 
  }
  for k, v in corrections.items():
    phrase = phrase.replace(f"{k}", v)
  return phrase

def undo_contractions(phrase):
    # specific
    phrase = re.sub(r"won[\'’]t", "will not", phrase)
    phrase = re.sub(r"can[\'’]t", "can not", phrase)

    # general
    phrase = re.sub(r"n[\'’]t", " not", phrase)
    phrase = re.sub(r"[\'’]re", " are", phrase)
    phrase = re.sub(r"[\'’]s", " is", phrase)
    phrase = re.sub(r"[\'’]d", " would", phrase)
    phrase = re.sub(r"[\'’]ll", " will", phrase)
    phrase = re.sub(r"[\'’]t", " not", phrase)
    phrase = re.sub(r"[\'’]ve", " have", phrase)
    phrase = re.sub(r"[\'’]m", " am", phrase)
    return phrase

emoji_regex = ''

def preprocess_reviews(reviews):
  reviews['text'] = reviews['title'] + ' . ' + reviews['review']

  reviews['text_cleaned'] = (reviews['text']
    .fillna('')
    .str.replace(r'([a-z]+)([A-Z])', r'\1 \2') # badProduct bad Product
    .str.lower()
    .str.replace(emoji_regex, '')
    .str.replace('\n', '.')
    .str.replace(r'\s*\.+\s*', '. ') # dots and spaces
    .str.replace(r'([\{\(\[\}\)\]])', r' \1 ') # spaces between parenthesis
    .str.replace(r'([:])', r' \1 ') # spaces between :
    .str.replace(r'(\d+\.?\d*)', r' \1 ') # spaces between numbers
    .apply(correct_spellings)
    .apply(undo_contractions)
  )

  return reviews