aboutsummaryrefslogtreecommitdiffstats
path: root/skate/unstructured.go
blob: a12507ad1e53e8561088398036029ca707e38ccb (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
package skate

import (
	"regexp"
	"strings"
)

var (
	PatDOI          = regexp.MustCompile(`10[.][0-9]{1,8}/[^ ]*[\w]`)
	PatDOINoHyphen  = regexp.MustCompile(`10[.][0-9]{1,8}/[^ -]*[\w]`)
	PatArxivPDF     = regexp.MustCompile(`https?://arxiv.org/pdf/([0-9]{4,4}[.][0-9]{1,8})(v[0-9]{1,2})?(.pdf)?`)
	PatArxivAbs     = regexp.MustCompile(`https?://arxiv.org/abs/([0-9]{4,4}[.][0-9]{1,8})(v[0-9]{1,2})?(.pdf)?`)
	DOILinkPrefixes = []string{
		"http://doi.org/",
		"http://dx.doi.org/",
		"https://doi.org/",
		"https://dx.doi.org/",
	}
)

// ParseUnstructured will in-place augment missing DOI, arxiv id and so on.
func ParseUnstructured(ref *Ref) error {
	var (
		uns = ref.Biblio.Unstructured
		v   string
		vs  []string
	)
	// Handle things like: 10.1111/j.1550-7408.1968.tb02138.x-BIB5|cit5,
	// 10.1111/j.1558-5646.1997.tb02431.x-BIB0008|evo02431-cit-0008, ...
	if strings.Contains(strings.ToLower(ref.Key), "-bib") && ref.Biblio.DOI == "" {
		parts := strings.Split(strings.ToLower(ref.Key), "-bib")
		ref.Biblio.DOI = parts[0]
	}
	// DOI
	v = PatDOI.FindString(uns)
	if v != "" && ref.Biblio.DOI == "" {
		ref.Biblio.DOI = v
	}
	// DOI in Key
	v = PatDOINoHyphen.FindString(ref.Key)
	if v != "" && ref.Biblio.DOI == "" {
		ref.Biblio.DOI = v
	}
	// DOI in URL
	for _, prefix := range DOILinkPrefixes {
		if ref.Biblio.DOI != "" && strings.HasPrefix(ref.Biblio.Url, prefix) {
			ref.Biblio.DOI = strings.Replace(ref.Biblio.Url, prefix, "", -1)
		}
	}
	// Another DOI pattern.
	v = PatDOINoHyphen.FindString(ref.Key)
	if v != "" && ref.Biblio.DOI == "" {
		ref.Biblio.DOI = v
	}
	// Arxiv
	vs = PatArxivPDF.FindStringSubmatch(uns)
	if len(vs) != 0 && ref.Biblio.ArxivId == "" {
		ref.Biblio.ArxivId = vs[1]
	} else {
		vs = PatArxivAbs.FindStringSubmatch(uns)
		if len(vs) != 0 && ref.Biblio.ArxivId == "" {
			ref.Biblio.ArxivId = vs[1]
		}
	}
	// XXX: ISBN
	ref.Biblio.Extra.ISBN = ParseIsbn(uns)
	// Some more, not yet handled examples:
	// - 2005 Sep;95(9):1545-51
	// - Pattern Anal. Mach. Intell., 36(2):346-360, 2014. 4, 6, 7"
	// - only unstructured, and it contains a URL - then reference might be to
	//   that site, actually, e.g.
	//   https://fatcat.wiki/release/xqgaanhpf5gotdxxxytgyxw2ty/references ref #20
	//
	//
	// There seems to be a number tiny of data issues; e.g. one off dates. Can
	// we fix them here?
	// Include: https://images.webofknowledge.com/images/help/WOS/J_abrvjt.html, ...
	//
	// Also: We may have differing values (between .pages and some hint in
	// unstructured), e.g. for the page counts, etc.

	return nil
}