本文整理汇总了Python中nlmmanipulate.NlmManipulate.save_tree方法的典型用法代码示例。如果您正苦于以下问题:Python NlmManipulate.save_tree方法的具体用法?Python NlmManipulate.save_tree怎么用?Python NlmManipulate.save_tree使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类nlmmanipulate.NlmManipulate
的用法示例。
在下文中一共展示了NlmManipulate.save_tree方法的6个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。
示例1: prune
# 需要导入模块: from nlmmanipulate import NlmManipulate [as 别名]
# 或者: from nlmmanipulate.NlmManipulate import save_tree [as 别名]
def prune(self):
self.debug.print_debug(self, u'Deleting all stubs from article')
manipulate = NlmManipulate(self.gv)
tree = manipulate.load_dom_tree()
for p in tree.xpath('//xref[@ref-type="bibr" and @rid="TO_LINK"]'):
self.extract_contents(p)
manipulate.save_tree(tree)
示例2: run_prompt
# 需要导入模块: from nlmmanipulate import NlmManipulate [as 别名]
# 或者: from nlmmanipulate.NlmManipulate import save_tree [as 别名]
def run_prompt(self):
self.run(False)
self.debug.print_debug(self, u"Entering interactive mode")
prompt = Interactive(self.gv)
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
for p in tree.xpath('//xref[@ref-type="bibr"]'):
text = manipulate.get_stripped_text(p)
if "rid" in p.attrib and p.attrib["rid"] == "TO_LINK":
prompt.print_(u"Found an unhandled reference marker: {0}".format(text))
elif "rid" in p.attrib:
remote = next((x for x in ref_items if "id" in x.attrib and (x.attrib["id"] == p.attrib["rid"])), None)
remote_text = manipulate.get_stripped_text(remote)
prompt.print_(u'Found a handled reference marker: "{0}" which links to "{1}"'.format(text, remote_text))
opts = (
"Skip",
"Delete",
"deleTe all",
"Enter search",
"Ibid",
"enter Link id",
"skip Rest",
"show Context",
)
sel = ""
if delete_all:
sel = "d"
else:
sel = prompt.input_options(opts)
result = self.handle_input(manipulate, opts, p, prompt, ref_items, sel, tree=tree)
if result == "abort":
manipulate.save_tree(tree)
return
elif result == "delall":
delete_all = True
manipulate.save_tree(tree)
示例3: link_items
# 需要导入模块: from nlmmanipulate import NlmManipulate [as 别名]
# 或者: from nlmmanipulate.NlmManipulate import save_tree [as 别名]
def link_items(self, source_id, dest_id, manipulate=None, tree=None):
self.debug.print_debug(self, u'Attempting to link XREF {0} to REF {1}'.format(source_id, dest_id))
if manipulate is None:
manipulate = NlmManipulate(self.gv)
if tree is None:
tree = manipulate.load_dom_tree()
source = tree.xpath('//xref[@id="{0}"]'.format(source_id))[0]
dest = tree.xpath('//ref[@id="{0}"]'.format(dest_id))[0]
ReplaceObject(self.gv, source, dest).link()
manipulate.save_tree(tree)
示例4: run_prompt
# 需要导入模块: from nlmmanipulate import NlmManipulate [as 别名]
# 或者: from nlmmanipulate.NlmManipulate import save_tree [as 别名]
def run_prompt(self):
self.run(False)
self.debug.print_debug(self, u'Entering interactive mode')
prompt = Interactive(self.gv)
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
for p in tree.xpath('//xref[@ref-type="bibr"]'):
text = manipulate.get_stripped_text(p)
prompt.print_(prompt.colorize('green',("-" * 80)))
if 'rid' in p.attrib and p.attrib['rid'] == 'TO_LINK':
prompt.print_(u"Found an unhandled reference marker: {0}".format(text))
elif 'rid' in p.attrib:
remote = next((x for x in ref_items if 'id' in x.attrib and (x.attrib['id'] == p.attrib['rid'])), None)
remote_text = manipulate.get_stripped_text(remote)
prompt.print_(u"Found a handled reference marker: \"{0}\" which links to \"{1}\"".format(text,
remote_text))
opts = ('Skip', 'Delete', 'deleTe all', 'Enter search', 'Ibid', 'enter Link id',
'skip Rest', 'show Context')
sel = ''
if delete_all:
sel = 'd'
else:
sel = prompt.input_options(opts)
result = self.handle_input(manipulate, opts, p, prompt, ref_items, sel, tree=tree)
if result == 'abort':
manipulate.save_tree(tree)
return
elif result == 'delall':
delete_all = True
manipulate.save_tree(tree)
示例5: process_zotero
# 需要导入模块: from nlmmanipulate import NlmManipulate [as 别名]
# 或者: from nlmmanipulate.NlmManipulate import save_tree [as 别名]
def process_zotero(self):
from zotero import libzotero
zotero = libzotero.LibZotero(unicode(self.gv.settings.get_setting(u'zotero', self)), self.gv)
manipulate = NlmManipulate(self.gv)
master_tree = manipulate.load_dom_tree()
tree = master_tree.xpath('//back/ref-list/ref')
for element in tree:
original_term = manipulate.get_stripped_text(element)
term = original_term
#term = re.sub(r'(.+?)(\(.+?\))(.*)', r'\1\3', term)
term = re.sub(r'(?<![0-9])[1-9][0-9]{0,2}(?![0-9])', r'', term)
term = re.sub(r'[\-,\.\<\>\(\)\;\:\@\'\#\~\}\{\[\]\"\!\\/]', '', term)
term = re.sub(u'[^\s]+?\s[Ee]dition', u' ', term)
term = re.sub(u'\s.\s', u' ', term)
term = re.sub(u'(?<=[A-Z])\.', u' ', term)
term = term.replace(u'“', u'')
term = term.replace(u'\'s', u'')
term = term.replace(u'’s', u'')
term = term.replace(u'’', u'')
term = term.replace(u' Ed. ', u' ')
term = term.replace(u' Ed ', u' ')
term = term.replace(u' Trans. ', u' ')
term = term.replace(u' Trans ', u' ')
term = term.replace(u' trans ', u' ')
term = term.replace(u' trans. ', u' ')
term = term.replace(u' by. ', u' ')
term = term.replace(u' by ', u' ')
term = term.replace(u' ed. ', u' ')
term = term.replace(u' ed ', u' ')
term = term.replace(u' In ', u' ')
term = term.replace(u' in ', u' ')
term = term.replace(u' print ', u' ')
term = term.replace(u' Print ', u' ')
term = term.replace(u' and ', u' ')
term = term.replace(u'”', u'')
term = re.sub(r'[Aa]ccessed', '', term)
term = re.sub(r'meTypesetbr', '', term)
term = re.sub(r'\s+', ' ', term)
results = zotero.search(term.strip())
while len(results) == 0 and len(term.strip().split(' ')) > 2:
# no results found.
# begin iterating backwards
term = ' '.join(term.strip().split(' ')[:-1])
results = zotero.search(term.strip())
if len(results) == 1:
res = results[0].JATS_format()
if res is not None:
ref = etree.fromstring(res)
if 'id' in element.attrib:
ref.attrib['id'] = element.attrib['id']
element.addnext(ref)
original_term = re.sub(u'--', u'', original_term)
comment = etree.Comment(original_term)
ref.addnext(comment)
element.tag = 'REMOVE'
etree.strip_elements(master_tree, 'REMOVE')
manipulate.save_tree(master_tree)
示例6: run
# 需要导入模块: from nlmmanipulate import NlmManipulate [as 别名]
# 或者: from nlmmanipulate.NlmManipulate import save_tree [as 别名]
def run(self, interactive):
if interactive:
self.run_prompt()
return
manipulate = NlmManipulate(self.gv)
tree = manipulate.load_dom_tree()
# pre-cleanup: remove all empty ext-links as these break the linker
items_to_clean = tree.xpath('//ext-link')
count = 0
for item in items_to_clean:
if '{http://www.w3.org/1999/xlink}href' in item.attrib and \
item.attrib['{http://www.w3.org/1999/xlink}href'] == '':
count += 1
item.tag = 'REMOVE'
etree.strip_tags(item.getparent(), 'REMOVE')
if count > 0:
manipulate.save_tree(tree)
self.debug.print_debug(self, u'Removed {0} blank ext-link tags'.format(count))
ref_items = tree.xpath('//back/ref-list/ref')
self.clean_ref_items(tree, ref_items, manipulate)
# handle numbered reference items
references_and_numbers = {}
for ref in ref_items:
text = manipulate.get_stripped_text(ref)
ref_match = re.compile('^(?P<number>\d+)\.*')
result = ref_match.match(text)
if result:
references_and_numbers[result.group('number')] = ref
parsed = self.process_ibid_authors(ref_items)
if parsed > 0:
manipulate.save_tree(tree)
self.debug.print_debug(self, u'Replace {0} instances of "---." at start of references'.format(parsed))
to_link = []
to_stub = []
square_bracket_count = {}
for p in tree.xpath('//sec//p[not(mml:math)] | //td',
namespaces={'mml': 'http://www.w3.org/1998/Math/MathML'}):
text = manipulate.get_stripped_text(p)
reference_test = re.compile('\((?P<text>[^%]+?)\)')
matches = reference_test.finditer(text)
# exclude any square brackets with numbers inside
sub_match = re.compile('\[(?P<square>\d*[,\-;\d\s]*)\]')
smatch = sub_match.search(text)
if smatch:
smatches = sub_match.finditer(text)
for smatch in smatches:
self.debug.print_debug(self, u'Handling references in square '
u'brackets: [{0}] '.format(smatch.group('square')))
for item in re.split(';|,', smatch.group('square')):
if '-' in item:
parent, tail = manipulate.find_text(p, item)
if parent is not None:
new_string = ''
try:
split_range = item.strip().split('-')
for no in range(int(split_range[0]), int(split_range[1]) + 1):
new_string += str(no) + ','
except:
self.debug.print_debug(self, u'Unable to parse reference '
u'number in range {0}'.format(item))
break
if new_string.endswith(',') and not item.endswith(','):
new_string = new_string[0:len(new_string) - 1]
if tail and new_string != '':
parent.tail = parent.tail.replace(item, new_string)
elif not tail and new_string != '':
parent.text = parent.text.replace(item, new_string)
try:
split_range = item.strip().split('-')
for no in range(int(split_range[0]), int(split_range[1]) + 1):
self.debug.print_debug(self, u'Parsing reference '
u'number in range {0}'.format(str(no)))
#.........这里部分代码省略.........