org.globalnames.formatters.Canonizer.scala Maven / Gradle / Ivy
Go to download
Show more of this group Show more artifacts with this name
Show all versions of gnparser_2.11 Show documentation
Show all versions of gnparser_2.11 Show documentation
Fast and elegant parser for taxonomic scientific names
package org.globalnames.formatters
import org.globalnames.parser._
import scalaz.{Name => _, _}
import Scalaz._
trait Canonizer { parsedResult: ScientificNameParser.Result =>
def canonized(showRanks: Boolean): Option[String] = {
def canonizedNamesGroup(namesGroup: NamesGroup): String =
if (namesGroup.name.size == 1) {
namesGroup.hybrid.map { _ ⇒ "× " }.orZero +
canonizedName(namesGroup.name.head)
} else {
namesGroup.name.map(canonizedName).mkString(" × ")
}
def canonizedName(nm: Name): String =
Vector(canonizedUninomial(nm.uninomial, showRanks),
nm.species.map { canonizedSpecies },
nm.infraspecies.map { canonizedInfraspeciesGroup })
.flatten.mkString(" ")
def canonizedSpecies(sp: Species): String =
Util.norm(stringOf(sp))
def canonizedInfraspecies(is: Infraspecies): String = {
val rankStrMaybe = showRanks.option {
is.rank.map { r => r.typ.getOrElse(stringOf(r)) + " " }}.join
rankStrMaybe.orZero + Util.norm(stringOf(is))
}
def canonizedInfraspeciesGroup(isg: InfraspeciesGroup): String =
isg.group.map(canonizedInfraspecies).mkString(" ")
parsedResult.scientificName.namesGroup.map(canonizedNamesGroup)
}
def canonizedUninomial(uninomial: Uninomial,
showRanks: Boolean): Option[String] =
(!uninomial.implied).option {
val rankStrMaybe = showRanks.option {
uninomial.parent.map { p => Util.norm(stringOf(p)) + " " } |+|
uninomial.rank.map { r => r.typ.getOrElse(stringOf(r)) }}.join
rankStrMaybe.map { _ + " " }.orZero + Util.norm(stringOf(uninomial))
}
}
© 2015 - 2025 Weber Informatics LLC | Privacy Policy