recipeingredients

package module
v0.0.2 Latest Latest
Warning

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

Go to latest
Published: Jul 8, 2019 License: MIT Imports: 14 Imported by: 0

README

recipeingredients

Code coverage Build Status Go Doc

This is a Golang library for extracting all the ingredients fpr a culinary recipe from any site on the internet. This library compartmentalizes and improve aspects of recipe extraction that I did previously with schollz/meanrecipe and schollz/extract_recipe.

Note: This is still a WIP.

Install

go get github.com/schollz/recipeingredients

Example

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

r, _ := NewFromURL("https://joyfoodsunshine.com/the-most-amazing-chocolate-chip-cookies/")
r.Parse()
fmt.Println(r.PrintIngredientList())
// 1 cup butter
// 1 cup white sugar
// 1 cup brown sugar
// 2 tsp vanilla
// 2 whole eggs
// 3 cups flour
// 1 tsp baking soda
// 1/2 tsp baking powder
// 1 tsp salt
// 2 cups chocolate chips

Please make an issue if you find a problem.

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 GetBestTopHatPositions

func GetBestTopHatPositions(vectorFloat []float64) (start, end int)

GetBestTopHatPositions finds the start and the end of the longest and highest vector.

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"`
	Frequency float64 `json:"frequency,omitempty`
}

Ingredient is the basic struct for ingredients

type IngredientList

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

IngredientList is a list of ingredients

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"`
}

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"`
	Directions  []string                      `json:"directions"`
	Ingredients []Ingredient                  `json:"ingredients"`
	Ratios      map[string]map[string]float64 `json:"ratios"`
}

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 file

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) Parse

func (r *Recipe) Parse() (rerr error)

Parse is the main parser for a given recipe. It looks for the following - Contains number - Contains mass/volume - Contains ingredient - Number occurs before ingredient - Number occurs before mass/volume - Number of ingredients is 1 - Percent of other words is less than 50% - Part of list (contains - or *)

func (*Recipe) PrintDirections

func (r *Recipe) PrintDirections() string

func (*Recipe) PrintIngredientList

func (r *Recipe) PrintIngredientList() string

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

Jump to

Keyboard shortcuts

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