units

package
v1.0.0 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Jan 2, 2024 License: GPL-3.0 Imports: 13 Imported by: 0

Documentation

Overview

Package units provides measurement systems, and unit conversion.

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func ConvertParagraph

func ConvertParagraph(paragraph string, from, to System) string

ConvertParagraph converts the paragraph to the desired System.

func ConvertSentence

func ConvertSentence(input string, from, to System) (string, error)

ConvertSentence converts the sentence to the desired System.

func ReplaceDecimalFractions

func ReplaceDecimalFractions(input string) string

ReplaceDecimalFractions converts the decimals in a string to fractions.

func ReplaceVulgarFractions

func ReplaceVulgarFractions(input string) string

ReplaceVulgarFractions replaces vulgar fractions in a string to decimal ones.

Types

type Measurement

type Measurement struct {
	Quantity float64
	Unit     Unit
}

Measurement represents a physical measurement consisting of a quantity and a unit.

func NewMeasurement

func NewMeasurement(quantity float64, unit string) (Measurement, error)

NewMeasurement creates a Measurement from a quantity of type int or float64 and a unit. The creation fails when the unit is invalid.

func NewMeasurementFromString

func NewMeasurementFromString(s string) (Measurement, error)

NewMeasurementFromString creates a Measurement from a string.

func (Measurement) Convert

func (m Measurement) Convert(to Unit) (Measurement, error)

Convert converts the measurement to the desired unit.

func (Measurement) Scale

func (m Measurement) Scale(multiplier float64) Measurement

Scale scales the measurement by the given multiplier.

func (Measurement) String

func (m Measurement) String() string

String represents the Measurement as a string.

type System

type System string

System is a type alias for a measurement system.

const (
	ImperialSystem System = "imperial"
	InvalidSystem  System = "invalid"
	MetricSystem   System = "metric"
)

These constants enumerate all possible measurement systems.

func DetectMeasurementSystem

func DetectMeasurementSystem(s string) System

DetectMeasurementSystem determines the System used in the text.

func NewSystem

func NewSystem(system string) System

NewSystem converts the system string to a MeasurementSystem.

func (System) String

func (m System) String() string

String represents the MeasurementSystem as a string.

type TokenizedIngredient

type TokenizedIngredient struct {
	Ingredients []string
	Measurement Measurement
}

TokenizedIngredient holds tokenized ingredients and a Measurement.

func NewTokenizedIngredientFromText

func NewTokenizedIngredientFromText(sentence string) TokenizedIngredient

NewTokenizedIngredientFromText creates a TokenizedIngredient from a sentence.

type Unit

type Unit int

Unit type is a string alias representing a unit.

const (
	Invalid Unit = iota
	Celsius
	Centimeter
	Cup
	Decilitre
	Fahrenheit
	Feet
	FlOz
	Gallon
	Gram
	Inch
	Kilogram
	Litre
	Meter
	Milligram
	Millilitre
	Millimeter
	Ounce
	Pint
	Pound
	Quart
	Tablespoon
	Teaspoon
	Yard
)

These constants enumerate all possible units.

func (Unit) String

func (u Unit) String() string

String represents the Unit as a string.

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL