-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathsearch_meta.py
69 lines (57 loc) · 2.23 KB
/
search_meta.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
# -*- coding: utf-8 -*-
import argparse
from lib.collection_utils import *
from lib.io_utils import *
from lib.math_utils import *
from lib.processing_utils import *
import os
from pprint import pprint
import re
import sys
# input
parser = argparse.ArgumentParser()
parser.add_argument('-in', dest="INPUT_FILE", default="tmp/ia_fedflixnara.csv", help="Input file")
parser.add_argument('-key', dest="COLUMN_KEY", default="description", help="Column key to match against")
parser.add_argument('-pattern', dest="PATTERN", default="Department of [A-Za-z ]+", help="String pattern")
parser.add_argument('-features', dest="OUTPUT_KEY", default="collection", help="Features that the pattern maps to")
parser.add_argument('-out', dest="OUTPUT_FILE", default="", help="CSV output file; leave blank if update the same file")
parser.add_argument('-probe', dest="PROBE", action="store_true", help="Show plot?")
parser.add_argument('-verbose', dest="VERBOSE", action="store_true", help="Verbose messaging?")
parser.add_argument('-overwrite', dest="OVERWRITE", action="store_true", help="Overwrite value if it already exists?")
a = parser.parse_args()
# Parse arguments
INPUT_FILE = a.INPUT_FILE
COLUMN_KEY = a.COLUMN_KEY
OUTPUT_FILE = a.OUTPUT_FILE if len(a.OUTPUT_FILE) > 0 else INPUT_FILE
PATTERN = a.PATTERN.strip()
OUTPUT_KEY = a.OUTPUT_KEY.strip()
PROBE = a.PROBE
# Read data
headings, rows = readCsv(INPUT_FILE)
if OUTPUT_KEY not in headings:
headings.append(OUTPUT_KEY)
# Make sure output dirs exist
makeDirectories(OUTPUT_FILE)
pattern = re.compile(PATTERN+'|$')
rowCount = len(rows)
noMatchCount = 0
matched = [False for i in range(len(rows))]
for i, row in enumerate(rows):
match = pattern.search(row[COLUMN_KEY]).group()
if len(match) < 1:
if a.VERBOSE:
print("Did not match: %s" % row[COLUMN_KEY])
noMatchCount += 1
else:
matched[i] = True
# check to see if we can overwrite
if not a.OVERWRITE and OUTPUT_KEY in row and row[OUTPUT_KEY] != "":
continue
rows[i][OUTPUT_KEY] = match
print("Matched %s files" % (rowCount-noMatchCount))
print("Did not match %s files" % noMatchCount)
if a.PROBE:
counts = getCounts(rows, OUTPUT_KEY)
pprint(counts)
sys.exit()
writeCsv(OUTPUT_FILE, rows, headings)