-
Notifications
You must be signed in to change notification settings - Fork 115
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
5 changed files
with
88 additions
and
8 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,30 @@ | ||
from __future__ import absolute_import | ||
from __future__ import division | ||
from __future__ import print_function | ||
from __future__ import unicode_literals | ||
|
||
from .base import BaseParser | ||
from .elements import W, R, Optional | ||
from ..model import StringType, Compound | ||
from .actions import merge | ||
|
||
|
||
doi = ((R('[Dd][Oo][Ii]') + Optional(W(':'))).hide() + | ||
R('10[.][0-9]{4,}(?:[.][0-9]+)*') + | ||
W('/') + | ||
R('(?:(?!["&\'<>])\S)+')).add_action(merge)('doi') | ||
|
||
|
||
class DoiParser(BaseParser): | ||
"""""" | ||
root = doi | ||
|
||
def __init__(self): | ||
pass | ||
|
||
def interpret(self, result, start, end): | ||
c = Compound( | ||
doi=result.xpath('./text()') | ||
) | ||
|
||
yield c |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,49 @@ | ||
# -*- coding: utf-8 -*- | ||
""" | ||
test_parse_doi | ||
~~~~~~~~~~~~~~ | ||
Test DOI parser. | ||
""" | ||
|
||
from __future__ import absolute_import | ||
from __future__ import division | ||
from __future__ import print_function | ||
from __future__ import unicode_literals | ||
import logging | ||
import unittest | ||
|
||
from lxml import etree | ||
|
||
from chemdataextractor.doc.text import Sentence | ||
from chemdataextractor.parse.doi import doi | ||
|
||
logging.basicConfig(level=logging.DEBUG) | ||
log = logging.getLogger(__name__) | ||
|
||
|
||
class TestParseDOI(unittest.TestCase): | ||
maxDiff = None | ||
|
||
def do_parse(self, input, expected): | ||
s = Sentence(input) | ||
log.debug(s) | ||
log.debug(s.tagged_tokens) | ||
result = next(doi.scan(s.tagged_tokens))[0] | ||
log.debug(etree.tostring(result, pretty_print=True, encoding='unicode')) | ||
self.assertEqual(expected, etree.tostring(result, encoding='unicode')) | ||
|
||
def test_doi1(self): | ||
tests = [ | ||
'DOI:10.1021/jo101758t', | ||
'doi:10.3390/molecules201219848\n hello world', | ||
'Molecules 2015, 20(12), 22272-22285; doi:10.3390/molecules201219846' | ||
] | ||
values = [ | ||
'<doi>10.1021/jo101758t</doi>', | ||
'<doi>10.3390/molecules201219848</doi>', | ||
'<doi>10.3390/molecules201219846</doi>' | ||
] | ||
for test, expected in zip(tests, values): | ||
self.do_parse(test, expected) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters