-
Notifications
You must be signed in to change notification settings - Fork 33
/
bibliographyclassifier.py
executable file
·176 lines (129 loc) · 5.62 KB
/
bibliographyclassifier.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
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
#!/usr/bin/env python
"""bibliographyclassfier.py: a tool to manipulate reference lists
Usage:
bibliographyclassifier.py confirm <input> [options]
Options:
-d, --debug Enable debug output
--interactive Prompt the user to assist in interactive tagging
-h, --help Show this screen.
-v, --version Show version.
"""
from teimanipulate import *
from nlmmanipulate import *
from docopt import docopt
from bare_globals import GV
from interactive import Interactive
__author__ = "Martin Paul Eve"
__email__ = "martin@martineve.com"
"""
A class that assists with bibliography classification.
"""
from debug import Debuggable
import codecs
class BibliographyClassifier(Debuggable):
def __init__(self, global_variables):
self.gv = global_variables
self.debug = self.gv.debug
Debuggable.__init__(self, 'Bibliography Classifier')
def linguistic_cues(self, manipulate, tree):
self.debug.print_debug(self, u'Using linguistic cue method to classify bibliography')
language_list = self.gv.settings.get_setting('reference-languages', self).split(',')
for language in language_list:
with codecs.open('{0}/language/ref_marker_{1}.txt'.format(self.gv.script_dir, language),
encoding='utf-8') as lang_file:
lines = lang_file.read().split('\n')
for line in lines:
if line.strip() != '':
if manipulate.find_references_from_cue(line, tree):
return True
def run(self):
if int(self.gv.settings.args['--aggression']) < int(self.gv.settings.get_setting('bibliographyclassifier', self,
domain='aggression')):
self.debug.print_debug(self, u'Aggression level less than 4: exiting module.')
return
tei_manipulator = TeiManipulate(self.gv)
tree = tei_manipulator.load_dom_tree()
found = tei_manipulator.find_reference_list_in_word_list(tree)
if not found:
found = self.linguistic_cues(tei_manipulator, tree)
tei_manipulator.enclose_bibliography_tags('//tei:p[@rend="Bibliography"]', 'back', 'div', 'type', 'bibliogr')
def unconfirm(self, p, tree):
# find a potential reference point
old_reference_points = tree.xpath('//*[@meTypesetRender]')
parent = False
old_reference_point = None
if len(old_reference_points) > 0:
old_reference_point = old_reference_points[0]
# determine if ref-list-before or ref-list-parent
if old_reference_point.attrib['meTypesetRender'] == 'ref-list-parent':
parent = True
else:
# get parent (ref-list)'s parent (back)'s previous sibling (sec)
old_reference_point = tree.xpath('//sec[last()]')[0]
parent = True
if old_reference_point is not None:
p.tag = 'p'
if parent:
Manipulate.append_safe(old_reference_point, p, self)
else:
old_reference_point.addnext(p)
def handle_input(self, manipulate, opts, p, prompt, sel, tree, text):
if sel == 'a':
prompt.print_(u"Leaving interactive mode on user command")
return "abort"
elif sel == 'c':
# confirm
pass
elif sel == 'o':
# confirm all
return "confirmall"
elif sel == 'u':
# delete the surrounding xref
self.debug.print_debug(self, u'Unconfirming reference {0}'.format(text))
self.unconfirm(p, tree)
pass
elif sel == 'n':
# delete all
self.debug.print_debug(self, u'Unconfirming reference {0}'.format(text))
self.unconfirm(p, tree)
return "delall"
def run_prompt(self, interactive):
if not interactive:
self.debug.fatal_error(self, 'Cannot enter confirmation mode without interactive flag')
prompt = Interactive(self.gv)
opts = ('Confirm', 'Unconfirm', 'cOnfirm all', 'uNconfirm all', 'Abort')
manipulate = NlmManipulate(self.gv)
tree = manipulate.load_dom_tree()
ref_items = tree.xpath('//back/ref-list/ref')
# note that we don't want to exit even if there are no references to link because the user may want to delete
# some
delete_all = False
confirm_all = False
for p in tree.xpath('//ref'):
text = manipulate.get_stripped_text(p)
sel = ''
if delete_all:
sel = 'u'
elif confirm_all:
sel = 'c'
else:
prompt.print_(u"Please confirm whether the following is a bibliographic reference: {0}".format(text))
sel = prompt.input_options(opts)
result = self.handle_input(manipulate, opts, p, prompt, sel, tree, text)
if result == 'abort':
return
elif result == 'delall':
delete_all = True
elif result == 'confirmall':
confirm_all = True
manipulate.save_tree(tree)
def main():
args = docopt(__doc__, version='meTypeset 0.1')
bare_gv = GV(args)
if args['--debug']:
bare_gv.debug.enable_debug()
bc_instance = BibliographyClassifier(bare_gv)
if args['confirm']:
bc_instance.run_prompt(args['--interactive'])
if __name__ == '__main__':
main()