Repository navigation
Expand file tree
/
Copy pathpreprocess.py
More file actions
56 lines (44 loc) · 1.54 KB
/
Copy pathpreprocess.py
File metadata and controls
56 lines (44 loc) · 1.54 KB
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
import csv
import re
from functools import reduce
from multiprocessing import Pool
# Open a filename for reading and execute the callback with the reader
# iterator.
def withcsv(filename, func):
with open(filename) as csvfile:
reader = csv.DictReader(csvfile)
return func(reader)
# Given a filename and a iterator data, write out each row to the csv file.
def outcsv(filename, data):
with open(filename, 'w') as outfile:
writer = csv.writer(outfile)
for row in data:
writer.writerow(row)
def process(data, func):
with Pool(10) as p:
return p.map(func, data)
# Returns a processed row
def preprocess(row):
return [
titleScore(row),
numFreeDownload(row),
numWatchMovie(row),
alphaRatio(row)
]
# Count return 0 or 1 depending on whether or not the row has a title.
def titleScore(row):
return 0 if row['title'] else 1
# Return the number of times a text has "free" or "download".
def numFreeDownload(row):
return len(re.findall("free|download", row['text'],re.IGNORECASE))
# Return the number of times a text has "movie" or "watch".
def numWatchMovie(row):
return len(re.findall("movie|watch", row['text'], re.IGNORECASE))
# Return the ratio of alpha characters to total characters * 20.
def alphaRatio(row):
numAlpha = reduce(isalpha, row['text'], 0)
multiplier = 20
return (numAlpha / len(row['text'])) * multiplier
# This is used for the reduce() in alphaRatio.
def isalpha(total, c):
return total + 1 if c.isalpha() else total