Skip to content
This repository has been archived by the owner on Jun 26, 2020. It is now read-only.

changed method to methods and added args #13

Closed
wants to merge 1 commit into from
Closed
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
52 changes: 28 additions & 24 deletions pymed/article.py
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,9 @@ def __init__(
self.article_id = kwargs.get("article_id")
self.title = kwargs.get("title")
self.abstract = kwargs.get("abstract")
self.methods = kwargs.get("methods")
self.conclusion = kwargs.get("conclusion")
self.results = kwargs.get("results")
self.keywords = kwargs.get("keywords")
self.journal = kwargs.get("journal")
self.publication_date = kwargs.get("publication_date")
Expand All @@ -44,6 +47,26 @@ def _initializeFromXML(self: object, xml_element: TypeVar("Element")) -> None:
""" Helper method that parses an XML element into an article object.
"""

def _attemptGrab(xml_finder_text):
# Try to parse and clean the conclusion
try:
element = xml_element.find(xml_finder_text)
if element is not None:
element = (
xml.tostring(element, method="text")
.decode("utf8")
.strip()
.replace("\n", " ")
)

# Set to None if we're unable to parse it
except:
element = None

return element



def _getText(element: TypeVar("Element"), path: str, default: str = "") -> str:
""" Internal helper method that retrieves the text content of an
XML element.
Expand Down Expand Up @@ -78,32 +101,13 @@ def _getText(element: TypeVar("Element"), path: str, default: str = "") -> str:
]
self.journal = _getText(xml_element, ".//Journal/Title", None)

# Try to parse and clean the abstract
try:
abstract_element = xml_element.findall(".//AbstractText")

if abstract_element is None or (
isinstance(abstract_element, list) and len(abstract_element) == 0
):
self.abstract = ""
# Try to parse and clean abstract elements
self.abstract = _attemptGrab(xml_finder_text=".//AbstractText")
self.conclusion = _attemptGrab(xml_finder_text=".//AbstractText[@Label='CONCLUSION']")
self.methods = _attemptGrab(xml_finder_text=".//AbstractText[@Label='METHODS']")
self.results = _attemptGrab(xml_finder_text=".//AbstractText[@Label='RESULTS']")

else:
self.abstract = "\n".join(
[
(
xml.tostring(abstract_element_section, method="text")
.decode("utf8")
.strip()
.replace("\n", " ")
)
for abstract_element_section in abstract_element
]
)

# Set the abstract to None if we're unable to parse it
except Exception as e:
print(e)
self.abstract = None

# Get the publication date
try:
Expand Down