-
Notifications
You must be signed in to change notification settings - Fork 1
/
feature_build.py
146 lines (119 loc) · 4.34 KB
/
feature_build.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
138
139
140
141
142
143
144
import numpy as np
import glob
import pandas as pd
import csv
file_root = '/data'
def feature(quantity_name):
if quantity_name == 'length':
txt_file = 'quantity_length.txt'
csv_file = 'features_length.csv'
append_number = '1'
elif quantity_name == 'time':
txt_file = 'quantity_time.txt'
csv_file = 'features_time.csv'
append_number = '2'
elif quantity_name == 'percent':
txt_file = 'quantity_percent.txt'
csv_file = 'features_percent.csv'
append_number = '3'
elif quantity_name == 'currency':
txt_file = 'quantity_currency.txt'
csv_file = 'features_currency.csv'
append_number = '4'
elif quantity_name == 'weight':
txt_file = 'quantity_weight.txt'
csv_file = 'features_weight.csv'
append_number = '5'
elif quantity_name == 'none':
txt_file = 'quantity_none.txt'
csv_file = 'features_none.csv'
append_number = '0'
with open(txt_file, 'r') as reader:
content = [x.strip().split(';') for x in reader.readlines()]
csv_path_list = ['/'.join([file_root] + x[:2]+['data.csv']) for x in content]
column_names_list = [x[2:] for x in content]
index = 0
for csv_path, column_names in zip(csv_path_list, column_names_list):
index = index + 1
print(index)
for single_column in column_names:
try:
df = pd.read_csv(csv_path, header=0, low_memory = True)
content = df[single_column].values.tolist()
content_list = [x for x in content if str(x) != 'nan']
##################################################################
# Features:
# Column Content:
# 1. Maximum value
# 2. Minimum value
# 3. Average value
# 4. Range value (maximum - minimum)
# 5. Length of the maximum value (when expressed as a string)
#
# Column Name:
# 1. Number of words
# 2. Number of characters
# 3. Presence of quantity-specific terms
##################################################################
maximum_value = max(content_list)
minimum_value = min(content_list)
average_value = reduce(lambda x, y: x + y, content_list) / len(content_list)
range_value = maximum_value - minimum_value
length_max_value = len(str(maximum_value))
number_char = len(single_column)
number_word = len(single_column.split())
length_type = 0
time_type = 0
percent_type = 0
currency_type = 0
weight_type = 0
length = ['meter', '(m)', 'in_m', 'mile', '(mi)', 'in_mi', 'inch', '(in)', 'in_in', 'feet', '(ft)', 'in_ft', 'length', 'height', 'width']
time = ['second', '(s)', 'in_s', '(sec)', 'in_sec', 'minute', '(min)', 'in_min', 'hour', '(hr)', 'in_hr', 'hrs', 'in_hrs', 'duration', 'time']
percent = ['percent', '%', 'percentage', 'accuracy']
currency = ['dollar', '$', 'usd', 'euro', 'EUR', 'pound', 'GBP', 'amount', 'cost']
weight = ['weight', 'gram', 'kilogram', '(g)', 'in_g', '(kg)', 'in_kg', 'pound', '(lb)', 'in_lb', 'ounce', '(oz)', 'in_oz', 'tons', '(t)', 'in_t']
for unit_length in length:
if unit_length in single_column.lower():
length_type = 1
for unit_time in time:
if unit_time in single_column.lower():
time_type = 1
for unit_percent in percent:
if unit_percent in single_column.lower():
percent_type = 1
for unit_currency in currency:
if unit_currency in single_column.lower():
currency_type = 1
for unit_weight in weight:
if unit_weight in single_column.lower():
weight_type = 1
new_row = []
new_row.append(csv_path.strip().split('/')[11])
new_row.append(csv_path.strip().split('/')[12])
new_row.append(single_column)
new_row.append(maximum_value)
new_row.append(minimum_value)
new_row.append(average_value)
new_row.append(range_value)
new_row.append(length_max_value)
new_row.append(number_char)
new_row.append(number_word)
new_row.append(length_type)
new_row.append(time_type)
new_row.append(percent_type)
new_row.append(currency_type)
new_row.append(weight_type)
new_row.append(append_number)
print("ok")
with open(r'features_length.csv', 'a') as f:
writer = csv.writer(f)
writer.writerow(new_row)
except Exception as e:
pass
if __name__ == "__main__":
feature('length')
feature('time')
feature('percent')
feature('currency')
feature('weight')
feature('none')