diff options
-rw-r--r-- | skate/reduce.go | 4 | ||||
-rw-r--r-- | skate/schema.go | 31 | ||||
-rw-r--r-- | skate/schema_test.go | 51 |
3 files changed, 84 insertions, 2 deletions
diff --git a/skate/reduce.go b/skate/reduce.go index 2a8ac1c..8dd90f7 100644 --- a/skate/reduce.go +++ b/skate/reduce.go @@ -293,8 +293,7 @@ func ZippyVerifyRefsOpenLibrary(olr, refs io.Reader, w io.Writer) error { keyer = makeKeyFunc("\t", 1) cleanIdentifier = func(s string) string { // Turn ids like /books/OL31189321M into OL31189321M - s = strings.TrimSpace(s) - if len(s) == 0 { + if s = strings.TrimSpace(s); len(s) == 0 { return "" } var ( @@ -344,6 +343,7 @@ func ZippyVerifyRefsOpenLibrary(olr, refs io.Reader, w io.Writer) error { bref.MatchProvenance = ref.Extra.Skate.Ref.Source bref.MatchStatus = result.Status.Short() bref.MatchReason = result.Reason.Short() + bref.TargetUnstructured = "xxx" if err := enc.Encode(bref); err != nil { return err } diff --git a/skate/schema.go b/skate/schema.go index d35272e..a9b6a96 100644 --- a/skate/schema.go +++ b/skate/schema.go @@ -122,6 +122,37 @@ func RefToRelease(ref *Ref) (*Release, error) { return &release, nil } +// ReleaseToUnstructured tries to render a sensible string, e.g. for frontend +// display of unmatched and other relations. Some examples: +// https://guides.lib.uw.edu/c.php?g=341448&p=4076094 No specific style, just +// try to be readable. +func ReleaseToUnstructured(r *Release) string { + var ( + buf bytes.Buffer + names = make([]string, len(r.Contribs)) + ) + for i := 0; i < len(r.Contribs); i++ { + names[i] = r.Contribs[i].RawName + } + fmt.Fprintf(&buf, "%s", strings.Join(names, ", ")) + if r.Title != "" { + if buf.Len() > 0 { + fmt.Fprintf(&buf, ". ") + } + fmt.Fprintf(&buf, `%s`, r.Title) + } + if len(r.Subtitle()) > 0 { + fmt.Fprintf(&buf, ": %s", strings.Join(r.Subtitle(), " ")) + } + if r.ContainerName != "" { + if buf.Len() > 0 { + fmt.Fprintf(&buf, ". ") + } + fmt.Fprintf(&buf, `%s`, r.ContainerName) + } + return buf.String() +} + // ParseIsbn tries to find and validate ISBN from unstructured data. Returns a // list of unique, unsorted and validated ISBN13, e.g. 9780123838520. func ParseIsbn(s string) []string { diff --git a/skate/schema_test.go b/skate/schema_test.go index 1301184..a8f1ecf 100644 --- a/skate/schema_test.go +++ b/skate/schema_test.go @@ -256,6 +256,57 @@ func TestLinkHash(t *testing.T) { } } +func TestReleaseToUnstructured(t *testing.T) { + var cases = []struct { + r *Release + s string + }{ + {r: &Release{}, s: ""}, + {r: &Release{ + Title: "ABC", + }, s: "ABC"}, + {r: &Release{ + Title: "ABC", + ContainerName: "Journal of Letters", + }, s: "ABC. Journal of Letters"}, + {r: &Release{ + Title: "ABC", + ContainerName: "Journal of Letters", + Contribs: []struct { + Index int `json:"index,omitempty"` + RawName string `json:"raw_name,omitempty"` + Role string `json:"role,omitempty"` + }{ + { + RawName: "Liam Ling", + }, + }, + }, s: "Liam Ling. ABC. Journal of Letters"}, + {r: &Release{ + Title: "ABC", + ContainerName: "Journal of Letters", + Contribs: []struct { + Index int `json:"index,omitempty"` + RawName string `json:"raw_name,omitempty"` + Role string `json:"role,omitempty"` + }{ + { + RawName: "Liam Ling", + }, + { + RawName: "Lin Lee", + }, + }, + }, s: "Liam Ling, Lin Lee. ABC. Journal of Letters"}, + } + for _, c := range cases { + got := ReleaseToUnstructured(c.r) + if got != c.s { + t.Fatalf("got %v, want %v", got, c.s) + } + } +} + func BenchmarkParseIsbn(b *testing.B) { for n := 0; n < b.N; n++ { ParseIsbn("House Pvt. Limited., (2006), ISBN 9788183561426. Date accessed: August 2015.") |