|
| 1 | +import re |
| 2 | +import csv |
| 3 | +from collections import OrderedDict |
| 4 | +import numpy as np |
| 5 | +from .prune_rules import contains_empty_col, add_empty_col |
| 6 | +import datetime |
| 7 | +import random |
| 8 | +import string |
| 9 | +from textblob import TextBlob |
| 10 | +import featuretools as ft |
| 11 | +import pandas as pd |
| 12 | +import nltk |
| 13 | + |
| 14 | +try: |
| 15 | + nltk.data.find('') |
| 16 | +except LookupError: |
| 17 | + nltk.download() |
| 18 | +from nltk.corpus import stopwords |
| 19 | + |
| 20 | +try: |
| 21 | + nltk.data.find('corpus/stopwords') |
| 22 | +except LookupError: |
| 23 | + nltk.download('stopwords') |
| 24 | +from nltk import stem |
| 25 | + |
| 26 | +lemmatizer = stem.WordNetLemmatizer() |
| 27 | +stopwords_list = set(stopwords.words('english')) |
| 28 | + |
| 29 | + |
| 30 | +### Text to numeric ### |
| 31 | + |
| 32 | + |
| 33 | +def f_count_s(table, col, char): |
| 34 | + result_table = [] |
| 35 | + for row in table: |
| 36 | + try: |
| 37 | + count = str(len(re.findall(row[col], char))) |
| 38 | + except: |
| 39 | + count = str(row[col].count(char)) |
| 40 | + result_table.append(row[:col + 1] + [count, ] + row[col + 1:]) |
| 41 | + return result_table |
| 42 | + |
| 43 | + |
| 44 | +def f_number_of_words(table, col): |
| 45 | + return f_count_s(table, col, ' ') |
| 46 | + |
| 47 | + |
| 48 | +def f_number_of_sentences(table, col): |
| 49 | + return f_count_s(table, col, '.') |
| 50 | + |
| 51 | + |
| 52 | +def f_number_of_rows(table, col): |
| 53 | + return f_count_s(table, col, '\n') |
| 54 | + |
| 55 | + |
| 56 | +def f_number_of_questions(table, col): |
| 57 | + return f_count_s(table, col, '?') |
| 58 | + |
| 59 | + |
| 60 | +def f_number_of_emails(table, col): |
| 61 | + return f_count_s(table, col, r'\b[A-Za-z0-9._%+-]+@[A-Za-z0-9.-]+\.[A-Z|a-z]{2,}\b') |
| 62 | + |
| 63 | + |
| 64 | +def f_number_of_urls(table, col): |
| 65 | + return f_count_s(table, col, 'http[s]?://(?:[a-zA-Z]|[0-9]|[$-_@.&+]|[!*\(\),]|(?:%[0-9a-fA-F][0-9a-fA-F]))+') |
| 66 | + |
| 67 | + |
| 68 | +def f_number_of_ips(table, col): |
| 69 | + return f_count_s(table, col, r'^(\d{1,3})\.(\d{1,3})\.(\d{1,3})\.(\d{1,3})$') |
| 70 | + |
| 71 | + |
| 72 | +def f_number_of_phone_numbers(table, col): |
| 73 | + return f_count_s(table, col, |
| 74 | + r'[\+\d]?(\d{2,3}[-\.\s]??\d{2,3}[-\.\s]??\d{4}|\(\d{3}\)\s*\d{3}[-\.\s]??\d{4}|\d{3}[-\.\s]??\d{4})') |
| 75 | + |
| 76 | + |
| 77 | +def f_number_of_punctuations(table, col): |
| 78 | + return f_count_s(table, col, "[" + re.escape(string.punctuation) + "]") |
| 79 | + |
| 80 | + |
| 81 | +def f_number_of_stopwords(table, col): |
| 82 | + result_table = [] |
| 83 | + for row in table: |
| 84 | + count = str(len([word for word in row[col].split() if word in stopwords_list])) |
| 85 | + # result_table.append([count, ]) |
| 86 | + result_table.append(row[:col + 1] + [count, ] + row[col + 1:]) |
| 87 | + return result_table |
| 88 | + |
| 89 | + |
| 90 | +def f_len(table, col): |
| 91 | + result_table = [] |
| 92 | + for row in table: |
| 93 | + len_str = str(len(row[col])) |
| 94 | + result_table.append(row[:col + 1] + [len_str, ] + row[col + 1:]) |
| 95 | + return result_table |
| 96 | + |
| 97 | + |
| 98 | +### Text to class ### |
| 99 | + |
| 100 | +def f_exists_s(table, col, char): |
| 101 | + result_table = [] |
| 102 | + for row in table: |
| 103 | + exists = str(int(bool(len(re.findall(row[col], char))))) |
| 104 | + result_table.append(row[:col + 1] + [exists, ] + row[col + 1:]) |
| 105 | + return result_table |
| 106 | + |
| 107 | + |
| 108 | +def f_contains_multiple_words(table, col): |
| 109 | + return f_exists_s(table, col, ' ') |
| 110 | + |
| 111 | + |
| 112 | +def f_contains_multiple_sentences(table, col): |
| 113 | + return f_exists_s(table, col, '.') |
| 114 | + |
| 115 | + |
| 116 | +def f_contains_multiple_rows(table, col): |
| 117 | + return f_exists_s(table, col, '\n') |
| 118 | + |
| 119 | + |
| 120 | +def f_contains_a_questions(table, col): |
| 121 | + return f_exists_s(table, col, '?') |
| 122 | + |
| 123 | + |
| 124 | +def f_contains_an_email(table, col): |
| 125 | + return f_exists_s(table, col, r'\b[A-Za-z0-9._%+-]+@[A-Za-z0-9.-]+\.[A-Z|a-z]{2,}\b') |
| 126 | + |
| 127 | + |
| 128 | +def f_contains_an_url(table, col): |
| 129 | + return f_exists_s(table, col, 'http[s]?://(?:[a-zA-Z]|[0-9]|[$-_@.&+]|[!*\(\),]|(?:%[0-9a-fA-F][0-9a-fA-F]))+') |
| 130 | + |
| 131 | + |
| 132 | +def f_contains_an_ip(table, col): |
| 133 | + return f_exists_s(table, col, r'^(\d{1,3})\.(\d{1,3})\.(\d{1,3})\.(\d{1,3})$') |
| 134 | + |
| 135 | + |
| 136 | +def f_contains_a_phone_number(table, col): |
| 137 | + return f_exists_s(table, col, |
| 138 | + r'[\+\d]?(\d{2,3}[-\.\s]??\d{2,3}[-\.\s]??\d{4}|\(\d{3}\)\s*\d{3}[-\.\s]??\d{4}|\d{3}[-\.\s]??\d{4})') |
| 139 | + |
| 140 | + |
| 141 | +def f_contains_a_punctuation(table, col): |
| 142 | + return f_count_s(table, col, "[" + re.escape(string.punctuation) + "]") |
| 143 | + |
| 144 | + |
| 145 | +def f_contains_a_stopword(table, col): |
| 146 | + result_table = [] |
| 147 | + for row in table: |
| 148 | + count = str(bool(len([word for word in row[col].split() if word in stopwords_list]))) |
| 149 | + result_table.append(row[:col + 1] + [count, ] + row[col + 1:]) |
| 150 | + return result_table |
| 151 | + |
| 152 | + |
| 153 | +# New Transformations (NLP Data Cleaning) |
| 154 | + |
| 155 | +def f_remove_stopwords(table, col): |
| 156 | + result_table = [] |
| 157 | + for row in table: |
| 158 | + new_row = ' '.join([word for word in row[col].split() if word not in stopwords_list]) |
| 159 | + # result_table.append([count, ]) |
| 160 | + result_table.append(row[:col + 1] + [new_row, ] + row[col + 1:]) |
| 161 | + return result_table |
| 162 | + |
| 163 | + |
| 164 | +def f_remove_numeric(table, col): |
| 165 | + result_table = [] |
| 166 | + for row in table: |
| 167 | + new_row = ' '.join([word for word in row[col].split() if not word.isdigit()]) |
| 168 | + # result_table.append([count, ]) |
| 169 | + result_table.append(row[:col + 1] + [new_row, ] + row[col + 1:]) |
| 170 | + return result_table |
| 171 | + |
| 172 | + |
| 173 | +def f_remove_punctuation(table, col): |
| 174 | + # print(table) |
| 175 | + result_table = [] |
| 176 | + regex = re.compile('[%s]' % re.escape(string.punctuation)) |
| 177 | + for row in table: |
| 178 | + new_row = regex.sub('', row[col]) |
| 179 | + # result_table.append([count, ]) |
| 180 | + result_table.append(row[:col + 1] + [new_row, ] + row[col + 1:]) |
| 181 | + # print(result_table) |
| 182 | + return result_table |
| 183 | + |
| 184 | + |
| 185 | +def f_remove_url(table, col): |
| 186 | + result_table = [] |
| 187 | + regex = re.compile(r'https?://\S+|www\.\S+') |
| 188 | + for row in table: |
| 189 | + new_row = regex.sub('', row[col]) |
| 190 | + # result_table.append([count, ]) |
| 191 | + result_table.append(row[:col + 1] + [new_row, ] + row[col + 1:]) |
| 192 | + return result_table |
| 193 | + |
| 194 | + |
| 195 | +def f_remove_html_tags(table, col): |
| 196 | + result_table = [] |
| 197 | + regex = re.compile(r'<.*?>') |
| 198 | + for row in table: |
| 199 | + new_row = regex.sub('', row[col]) |
| 200 | + result_table.append(row[:col + 1] + [new_row, ] + row[col + 1:]) |
| 201 | + return result_table |
| 202 | + |
| 203 | + |
| 204 | +def f_spell_correction(table, col): |
| 205 | + result_table = [] |
| 206 | + for row in table: |
| 207 | + new_row = ' '.join([word for word in row[col].split() if not TextBlob(row[col]).correct()]) |
| 208 | + result_table.append(row[:col + 1] + [new_row, ] + row[col + 1:]) |
| 209 | + return result_table |
| 210 | + |
| 211 | + |
| 212 | +def f_lemmatization(table, col): |
| 213 | + result_table = [] |
| 214 | + for row in table: |
| 215 | + new_row = lemmatizer.lemmatize(row[col]) |
| 216 | + result_table.append(row[:col + 1] + [new_row, ] + row[col + 1:]) |
| 217 | + return result_table |
| 218 | + |
| 219 | + |
| 220 | +def f_lower(table, col): |
| 221 | + result_table = [] |
| 222 | + for row in table: |
| 223 | + new_row = row[col].lower() |
| 224 | + result_table.append(row[:col + 1] + [new_row, ] + row[col + 1:]) |
| 225 | + return result_table |
0 commit comments