-
Notifications
You must be signed in to change notification settings - Fork 7
/
article.go
177 lines (156 loc) · 5.34 KB
/
article.go
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
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
// TODO.
package jstor
// Copyright 2015 by Leipzig University Library, http://ub.uni-leipzig.de
// The Finc Authors, http://finc.info
// Martin Czygan, <martin.czygan@uni-leipzig.de>
//
// This file is part of some open source application.
//
// Some open source application is free software: you can redistribute
// it and/or modify it under the terms of the GNU General Public
// License as published by the Free Software Foundation, either
// version 3 of the License, or (at your option) any later version.
//
// Some open source application is distributed in the hope that it will
// be useful, but WITHOUT ANY WARRANTY; without even the implied warranty
// of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU General Public License for more details.
//
// You should have received a copy of the GNU General Public License
// along with Foobar. If not, see <http://www.gnu.org/licenses/>.
//
// @license GPL-3.0+ <http://spdx.org/licenses/GPL-3.0+>
//
import (
"encoding/base64"
"encoding/xml"
"fmt"
"regexp"
"strings"
"github.com/kennygrant/sanitize"
"github.com/miku/span"
"github.com/miku/span/container"
"github.com/miku/span/formats/finc"
"github.com/miku/span/formats/jats"
"golang.org/x/text/language"
)
const (
// SourceID for internal bookkeeping.
SourceID = "55"
// SourceName for finc.mega_collection.
SourceName = "JSTOR"
// Format for intermediate schema.
Format = "ElectronicArticle"
)
var (
// ArticleTitleBlockPatterns
ArticleTitleBlockPatterns = []*regexp.Regexp{
regexp.MustCompile(`(?i)(front|back)\s*matter`),
regexp.MustCompile(`(?i)table\s*of\s*content[s]?`),
}
DOIPattern = regexp.MustCompile(`10\.[0-9]+\/\S+`)
)
// Article with extras for this source.
type Article struct {
XMLName xml.Name `xml:"article"`
jats.Article
}
// Identifiers returns the doi and the dependent url and recordID in a struct.
// Records from this source do not need a DOI necessarily.
func (article *Article) Identifiers() (jats.Identifiers, error) {
locator := article.Front.Article.SelfURI.Value
doi := DOIPattern.FindString(locator)
id := fmt.Sprintf("ai-%s-%s", SourceID, base64.RawURLEncoding.EncodeToString([]byte(locator)))
return jats.Identifiers{DOI: doi, URL: locator, ID: id}, nil
}
// Authors returns the authors as slice.
func (article *Article) Authors() []finc.Author {
var authors []finc.Author
group := article.Front.Article.ContribGroup
for _, contrib := range group.Contrib {
authors = append(authors, finc.Author{
LastName: contrib.StringName.Surname.Value,
FirstName: contrib.StringName.GivenNames.Value})
}
return authors
}
// Languages returns a list of language in 3-letter format.
func (article *Article) Languages() []string {
set := container.NewStringSet()
for _, cm := range article.Front.Article.CustomMetaGroup.CustomMeta {
if cm.Name.Value == "lang" {
base, err := language.ParseBase(cm.Value.Value)
if err == nil {
set.Add(base.ISO3())
}
}
}
return set.Values()
}
// ReviewedProduct returns the string of the reviewed thing in a best-effort way.
func (article *Article) ReviewedProduct() string {
if len(article.Front.Article.Products) == 0 {
return ""
}
if article.Front.Article.Products[0].Source.Value != "" {
return strings.TrimSpace(sanitize.HTML(article.Front.Article.Products[0].Source.Value))
}
// refs. #7111
if article.Front.Article.Products[0].StringName.Value != "" {
return strings.TrimSpace(sanitize.HTML(article.Front.Article.Products[0].StringName.Value))
}
return ""
}
// ToIntermediateSchema converts an article into an internal schema. There are a
// couple of content-dependent choices here.
func (article *Article) ToIntermediateSchema() (*finc.IntermediateSchema, error) {
output, err := article.Article.ToIntermediateSchema()
if err != nil {
return output, err
}
ids, err := article.Identifiers()
if err != nil {
return output, err
}
output.DOI = ids.DOI
id := ids.ID
if len(id) > span.KeyLengthLimit {
return output, span.Skip{Reason: fmt.Sprintf("id too long: %s", id)}
}
if len(id) == 0 {
return nil, span.Skip{Reason: fmt.Sprintf("no doi or identifier: %v", article)}
}
output.ID = id
output.RecordID = ids.DOI
output.URL = append(output.URL, ids.URL)
output.Authors = article.Authors()
output.Format = Format
output.Languages = article.Languages()
output.MegaCollections = []string{SourceName}
output.SourceID = SourceID
var normalized []string
for _, issn := range output.ISSN {
if len(issn) == 8 && !strings.Contains(issn, "-") {
normalized = append(normalized, fmt.Sprintf("%s-%s", issn[:4], issn[4:]))
}
}
output.ISSN = normalized
// refs #5686
if output.Date.IsZero() {
return output, span.Skip{Reason: fmt.Sprintf("zero date: %s", output.ID)}
}
// refs #5686
for _, p := range ArticleTitleBlockPatterns {
if p.MatchString(output.ArticleTitle) {
return output, span.Skip{Reason: fmt.Sprintf("title blacklisted: %s", output.ArticleTitle)}
}
}
// refs #5686, approx. article type distribution: https://git.io/vzlCr
switch article.Type {
case "book-review", "book-reviews", "Book Review":
output.ArticleTitle = fmt.Sprintf("Review: %s", article.ReviewedProduct())
case "misc", "other", "front-matter", "back-matter", "announcement", "font-matter", "fm", "fornt-matter":
return output, span.Skip{Reason: fmt.Sprintf("suppressed format: %s", article.Type)}
}
return output, nil
}