Repository navigation
Expand file tree
/
Copy pathlabel.py
More file actions
109 lines (78 loc) · 2.97 KB
/
Copy pathlabel.py
File metadata and controls
109 lines (78 loc) · 2.97 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
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
import pandas as pd
# Loading Training Data csv
df = pd.read_csv("TrainingData.csv")
inputRec = 0
results = pd.DataFrame(columns = ['RecordID', 'Form'])
def search(record):
# Printing Link
print('\033[1m' +'\nLink:' + '\033[0m', 'https://search-proquest-com.proxy.library.nyu.edu/docview/' + str(record))
text = df.loc[df['RecordID'] == int(record), 'text'].iloc[0]
# Printing Text
print('\033[1m' + 'Record Text:' + '\033[0m', text)
# Defining Target Variable
target = 0
# Stores indices to be converted into tuples
store = [] # Stores tuples
while target != 'done':
target = input("\nInput a target string. Type \033[1m'done'\033[0m to input new RecordID.\
\nPress \033[1mEnter\033[0m to see text again. \033[1m'cl'\033[0m clear the store:\n")
if target == 'done':
break
elif target == '':
print(text)
elif target == 'cl':
store=[]
else:
try:
for i in range(len(text)):
indices = []
# Storing index into indices
if text[i:i+len(target)] == target:
indices.append(i)
indices.append(i+len(target))
entity = input("\n Input Entity Type: ")
indices.append(entity)
tup = tuple(indices)
print('\n', tup)
store.append(tup)
print('\n', store)
continue
except:
print('\033[1m' + "\n Target not found" + '\033[0m')
continue
continue
append = 0
while append != 'y' or 'n':
append = input("\nAppend RecordID? y/n:\n")
global resultsDF
if append == 'y':
# Appending tuples into Data Frame
fullForm = ('(\"%s\", {\"entities\": %s }),') % (text, store)
d = {'RecordID': record,
'Form': fullForm
}
resultsDF = pd.DataFrame(d, index=[0])
break
elif append == 'n':
resultsDF = pd.DataFrame(columns = ['RecordID', 'Form'], index=[0])
break
else:
print("\nInvalid Response. Try again.")
continue
while inputRec != '':
inputRec = input("Input a RecordID. If you are finished, press \033[1mEnter\033[0m: \n")
if inputRec.isnumeric():
search(inputRec)
results = pd.concat([results, resultsDF], ignore_index=True)
results = results.dropna()
print('\n', results, '\n')
elif inputRec == '':
# Storing Results into CSV
print(results.head())
results.to_csv('./results.csv', index=True, encoding='utf-8')
break
else:
print('\033[1m' + "Invalid RecordID. Please try again.\n" + '\033[0m')
continue
else:
exit()