Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
20 changes: 20 additions & 0 deletions tests/test_identifier.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,10 +12,30 @@
[
("10.1063/5.0155600", Identifier.DOI),
("37526163", Identifier.PMID),
("1234567", Identifier.PMID),
("1", Identifier.PMID),
("2304.09409", Identifier.ARXIV),
("2304.09409v2", Identifier.ARXIV),
("hep-th/9901001", Identifier.ARXIV),
("hep-th/9901001v2", Identifier.ARXIV),
("math.GT/0309136", Identifier.ARXIV),
("Deep residual learning for image recognition", Identifier.TITLE),
],
)
def test_get_identifier_type(identifier, expected):
"""Test get_identifier_type()."""
assert get_identifier_type(identifier) == expected


@pytest.mark.parametrize(
"identifier",
[
"10.1063/5.0155600 trailing",
"2304.09409 trailing",
"hep-th/9901001 trailing",
"123456789",
],
)
def test_get_identifier_type_rejects_malformed_identifiers(identifier):
"""Test malformed identifier-like strings are not accepted by prefix."""
assert get_identifier_type(identifier) is None
9 changes: 6 additions & 3 deletions wenxian/identifier.py
Original file line number Diff line number Diff line change
Expand Up @@ -17,16 +17,19 @@ class Identifier(Enum):

REGEX: dict[Identifier, re.Pattern] = {
Identifier.DOI: re.compile(r"10\.\d{4,9}/[-._;()/:A-Z0-9]+", re.IGNORECASE),
Identifier.PMID: re.compile(r"\b\d{8}\b"),
Identifier.ARXIV: re.compile(r"\d{4}\.\d{4,5}(v\d+)?"),
Identifier.PMID: re.compile(r"\d{1,8}"),
Identifier.ARXIV: re.compile(
r"(?:\d{4}\.\d{4,5}|[A-Z][A-Z0-9.-]*/\d{7})(?:v\d+)?",
re.IGNORECASE,
),
}
"""Regex for identifiers."""


def get_identifier_type(identifier: str) -> Identifier | None:
"""Get the type of an identifier."""
for id_type, regex in REGEX.items():
if regex.match(identifier):
if regex.fullmatch(identifier):
return id_type
# Check if it could be a reasonable paper title
# Title should have at least 3 words and be at least 10 characters
Expand Down