This repository has been archived by the owner on Apr 25, 2020. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 65
/
Copy pathutil3.py
137 lines (128 loc) · 4.81 KB
/
util3.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
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
# -*- encoding:ISO-8859-1 -*-
import warnings
warnings.filterwarnings("ignore")
import time
import numpy as np
import pandas as pd
from nltk.stem.porter import *
stemmer = PorterStemmer()
import re
import random
random.seed(2016)
stop_w = ['for', 'xbi', 'and', 'in', 'th', 'on', 'sku', 'with', 'what', 'from', 'that', 'less', 'er', 'ing'] #'electr','paint','pipe','light','kitchen','wood','outdoor','door','bathroom'
strNum = {'zero': 0, 'one': 1, 'two': 2, 'three': 3, 'four': 4, 'five': 5, 'six': 6, 'seven': 7, 'eight': 8, 'nine': 9}
def str_stem(s):
if isinstance(s, str):
s = re.sub(r"(\w)\.([A-Z])", r"\1 \2", s) #Split words with a.A
s = s.lower()
s = s.replace(" "," ")
s = s.replace(",","") #could be number / segment later
s = s.replace("$"," ")
s = s.replace("?"," ")
s = s.replace("-"," ")
s = s.replace("//","/")
s = s.replace("..",".")
s = s.replace(" / "," ")
s = s.replace(" \\ "," ")
s = s.replace("."," . ")
s = re.sub(r"(^\.|/)", r"", s)
s = re.sub(r"(\.|/)$", r"", s)
s = re.sub(r"([0-9])([a-z])", r"\1 \2", s)
s = re.sub(r"([a-z])([0-9])", r"\1 \2", s)
s = s.replace(" x "," xbi ")
s = re.sub(r"([a-z])( *)\.( *)([a-z])", r"\1 \4", s)
s = re.sub(r"([a-z])( *)/( *)([a-z])", r"\1 \4", s)
s = s.replace("*"," xbi ")
s = s.replace(" by "," xbi ")
s = re.sub(r"([0-9])( *)\.( *)([0-9])", r"\1.\4", s)
s = re.sub(r"([0-9]+)( *)(inches|inch|in|')\.?", r"\1in. ", s)
s = re.sub(r"([0-9]+)( *)(foot|feet|ft|'')\.?", r"\1ft. ", s)
s = re.sub(r"([0-9]+)( *)(pounds|pound|lbs|lb)\.?", r"\1lb. ", s)
s = re.sub(r"([0-9]+)( *)(square|sq) ?\.?(feet|foot|ft)\.?", r"\1sq.ft. ", s)
s = re.sub(r"([0-9]+)( *)(cubic|cu) ?\.?(feet|foot|ft)\.?", r"\1cu.ft. ", s)
s = re.sub(r"([0-9]+)( *)(gallons|gallon|gal)\.?", r"\1gal. ", s)
s = re.sub(r"([0-9]+)( *)(ounces|ounce|oz)\.?", r"\1oz. ", s)
s = re.sub(r"([0-9]+)( *)(centimeters|cm)\.?", r"\1cm. ", s)
s = re.sub(r"([0-9]+)( *)(milimeters|mm)\.?", r"\1mm. ", s)
s = s.replace("¡ã"," degrees ")
s = re.sub(r"([0-9]+)( *)(degrees|degree)\.?", r"\1deg. ", s)
s = s.replace(" v "," volts ")
s = re.sub(r"([0-9]+)( *)(volts|volt)\.?", r"\1volt. ", s)
s = re.sub(r"([0-9]+)( *)(watts|watt)\.?", r"\1watt. ", s)
s = re.sub(r"([0-9]+)( *)(amperes|ampere|amps|amp)\.?", r"\1amp. ", s)
s = s.replace(" "," ")
s = s.replace(" . "," ")
#s = (" ").join([z for z in s.split(" ") if z not in stop_w])
s = (" ").join([str(strNum[z]) if z in strNum else z for z in s.split(" ")])
s = (" ").join([stemmer.stem(z) for z in s.split(" ")])
s = s.lower()
s = s.replace("toliet","toilet")
s = s.replace("airconditioner","air conditioner")
s = s.replace("vinal","vinyl")
s = s.replace("vynal","vinyl")
s = s.replace("skill","skil")
s = s.replace("snowbl","snow bl")
s = s.replace("plexigla","plexi gla")
s = s.replace("rustoleum","rust-oleum")
s = s.replace("whirpool","whirlpool")
s = s.replace("whirlpoolga", "whirlpool ga")
s = s.replace("whirlpoolstainless","whirlpool stainless")
return s
else:
return "null"
def seg_words(str1, str2):
str2 = str2.lower()
str2 = re.sub("[^a-z0-9./]"," ", str2)
str2 = [z for z in set(str2.split()) if len(z)>2]
words = str1.lower().split(" ")
s = []
for word in words:
if len(word)>3:
s1 = []
s1 += segmentit(word,str2,True)
if len(s)>1:
s += [z for z in s1 if z not in ['er','ing','s','less'] and len(z)>1]
else:
s.append(word)
else:
s.append(word)
return (" ".join(s))
def segmentit(s, txt_arr, t):
st = s
r = []
for j in range(len(s)):
for word in txt_arr:
if word == s[:-j]:
r.append(s[:-j])
#print(s[:-j],s[len(s)-j:])
s=s[len(s)-j:]
r += segmentit(s, txt_arr, False)
if t:
i = len(("").join(r))
if not i==len(st):
r.append(st[i:])
return r
def str_common_word(str1, str2):
words, cnt = str1.split(), 0
for word in words:
if str2.find(word)>=0:
cnt+=1
return cnt
def str_whole_word(str1, str2, i_):
cnt = 0
while i_ < len(str2):
i_ = str2.find(str1, i_)
if i_ == -1:
return cnt
else:
cnt += 1
i_ += len(str1)
return cnt
from typo_dict import typo_dict
def correct_typo(s):
# global typo_count
if s in typo_dict:
# typo_count += 1
return typo_dict[s]
else:
return s