-
Notifications
You must be signed in to change notification settings - Fork 0
/
beehive.py
139 lines (117 loc) · 4.44 KB
/
beehive.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
#!/usr/bin/env python3
# -*- coding: utf-8 -*-
"""
Created on Wed Sep 4 11:03:56 2019
@author: davidnelson
"""
import csv
def find_numbers(entry):
'''
This function will determine if string input contains numbers or not.
'''
return any(char.isdigit() for char in entry)
def alpha_annotator(item, ref):
'''
This function will create links in HTML syntax to alphabetical
entries in the Beehive
'''
pid = item['pid'].to_list()
pid = pid[0]
first_letter = item['first_letter'].to_list()
first_letter = first_letter[0]
if first_letter.startswith(('A', 'B', 'C', 'D')):
return f"<a href='/digital-beehive/alpha1/{pid}/'>{ref}</a>"
elif first_letter.startswith(('E', 'F', 'G', 'H')):
return f"<a href='/digital-beehive/alpha2/{pid}/'>{ref}</a>"
elif first_letter.startswith(('I', 'K', 'L', 'M', 'N')):
return f"<a href='/digital-beehive/alpha3/{pid}/'>{ref}</a>"
elif first_letter.startswith(('O', 'P', 'Q', 'R', 'S')):
return f"<a href='/digital-beehive/alpha4/{pid}/'>{ref}</a>"
else:
return f"<a href='/digital-beehive/alpha5/{pid}/'>{ref}</a>"
def num_annotator(entry, ref):
'''
This function will create links in HTML syntax to numerical entries
in the Beehive.
'''
try:
pid = entry['pid'].to_list()
pid = pid[0]
num = entry['entry'].to_list()
num = int(num[0])
except AttributeError:
pid = entry['pid']
num = int(entry['entry'])
if int(num) <= 250:
return f"<a href='/digital-beehive/num1/{pid}/'>{ref}</a>"
elif int(num) <= 500:
return f"<a href='/digital-beehive/num2/{pid}/'>{ref}</a>"
elif int(num) <= 725:
return f"<a href='/digital-beehive/num3/{pid}/'>{ref}</a>"
elif int(num) <= 1000:
return f"<a href='/digital-beehive/num4/{pid}/'>{ref}</a>"
def corrections_annotator(entry, ref):
pid = entry['pid'].to_list()
pid = pid[0]
if pid.startswith('num'):
return num_annotator(entry, ref)
elif pid.startswith('alpha'):
return alpha_annotator(entry, ref)
def index_annotator(item, ref):
'''
This function will create links in HTML syntax to index headers in
the Beehive.
'''
pid = item['pid'].to_list()
pid = pid[0]
first_letter = item['first_letter'].to_list()
first_letter = first_letter[0]
if first_letter.startswith(('A', 'B', 'C', 'D')):
return f"<a href='/digital-beehive/index1/{pid}/'>{ref}</a>"
elif first_letter.startswith(('E', 'F', 'G', 'H')):
return f"<a href='/digital-beehive/index2/{pid}/'>{ref}</a>"
elif first_letter.startswith(('I', 'K', 'L', 'M', 'N')):
return f"<a href='/digital-beehive/index3/{pid}/'>{ref}</a>"
elif first_letter.startswith(('O', 'P', 'Q', 'R', 'S')):
return f"<a href='/digital-beehive/index4/{pid}/'>{ref}</a>"
else:
return f"<a href='/digital-beehive/index5/{pid}/'>{ref}</a>"
def write_csv(infile, outfile, cond, data):
with open(infile, 'r') as ip, open(outfile, 'w') as f:
reader = csv.DictReader(ip, delimiter=',')
fields = reader.fieldnames
writer = csv.DictWriter(f, delimiter=',', fieldnames=fields)
writer.writeheader()
for row in reader:
if row[data].startswith(cond):
writer.writerow(row)
def write_num_csv(infile, outfile, cond1, cond2):
with open(infile, 'r') as ip, open(outfile, 'w') as f:
reader = csv.DictReader(ip, delimiter=',')
fields = reader.fieldnames
writer = csv.DictWriter(f, delimiter=',', fieldnames=fields)
writer.writeheader()
for row in reader:
if row['pid'].startswith('num'):
if cond1 <= int(row['entry']) <= cond2:
writer.writerow(row)
def add_or_append(dictionary, key, value):
if key not in dictionary:
dictionary[key] = []
dictionary[key].append(value)
def load_corrections(data):
with open(data, 'r') as f:
reader = csv.DictReader(f, delimiter=',')
corrections = {}
for row in reader:
corrections.update({row['input']: row['match']})
return corrections
def load_issues(data):
with open(data, 'r') as f:
reader = csv.DictReader(f, delimiter=',')
issues = {}
for row in reader:
add_or_append(issues, row['item'], row['problem'])
for i in issues.keys():
issues[i] = '|'.join(issues[i])
return issues