Skip to content

Commit c468966

Browse files
committed
compatibility with gnames 0.8.0 (close #89)
1 parent 621feea commit c468966

File tree

21 files changed

+164
-222
lines changed

21 files changed

+164
-222
lines changed

.markdownlint.json

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
{
2+
"default": true,
3+
"MD003": { "style": "atx" },
4+
"no-hard-tabs": false
5+
}

CHANGELOG.md

Lines changed: 16 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,9 @@
11
# Changelog
22

3+
## [v0.8.0] - 2022-02-24 Thu
4+
5+
- Add[#89]: compatibility with gnames v0.8.0
6+
37
## [v0.7.3] - 2022-02-14 Mon
48

59
- Add: make gnverifier compatible with gnames v0.7.1
@@ -139,17 +143,18 @@
139143

140144
This document follows [changelog guidelines]
141145

142-
[v0.7.3]: https://github.com/gnames/gnverifier/compare/v0.7.2...v1.7.3
143-
[v0.7.2]: https://github.com/gnames/gnverifier/compare/v0.7.1...v1.7.2
144-
[v0.7.1]: https://github.com/gnames/gnverifier/compare/v0.7.0...v1.7.1
145-
[v0.7.0]: https://github.com/gnames/gnverifier/compare/v0.6.6...v1.7.0
146-
[v0.6.6]: https://github.com/gnames/gnverifier/compare/v0.6.5...v1.6.6
147-
[v0.6.5]: https://github.com/gnames/gnverifier/compare/v0.6.4...v1.6.5
148-
[v0.6.4]: https://github.com/gnames/gnverifier/compare/v0.6.3...v1.6.4
149-
[v0.6.3]: https://github.com/gnames/gnverifier/compare/v0.6.2...v1.6.3
150-
[v0.6.2]: https://github.com/gnames/gnverifier/compare/v0.6.1...v1.6.2
151-
[v0.6.1]: https://github.com/gnames/gnverifier/compare/v0.6.0...v1.6.1
152-
[v0.6.0]: https://github.com/gnames/gnverifier/compare/v0.5.2...v1.6.0
146+
[v0.8.0]: https://github.com/gnames/gnverifier/compare/v0.7.3...v0.8.0
147+
[v0.7.3]: https://github.com/gnames/gnverifier/compare/v0.7.2...v0.7.3
148+
[v0.7.2]: https://github.com/gnames/gnverifier/compare/v0.7.1...v0.7.2
149+
[v0.7.1]: https://github.com/gnames/gnverifier/compare/v0.7.0...v0.7.1
150+
[v0.7.0]: https://github.com/gnames/gnverifier/compare/v0.6.6...v0.7.0
151+
[v0.6.6]: https://github.com/gnames/gnverifier/compare/v0.6.5...v0.6.6
152+
[v0.6.5]: https://github.com/gnames/gnverifier/compare/v0.6.4...v0.6.5
153+
[v0.6.4]: https://github.com/gnames/gnverifier/compare/v0.6.3...v0.6.4
154+
[v0.6.3]: https://github.com/gnames/gnverifier/compare/v0.6.2...v0.6.3
155+
[v0.6.2]: https://github.com/gnames/gnverifier/compare/v0.6.1...v0.6.2
156+
[v0.6.1]: https://github.com/gnames/gnverifier/compare/v0.6.0...v0.6.1
157+
[v0.6.0]: https://github.com/gnames/gnverifier/compare/v0.5.2...v0.6.0
153158
[v0.5.2]: https://github.com/gnames/gnverifier/compare/v0.5.1...v0.5.2
154159
[v0.5.1]: https://github.com/gnames/gnverifier/compare/v0.5.0...v0.5.1
155160
[v0.5.0]: https://github.com/gnames/gnverifier/compare/v0.4.1...v0.5.0

README.md

Lines changed: 0 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,6 @@ search feature.
3030
* [capitalize](#capitalize)
3131
* [format](#format)
3232
* [jobs](#jobs)
33-
* [only_preferred](#only_preferred)
3433
* [quiet](#quiet)
3534
* [sources](#sources)
3635
* [web-logs](#web-logs)
@@ -299,20 +298,6 @@ gnverifier -j 1 file.txt
299298

300299
This option is ignored by advanced search.
301300

302-
#### only_preferred
303-
304-
Sometimes a users wants to map a list of names to a DataSource. They
305-
are not interested if name matched anywhere else. In such case you can use
306-
the ``only_preferred`` flag.
307-
308-
```bash
309-
gnverifier -o -s '12' file.txt
310-
# or
311-
gnverifier --only_preferred --sources='1,12' file.tsv
312-
```
313-
314-
In case of advanced search use `all:t` together with this flag.
315-
316301
#### quiet
317302

318303
Removes log messages from the output. Note that results of verification go
@@ -398,7 +383,6 @@ possible to use environment variables for configuration.
398383
| Env. Var. | Configuration |
399384
| :---------------------- | :----------------- |
400385
| GNV_FORMAT | Format |
401-
| GNV_PREFERRED_ONLY | PreferredOnly |
402386
| GNV_DATA_SOURCES | DataSources |
403387
| GNV_WITH_ALL_MATCHES | WithAllMatches |
404388
| GNV_WITH_CAPITALIZATION | WithCapitalization |

config/config.go

Lines changed: 0 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -50,10 +50,6 @@ type Config struct {
5050
// Example: `api\/v(0|1)`
5151
NsqdRegexFilter *regexp.Regexp
5252

53-
// PreferredOnly hides BestResult if the user wants to see only
54-
// preferred results.
55-
PreferredOnly bool
56-
5753
// VerifierURL URL for gnames verification service. It only needs to
5854
// be changed if user sets local version of gnames.
5955
VerifierURL string
@@ -126,14 +122,6 @@ func OptNsqdTCPAddress(s string) Option {
126122
}
127123
}
128124

129-
// OptPreferredOnly sets PreferredOnly field. If it is true output only
130-
// contains results from preferred data-sources.
131-
func OptPreferredOnly(b bool) Option {
132-
return func(cnf *Config) {
133-
cnf.PreferredOnly = b
134-
}
135-
}
136-
137125
// OptVerifierURL sets URL of the verification resource.
138126
func OptVerifierURL(s string) Option {
139127
return func(cnf *Config) {

config/config_test.go

Lines changed: 8 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -16,23 +16,21 @@ func TestConfigDefault(t *testing.T) {
1616
Format: gnfmt.CSV,
1717
VerifierURL: "https://verifier.globalnames.org/api/v0/",
1818
}
19-
assert.Equal(t, cnf.Format, deflt.Format)
20-
assert.Equal(t, cnf.VerifierURL, deflt.VerifierURL)
19+
assert.Equal(t, deflt.Format, cnf.Format)
20+
assert.Equal(t, deflt.VerifierURL, cnf.VerifierURL)
2121
}
2222

2323
func TestConfigOpts(t *testing.T) {
2424
opts := opts()
2525
cnf := config.New(opts...)
2626
updt := config.Config{
27-
Format: gnfmt.PrettyJSON,
28-
PreferredOnly: true,
29-
DataSources: []int{1, 2, 3},
30-
VerifierURL: url,
27+
Format: gnfmt.PrettyJSON,
28+
DataSources: []int{1, 2, 3},
29+
VerifierURL: url,
3130
}
32-
assert.Equal(t, cnf.Format, updt.Format)
33-
assert.Equal(t, cnf.PreferredOnly, updt.PreferredOnly)
34-
assert.Equal(t, cnf.DataSources, updt.DataSources)
35-
assert.Equal(t, cnf.VerifierURL, updt.VerifierURL)
31+
assert.Equal(t, updt.Format, cnf.Format)
32+
assert.Equal(t, updt.DataSources, cnf.DataSources)
33+
assert.Equal(t, updt.VerifierURL, cnf.VerifierURL)
3634
}
3735

3836
type formatTest struct {
@@ -43,7 +41,6 @@ type formatTest struct {
4341
func opts() []config.Option {
4442
return []config.Option{
4543
config.OptFormat(gnfmt.PrettyJSON),
46-
config.OptPreferredOnly(true),
4744
config.OptDataSources([]int{1, 2, 3}),
4845
config.OptVerifierURL(url),
4946
}

ent/output/output.go

Lines changed: 22 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
package output
22

33
import (
4+
"fmt"
45
"strconv"
56
"strings"
67

@@ -12,6 +13,7 @@ type csvField int
1213

1314
const (
1415
kind csvField = iota
16+
sortScore
1517
matchType
1618
editDistance
1719
input
@@ -26,27 +28,27 @@ const (
2628
error
2729
)
2830

29-
const prefMatch = "PreferredMatch"
31+
const sortedMatch = "SortedMatch"
3032

3133
// NameOutput takes result of verification for one string and converts it into
3234
// required format (CSV or JSON).
33-
func NameOutput(ver vlib.Name, f gnfmt.Format, prefOnly bool) string {
35+
func NameOutput(ver vlib.Name, f gnfmt.Format) string {
3436
switch f {
3537
case gnfmt.CSV:
36-
return csvOutput(ver, prefOnly, ',')
38+
return csvOutput(ver, ',')
3739
case gnfmt.TSV:
38-
return csvOutput(ver, prefOnly, '\t')
40+
return csvOutput(ver, '\t')
3941
case gnfmt.CompactJSON:
40-
return jsonOutput(ver, prefOnly, false)
42+
return jsonOutput(ver, false)
4143
case gnfmt.PrettyJSON:
42-
return jsonOutput(ver, prefOnly, true)
44+
return jsonOutput(ver, true)
4345
}
4446
return "N/A"
4547
}
4648

4749
// CSVHeader returns the header string for CSV output format.
4850
func CSVHeader(f gnfmt.Format) string {
49-
header := []string{"Kind", "MatchType", "EditDistance", "ScientificName",
51+
header := []string{"Kind", "SortScore", "MatchType", "EditDistance", "ScientificName",
5052
"MatchedName", "MatchedCanonical", "TaxonId", "CurrentName", "Synonym",
5153
"DataSourceId", "DataSourceTitle", "ClassificationPath", "Error"}
5254
switch f {
@@ -59,14 +61,13 @@ func CSVHeader(f gnfmt.Format) string {
5961
}
6062
}
6163

62-
func csvOutput(ver vlib.Name, prefOnly bool, sep rune) string {
64+
func csvOutput(ver vlib.Name, sep rune) string {
6365
var res []string
64-
if !prefOnly {
66+
if ver.BestResult != nil {
6567
best := csvRow(ver, -1, sep)
6668
res = append(res, best)
67-
}
68-
if prefOnly && len(ver.Results) == 0 {
69-
res = append(res, csvNoPrefRow(ver, sep))
69+
} else if len(ver.Results) == 0 {
70+
res = append(res, csvEmptyRow(ver, sep))
7071
}
7172
for i := range ver.Results {
7273
pref := csvRow(ver, i, sep)
@@ -76,9 +77,9 @@ func csvOutput(ver vlib.Name, prefOnly bool, sep rune) string {
7677
return strings.Join(res, "\n")
7778
}
7879

79-
func csvNoPrefRow(ver vlib.Name, sep rune) string {
80+
func csvEmptyRow(ver vlib.Name, sep rune) string {
8081
s := []string{
81-
prefMatch, vlib.NoMatch.String(), "", ver.Name,
82+
sortedMatch, "0.0", vlib.NoMatch.String(), "", ver.Name,
8283
"", "", "", "", "", "", "", "", ver.Error,
8384
}
8485
return gnfmt.ToCSV(s, sep)
@@ -89,17 +90,21 @@ func csvRow(ver vlib.Name, prefIndex int, sep rune) string {
8990
res := ver.BestResult
9091

9192
if prefIndex > -1 {
92-
kind = prefMatch
93+
if prefIndex > 0 {
94+
kind = sortedMatch
95+
}
9396
res = ver.Results[prefIndex]
9497
}
9598

9699
s := []string{
97-
kind, ver.MatchType.String(), "", ver.Name,
100+
kind, "0.0", vlib.NoMatch.String(), "", ver.Name,
98101
"", "", "", "", "", "", "", "", ver.Error,
99102
}
100103

101104
if res != nil {
102105
s[editDistance] = strconv.Itoa(res.EditDistance)
106+
s[sortScore] = fmt.Sprintf("%0.5f", res.SortScore)
107+
s[matchType] = res.MatchType.String()
103108
s[matchedName] = res.MatchedName
104109
s[matchedCanonical] = res.MatchedCanonicalFull
105110
s[taxonID] = res.RecordID
@@ -113,11 +118,8 @@ func csvRow(ver vlib.Name, prefIndex int, sep rune) string {
113118
return gnfmt.ToCSV(s, sep)
114119
}
115120

116-
func jsonOutput(ver vlib.Name, prefOnly bool, pretty bool) string {
121+
func jsonOutput(ver vlib.Name, pretty bool) string {
117122
enc := gnfmt.GNjson{Pretty: pretty}
118-
if prefOnly {
119-
ver.BestResult = nil
120-
}
121123
res, _ := enc.Encode(ver)
122124
return string(res)
123125
}

ent/output/output_test.go

Lines changed: 17 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -19,53 +19,46 @@ func TestOutput(t *testing.T) {
1919
msg string
2020
input vlib.Name
2121
format gnfmt.Format
22-
prefOnly bool
2322
test func(*testing.T, string)
2423
linesNum int
2524
}{
2625
{
27-
msg: "csv_prefOnly_false",
28-
input: verifs[0],
29-
format: gnfmt.CSV,
30-
prefOnly: false,
26+
msg: "csv",
27+
input: verifs[0],
28+
format: gnfmt.CSV,
3129
test: func(t *testing.T, res string) {
3230
assert.NotContains(t, res, "inputID", "csv,false 1")
33-
assert.Contains(t, res, "BestMatch", "csv, false 2")
34-
assert.Contains(t, res, "PreferredMatch", "csv, false 3")
31+
assert.Contains(t, res, "SortedMatch", "csv, false 3")
3532
assert.True(t, strings.HasPrefix(res, "BestMatch"), "csv, false 4")
3633
},
3734
linesNum: 3,
3835
},
3936
{
40-
msg: "not_name_csv_prefOnly_true",
41-
input: verifs[2],
42-
format: gnfmt.CSV,
43-
prefOnly: true,
37+
msg: "not_name_csv",
38+
input: verifs[2],
39+
format: gnfmt.CSV,
4440
test: func(t *testing.T, res string) {
4541
assert.NotContains(t, res, "inputID", "noname, csv, true 1")
46-
assert.NotContains(t, res, "BestMatch", "noname, csv, true 2")
47-
assert.Contains(t, res, "PreferredMatch", "noname, csv, true 3")
42+
assert.Contains(t, res, "SortedMatch", "noname, csv, true 3")
4843
assert.Contains(t, res, "NoMatch", "noname, csv, true 4")
4944
},
5045
linesNum: 1,
5146
},
5247
{
53-
msg: "pretty",
54-
input: verifs[0],
55-
format: gnfmt.PrettyJSON,
56-
prefOnly: false,
48+
msg: "pretty",
49+
input: verifs[0],
50+
format: gnfmt.PrettyJSON,
5751
test: func(t *testing.T, res string) {
5852
assert.Contains(t, res, "id", "pretty 1")
5953
assert.Contains(t, res, "bestResult", "pretty 2")
6054
assert.Contains(t, res, "results", "pretty 3")
6155
},
62-
linesNum: 104,
56+
linesNum: 107,
6357
},
6458
{
65-
msg: "compact",
66-
input: verifs[0],
67-
format: gnfmt.CompactJSON,
68-
prefOnly: false,
59+
msg: "compact",
60+
input: verifs[0],
61+
format: gnfmt.CompactJSON,
6962
test: func(t *testing.T, res string) {
7063
assert.Contains(t, res, "id", "compact 1")
7164
assert.Contains(t, res, "bestResult", "compact 2")
@@ -77,9 +70,9 @@ func TestOutput(t *testing.T) {
7770

7871
for i := range tests {
7972
t.Run(tests[i].msg, func(t *testing.T) {
80-
res := output.NameOutput(tests[i].input, tests[i].format, tests[i].prefOnly)
73+
res := output.NameOutput(tests[i].input, tests[i].format)
8174
lines := strings.Split(res, "\n")
82-
assert.Equal(t, len(lines), tests[i].linesNum)
75+
assert.Equal(t, tests[i].linesNum, len(lines))
8376
tests[i].test(t, res)
8477
})
8578
}

gnverifier/cmd/gnverifier.yaml

Lines changed: 0 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -2,11 +2,6 @@
22
#
33
# Format: csv
44

5-
# PreferredOnly if true, do not show BestResult, only Preferred Results.
6-
# Its valid values are 'true' and 'false'.
7-
#
8-
# PreferredOnly: false
9-
105
# DataSources is a list of data-source IDs that should always return
116
# matched records if they are found.
127
# You can find list of all data-sources at

0 commit comments

Comments
 (0)