ingredients

package module
v0.0.1 Latest Latest
Warning

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

Go to latest
Published: Sep 14, 2023 License: MIT Imports: 17 Imported by: 0

README

ingredients

Code coverage Build Status Go Doc

This is a Golang library for ingredient tagging and extraction for any recipe on the internet. This library compartmentalizes and improves aspects of recipe extraction that I did previously with schollz/meanrecipe and schollz/extract_recipe.

Try it online: https://schollz.com/blog/ingredients/#try

Usage

Online API

You can use it online, just do GET https://ingredients.schollz.now.sh/?url=X where X is a URL to a recipe website.

$ curl https://ingredients.schollz.now.sh/?url=https://cooking.nytimes.com/recipes/12320-apple-pie
Command line

You can use it from the command line! If you download a release, you can also use it from the command line:

$ ingredients https://www.tasteofhome.com/recipes/banana-chocolate-chip-cookies/
Go library

You can use as a library.

$ go get github.com/schollz/ingredients

Pick a random website, like JoyFoodSunshine, and you can extract the ingredients:

r, _ := ingredients.NewFromURL("https://joyfoodsunshine.com/the-most-amazing-chocolate-chip-cookies/")
fmt.Println(r.IngredientList())
// Output:
// 1 cup butter (salted)
// 1 cup sugar (white)
// 1 cup brown sugar (light)
// 2 tsp vanilla (pure)
// 2 whole eggs
// 3 cups flour (all purpose)
// 1 tsp baking soda
// 1/2 tsp baking powder
// 1 tsp salt (sea)
// 2 cups chocolate chips

Please make an issue if you find a problem.

How does it work?

See my blog post about it: schollz.com/blog/ingredients.

Develop

If you modify the corpus/ information then you will need to run

$ go generate

before using the library again.

Contributing

Pull requests are welcome. Feel free to...

  • Revise documentation
  • Add new features
  • Fix bugs
  • Suggest improvements

License

MIT

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func AmountToString

func AmountToString(amount float64) string

func ConvertStringToNumber

func ConvertStringToNumber(s string) float64

ConvertStringToNumber

func SanitizeLine

func SanitizeLine(s string) string

SanitizeLine removes parentheses, trims the line, converts to lower case, replaces fractions with unicode and then does special conversion for ingredients (like eggs).

Types

type Ingredient

type Ingredient struct {
	Name    string  `json:"name,omitempty"`
	Comment string  `json:"comment,omitempty"`
	Measure Measure `json:"measure,omitempty"`
	Line    string  `json:"line,omitempty"`
}

Ingredient is the basic struct for ingredients

func IngredientsFromURL

func IngredientsFromURL(url string) (ingredients []Ingredient, err error)

type IngredientList

type IngredientList struct {
	Ingredients []Ingredient `json:"ingredients"`
}

IngredientList is a list of ingredients

func ParseTextIngredients

func ParseTextIngredients(text string) (ingredientList IngredientList, err error)

ParseTextIngredients parses a list of ingredients and returns an ingredient list back

func (IngredientList) String

func (il IngredientList) String() string

type LineInfo

type LineInfo struct {
	LineOriginal        string
	Line                string         `json:",omitempty"`
	IngredientsInString []WordPosition `json:",omitempty"`
	AmountInString      []WordPosition `json:",omitempty"`
	MeasureInString     []WordPosition `json:",omitempty"`
	Ingredient          Ingredient     `json:",omitempty"`
}

LineInfo has all the information for the parsing of a given line

type Measure

type Measure struct {
	Amount float64 `json:"amount"`
	Name   string  `json:"name"`
	Cups   float64 `json:"cups"`
	Weight float64 `json:"weight,omitempty"`
}

Measure includes the amount, name and the cups for conversions

type Recipe

type Recipe struct {
	FileName    string       `json:"filename"`
	FileContent string       `json:"file_content"`
	Lines       []LineInfo   `json:"lines"`
	Ingredients []Ingredient `json:"ingredients"`
}

Recipe contains the info for the file and the lines

func Load

func Load(fname string) (r *Recipe, err error)

Load will load a recipe file

func NewFromFile

func NewFromFile(fname string) (r *Recipe, err error)

NewFromFile generates a new parser from a HTML file

func NewFromHTML

func NewFromHTML(name, htmlstring string) (r *Recipe, err error)

NewFromHTML generates a new parser from a HTML text

func NewFromString

func NewFromString(htmlString string) (r *Recipe, err error)

NewFromString generates a new parser from a HTML string

func NewFromURL

func NewFromURL(url string) (r *Recipe, err error)

NewFromURL generates a new parser from a url

func (*Recipe) ConvertIngredients

func (r *Recipe) ConvertIngredients() (err error)

func (*Recipe) IngredientList

func (r *Recipe) IngredientList() (ingredientList IngredientList)

IngredientList will return a string containing the ingredient list

func (*Recipe) Save

func (r *Recipe) Save(fname string) (err error)

Save saves the recipe to a file

type WordPosition

type WordPosition struct {
	Word     string
	Position int
}

WordPosition shows a word and its position Note: the position is memory-dependent as it will be the position after the last deleted word

func GetIngredientsInString

func GetIngredientsInString(s string) (wordPositions []WordPosition)

GetIngredientsInString returns the word positions of the ingredients

func GetMeasuresInString

func GetMeasuresInString(s string) (wordPositions []WordPosition)

GetMeasuresInString returns the word positions of the measures in a ingredient string

func GetNumbersInString

func GetNumbersInString(s string) (wordPositions []WordPosition)

GetNumbersInString returns the word positions of the numbers in the ingredient string

Directories

Path Synopsis
cmd

Jump to

Keyboard shortcuts

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