-
Notifications
You must be signed in to change notification settings - Fork 30
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Implement a Wikidata identifier for Siegfried
Wikidata contains a host of file format information. Wikidata records provide information about format names, identifiers (PUID, LoC FDD), file-extensions, MIMEtypes, and format identification patterns. This means it might be quite handy as an extension to Siegfried's capabilties. This is the first-cut of that work. We are able to harvest information from the Wikidata Query Service. Create an identifier. And consume the information in the identifier to match file-formats using format-identification patterns taken directly from the service. We attempt to return new information not otherwise present in other identifiers yet, for example, signature provenance. Provenance is recorded in Wikidata as well as the date a format-identification pattern was added. We try and replay this to the user to enrich what is available to them. This work is graciously supported by Yale University Library (Euan Cochran, Kat Thornton) and Richard Lehane. It has been fun trying to put this out there for folk.
- Loading branch information
1 parent
8a95ae4
commit dfb579b
Showing
41 changed files
with
112,196 additions
and
73 deletions.
There are no files selected for viewing
106,636 changes: 106,636 additions & 0 deletions
106,636
cmd/roy/data/wikidata/wikidata-test-definitions
Large diffs are not rendered by default.
Oops, something went wrong.
2,195 changes: 2,195 additions & 0 deletions
2,195
cmd/roy/data/wikidata/wikidata-test-definitions-small
Large diffs are not rendered by default.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,62 @@ | ||
// Copyright 2020 Ross Spencer, Richard Lehane. All rights reserved. | ||
// | ||
// Licensed under the Apache License, Version 2.0 (the "License"); | ||
// you may not use this file except in compliance with the License. | ||
// You may obtain a copy of the License at | ||
// | ||
// http://www.apache.org/licenses/LICENSE-2.0 | ||
// | ||
// Unless required by applicable law or agreed to in writing, software | ||
// distributed under the License is distributed on an "AS IS" BASIS, | ||
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or | ||
// implied. See the License for the specific language governing | ||
// permissions and limitations under the License. | ||
|
||
package main | ||
|
||
import ( | ||
"fmt" | ||
"io/ioutil" | ||
"log" | ||
"os" | ||
|
||
"github.com/richardlehane/siegfried/pkg/config" | ||
"github.com/ross-spencer/spargo/pkg/spargo" | ||
) | ||
|
||
// harvestWikidata will connect to the configured Wikidata query service | ||
// and save the results of the configured query to disk. | ||
func harvestWikidata() error { | ||
log.Printf( | ||
"Roy (Wikidata): Harvesting Wikidata definitions: lang '%s'", | ||
config.WikidataLang(), | ||
) | ||
err := os.MkdirAll(config.WikidataHome(), os.ModePerm) | ||
if err != nil { | ||
return fmt.Errorf( | ||
"Roy (Wikidata): Error harvesting Wikidata definitions: '%s'", | ||
err, | ||
) | ||
} | ||
log.Printf( | ||
"Roy (Wikidata): Harvesting definitions from: '%s'", | ||
config.WikidataEndpoint(), | ||
) | ||
sparqlMe := spargo.SPARQLClient{} | ||
sparqlMe.ClientInit(config.WikidataEndpoint(), config.WikidataSPARQL()) | ||
sparqlMe.SetUserAgent(config.UserAgent()) | ||
res := sparqlMe.SPARQLGo() | ||
path := config.WikidataDefinitionsPath() | ||
err = ioutil.WriteFile(path, []byte(res.Human), config.WikidataFileMode()) | ||
if err != nil { | ||
return fmt.Errorf( | ||
"Roy (Wikidata): Error harvesting Wikidata: '%s'", | ||
err, | ||
) | ||
} | ||
log.Printf( | ||
"Roy (Wikidata): Harvesting Wikidata definitions '%s' complete", | ||
path, | ||
) | ||
return nil | ||
} |
Large diffs are not rendered by default.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file not shown.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
� |
Binary file not shown.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,126 @@ | ||
package main | ||
|
||
import ( | ||
"flag" | ||
"fmt" | ||
|
||
"os" | ||
|
||
"path/filepath" | ||
"strings" | ||
"testing" | ||
|
||
"github.com/richardlehane/siegfried" | ||
"github.com/richardlehane/siegfried/pkg/config" | ||
"github.com/richardlehane/siegfried/pkg/wikidata" | ||
) | ||
|
||
// Path components associated with the Roy command folder. | ||
const wikidataTestDefinitions = "wikidata-test-definitions" | ||
const wikidataDefinitionsBaseDir = "definitionsBaseDir" | ||
|
||
var royTestData = filepath.Join("..", "roy", "data") | ||
|
||
// Path components within the Siegfried command folder. | ||
const siegfriedTestData = "testdata" | ||
const wikidataTestData = "wikidata" | ||
const wikidataPRONOMSkeletons = "pro" | ||
const wikidataCustomSkeletons = "wd" | ||
|
||
var ( | ||
wikidataDefinitions = flag.String( | ||
wikidataDefinitionsBaseDir, | ||
royTestData, | ||
"Creates an flag var that is compatible with the config functions...", | ||
) | ||
) | ||
|
||
var wdSiegfried *siegfried.Siegfried | ||
|
||
func setupWikidata(opts ...config.Option) error { | ||
if opts == nil && wdSiegfried != nil { | ||
return fmt.Errorf( | ||
"Wikidata setup options are not properly configured", | ||
) | ||
} | ||
wdSiegfried = siegfried.New() | ||
config.SetHome(*wikidataDefinitions) | ||
config.SetWikidataNamespace() | ||
config.SetWikidataDefinitions(wikidataTestDefinitions) | ||
config.SetWikidataNoPRONOM() | ||
identifier, err := wikidata.New() | ||
if err != nil { | ||
return err | ||
} | ||
wdSiegfried.Add(identifier) | ||
return nil | ||
} | ||
|
||
// identificationTests provides our structure for table driven tests. | ||
type identificationTests struct { | ||
fname string | ||
qid string | ||
extMatch bool | ||
byteMatch bool | ||
error bool | ||
} | ||
|
||
var skeletonSamples = []identificationTests{ | ||
identificationTests{ | ||
filepath.Join(wikidataPRONOMSkeletons, "fmt-11-signature-id-58.png"), | ||
"Q178051", true, true, false}, | ||
identificationTests{ | ||
filepath.Join(wikidataPRONOMSkeletons, "fmt-279-signature-id-295.flac"), | ||
"Q27881556", true, true, false}, | ||
identificationTests{ | ||
filepath.Join(wikidataCustomSkeletons, "Q10287816.gz"), | ||
"Q10287816", true, true, false}, | ||
identificationTests{ | ||
filepath.Join(wikidataCustomSkeletons, "Q28205479.info"), | ||
"Q28205479", true, true, false}, | ||
} | ||
|
||
// Rudimentary consts that can help us determine the method of | ||
// identification. Can also add "container name" here for when we want | ||
// to validate PRONOM alongside Wikidata. | ||
const extensionMatch = "extension match" | ||
const byteMatch = "byte match" | ||
|
||
// TestWikidataBasic will perform some rudimentary tests using some | ||
// simple Skeleton files and the Wikidata identifier without PRONOM. | ||
func TestWikidataBasic(t *testing.T) { | ||
err := setupWikidata() | ||
if err != nil { | ||
t.Error(err) | ||
} | ||
for _, test := range skeletonSamples { | ||
path := filepath.Join(siegfriedTestData, wikidataTestData, test.fname) | ||
file, err := os.Open(path) | ||
if err != nil { | ||
t.Errorf("failed to open %v, got: %v", path, err) | ||
} | ||
defer file.Close() | ||
res, err := wdSiegfried.Identify(file, path, "") | ||
if err != nil && !test.error { | ||
t.Fatal(err) | ||
} | ||
if len(res) > 1 { | ||
t.Errorf("Match length greater than one: '%d'", len(res)) | ||
} | ||
id := res[0].Values()[1] | ||
basis := res[0].Values()[5] | ||
if id != test.qid { | ||
t.Errorf( | ||
"QID match different than anticipated: '%s' expected '%s'", | ||
id, | ||
test.qid, | ||
) | ||
} | ||
if test.extMatch && !strings.Contains(basis, extensionMatch) { | ||
t.Errorf("Extension match not returned by identifier: %s", basis) | ||
} | ||
if test.byteMatch && !strings.Contains(basis, byteMatch) { | ||
t.Errorf("Byte match not returned by identifier: %s", basis) | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.