summaryrefslogtreecommitdiffstats
path: root/python/fatcat_tools/normal.py
blob: 044ab87d94dc98bd0d16ab28cc82c17c570ef5e5 (plain)
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

"""
A bunch of helpers to parse and normalize strings: external identifiers,
free-form input, titles, etc.
"""

import re


def clean_doi(raw):
    """
    Removes any:
    - padding whitespace
    - 'doi:' prefix
    - URL prefix

    Does not try to un-URL-encode

    Returns None if not a valid DOI
    """
    raw = raw.strip()
    if len(raw.split()) != 1:
        return None
    if raw.startswith("doi:"):
        raw = raw[4:]
    if raw.startswith("http://"):
        raw = raw[7:]
    if raw.startswith("https://"):
        raw = raw[8:]
    if raw.startswith("doi.org/"):
        raw = raw[8:]
    if raw.startswith("dx.doi.org/"):
        raw = raw[11:]
    if not raw.startswith("10."):
        return None
    # TODO: actual regex
    return raw

def test_clean_doi():
    assert clean_doi("10.1234/asdf ") == "10.1234/asdf"
    assert clean_doi("http://doi.org/10.1234/asdf ") == "10.1234/asdf"
    assert clean_doi("https://dx.doi.org/10.1234/asdf ") == "10.1234/asdf"
    assert clean_doi("doi:10.1234/asdf ") == "10.1234/asdf"
    assert clean_doi("doi:10.1234/ asdf ") == None

def clean_arxiv_id(raw):
    """
    Removes any:
    - 'arxiv:' prefix

    Works with versioned or un-versioned arxiv identifiers.
    """
    pass

def test_clean_arxiv_id():
    pass

def clean_pmcid(raw):
    raw = raw.strip()
    if len(raw.split()) != 1:
        return None
    if raw.startswith("PMC") and raw[3:] and raw[3:].isdigit():
        return raw
    return None

def clean_sha1(raw):
    raw = raw.strip()
    if len(raw.split()) != 1:
        return None
    pass

def clean_issn(raw):
    raw = raw.strip()
    if len(raw.split()) != 1:
        return None
    if len(raw) == 9 and raw[4] == "-" and raw[0:4].isdigit():
        return raw
    return None

def test_clean_issn():
    assert clean_issn("1234-4567") == "1234-4567"
    assert clean_issn("134-4567") == None
    assert clean_issn("123X-4567") == None

def clean_isbn13(raw):
    raw = raw.strip()
    if len(raw.split()) != 1:
        return None
    return None

def clean_orcid(raw):
    raw = raw.strip()
    if len(raw.split()) != 1:
        return None
    return None