Skip to content

Commit

Permalink
add nomenclatural code flag (close #265)
Browse files Browse the repository at this point in the history
  • Loading branch information
dimus committed Nov 7, 2024
1 parent 2e2e728 commit 9ba1c58
Show file tree
Hide file tree
Showing 3 changed files with 83 additions and 11 deletions.
16 changes: 9 additions & 7 deletions config.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,10 @@ type Config struct {
// BatchSize sets the maximum number of elements in names-strings slice.
BatchSize int

// Code defines nomenclatural code that might modify parsing process in
// some cases.
nomcode.Code

// Debug sets a "debug" state for parsing. The debug state forces output
// format to showing parsed ast tree.
Debug bool
Expand All @@ -36,17 +40,15 @@ type Config struct {
// a stream of name-strings.
JobsNum int

nomcode.NomCode

// Port to run wer-service.
Port int

// WithCapitalization flag, when true, the first letter of a name-string
// is capitalized, if appropriate.
WithCapitalization bool

// WithCultivars flag, when true, cultivar names will be parsed and
// modify cardinality, normalized and canonical output.
WithCultivars bool

// WithDetails can be set to true when a simplified output is not sufficient
// for obtaining a required information.
WithDetails bool
Expand Down Expand Up @@ -145,10 +147,10 @@ func OptWithCapitaliation(b bool) Option {
}
}

// OptWithCultivars sets the EnableCultivars field.
func OptWithCultivars(b bool) Option {
// OptCode sets nomenclatural code to modify parsing process.
func OptWithCultivars(c nomcode.Code) Option {
return func(cfg *Config) {
cfg.WithCultivars = b
cfg.Code = c
}
}

Expand Down
52 changes: 52 additions & 0 deletions ent/nomcode/nomcode.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
package nomcode

import (
"log/slog"
"strings"
)

// Code represents a nomenclatural code.
type Code int

// Constants for different nomenclatural codes.
const (
Unknown Code = iota // Unknown code
Zoological // Zoological code
Botanical // Botanical code
Cultivar // Cultivar code
)

// New creates a new Code from a string representation.
// It accepts short codes ('b', 'z', 'c') and full names
// ('botanical', 'zoological', 'cultivar') as well as
// official abbreviations ('icn', 'iczn', 'icncp').
// The input string is case-insensitive.
func New(s string) Code {
sOrig := s
s = strings.ToLower(s)
switch s {
case "b", "bot", "botanical", "icn":
return Botanical
case "z", "zoo", "zoological", "iczn":
return Zoological
case "c", "cult", "cultivar", "icncp":
return Cultivar
default:
slog.Warn("Cannot determine nomenclatural code", "input", sOrig)
return Unknown
}
}

// String returns the official abbreviation of the nomenclatural code.
func (c Code) String() string {
switch c {
case Zoological:
return "ICZN"
case Botanical:
return "ICN"
case Cultivar:
return "ICNCP"
default:
return ""
}
}
26 changes: 22 additions & 4 deletions gnparser/cmd/root.go
Original file line number Diff line number Diff line change
Expand Up @@ -80,6 +80,7 @@ gnparser -j 5 -p 8080
withPreserveDiaeresesFlag(cmd)
batchSizeFlag(cmd)
spGrCutFlag(cmd)

port := portFlag(cmd)
cfg := gnparser.NewConfig(opts...)
batchSize = cfg.BatchSize
Expand Down Expand Up @@ -139,7 +140,18 @@ func init() {
"maximum number of names in a batch send for processing.")

rootCmd.Flags().BoolP("cultivar", "C", false,
"include cultivar epithets and graft-chimeras in normalized and canonical outputs")
"parse according to cultivar code ICNCP (DEPRECATED, use nomenclatural-code instead)")

codeHelp := `Modifies the parser's behavior in ambiguous cases, sometimes
introducing additional parsing rules.
Accepted values are:
- 'bot', 'icn', 'botanical' for botanical code
- 'cult', 'icncp', 'cultivar' for cultivar code
- 'zoo', 'iczn', 'zoological' for zoological code
If not set, the parser will attempt to determine the appropriate code/s.`
rootCmd.Flags().StringP("nomenclatural-code", "n", "", codeHelp)

rootCmd.Flags().BoolP("capitalize", "c", false,
"capitalize the first letter of input name-strings")
Expand All @@ -149,9 +161,15 @@ func init() {

rootCmd.Flags().BoolP("details", "d", false, "provides more details")

formatHelp := "sets output format. Can be one of:\n" +
"'csv', 'tsv', 'compact', 'pretty'\n" +
"default is 'csv'"
formatHelp := `Sets the output format.
Accepted values are:
- 'csv': Comma-separated values
- 'tsv': Tab-separated values
- 'compact': Compact JSON format
- 'pretty': Human-readable JSON format
If not set, the output format defaults to 'csv'.`
rootCmd.Flags().StringP("format", "f", "", formatHelp)

rootCmd.Flags().BoolP("ignore_tags", "i", false,
Expand Down

0 comments on commit 9ba1c58

Please sign in to comment.