Documentation
¶
Overview ¶
Package food reads the food tables the selfhostit apps share, and helps search them:
- Open Food Facts products: the monthly export, read for a few countries, and live barcode lookups (also in Open Products Facts and Open Beauty Facts, for things that aren't food)
- USDA FoodData Central (SR Legacy and Foundation Foods), bundled, in the public domain
- NEVO (RIVM, Netherlands), read from the download the owner uploads
It holds no database code: each app stores the foods its own way, and ranks search results by what matters to it.
Index ¶
- Constants
- Variables
- func CoversAll(text string, words []string) bool
- func FieldScore(text string, words []string) float64
- func HasOtherWord(name string, these, asked []string) bool
- func Normalize(s string) string
- func Plausible(f Food) bool
- func ReadExport(r io.Reader, countries []string, add func(Food) error) error
- func Stemmed(words []string) []string
- func Words(q string) []string
- type Client
- type Food
- type FormatError
- type Table
Constants ¶
const ( OFFAttribution = "Open Food Facts, https://world.openfoodfacts.org (Open Database Licence)" OFFPhotoAttribution = "Product photos: Open Food Facts contributors, CC BY-SA 3.0" )
The Open Food Facts credits. The data is under the Open Database Licence, the product photos under Creative Commons Attribution-ShareAlike.
const ( OpenFoodFacts = "https://world.openfoodfacts.org" OpenProductsFacts = "https://world.openproductsfacts.org" OpenBeautyFacts = "https://world.openbeautyfacts.org" )
The sites a barcode can be looked up in. They share one API: Open Food Facts has food, Open Products Facts other household products and Open Beauty Facts cosmetics.
const NEVOAttribution = "NEVO-online version 2025/9.0, RIVM, Bilthoven"
NEVOAttribution is the citation RIVM asks for wherever NEVO data is used.
const OFFExportURL = "https://static.openfoodfacts.org/data/en.openfoodfacts.org.products.csv.gz"
OFFExportURL is the monthly export of every Open Food Facts product, about a gigabyte compressed.
const USDAAttribution = "U.S. Department of Agriculture, Agricultural Research Service. FoodData Central. https://fdc.nal.usda.gov/ (public domain)"
USDAAttribution is shown with the USDA foods. The data is in the public domain; USDA asks to be cited.
Variables ¶
var BarcodePattern = regexp.MustCompile(`^\d{6,14}$`)
BarcodePattern matches the barcodes products are kept by: 6 to 14 digits.
var BelgiumNetherlands = []string{"en:belgium", "en:netherlands"}
BelgiumNetherlands are the countries the selfhostit apps keep products for.
var ErrUnknownProduct = errors.New("unknown product")
ErrUnknownProduct means the site doesn't have the barcode, or has it without a name.
Functions ¶
func CoversAll ¶
CoversAll reports whether every word of the normalized text is one of the words, or its plural: "rice" for "white rice", "apples" for "apple".
func FieldScore ¶
FieldScore rates how well one normalized name matches the words: each word found counts, more at the start of a word and more again as a whole word; a name that starts with the first word or holds every word counts extra.
func HasOtherWord ¶
HasOtherWord reports whether the normalized name holds one of these words that wasn't asked for.
func Normalize ¶
Normalize lowercases s, drops accents and turns everything but letters and digits into single spaces, so "Pâté, de campagne" becomes "pate de campagne".
func Plausible ¶
Plausible leaves out foods whose values can't be right, which happens in a crowd-sourced database: more energy than pure fat, or more than 100 g of macronutrients in 100 g.
func ReadExport ¶
ReadExport reads the tab-separated export and hands add each product sold in one of the countries ("en:belgium") that has a barcode and a name. Those without nutrition come too, with Nutrition false; Plausible tells the products whose values can't be right.
Types ¶
type Client ¶
type Client struct {
UserAgent string
// HTTP makes the requests; nil means one with a 10 second timeout for
// lookups, and no timeout for the export, which is read as it streams.
HTTP *http.Client
}
Client asks Open Food Facts and its sister sites for products. UserAgent says which app is asking, as they ask apps to: "Troye/1.2 (self-hosted calorie tracker)".
type Food ¶
type Food struct {
SourceID string `json:"id"`
Name string `json:"name"`
NameNL string `json:"name_nl,omitempty"`
NameFR string `json:"name_fr,omitempty"`
Aliases string `json:"aliases,omitempty"`
Group string `json:"group,omitempty"`
Brand string `json:"brand,omitempty"`
Barcode string `json:"barcode,omitempty"`
// Products only: the package size ("1 L", "6 x 33 cl"), Open Food Facts'
// category tags joined by commas, and the URL of a small photo of the
// front, about 200 pixels high.
Quantity string `json:"quantity,omitempty"`
Categories string `json:"categories,omitempty"`
ImageURL string `json:"image_url,omitempty"`
// Nutrition is false for a product that gives no energy value, in which
// case the values below are all zero.
Nutrition bool `json:"-"`
Kcal float64 `json:"kcal"`
Protein float64 `json:"protein"`
Carbs float64 `json:"carbs"`
Fat float64 `json:"fat"`
Fiber float64 `json:"fiber"`
Sugar float64 `json:"sugar"`
Salt float64 `json:"salt"`
ServingG float64 `json:"serving_g,omitempty"`
ServingLabel string `json:"serving_label,omitempty"`
}
Food is one food from a table, with its values per 100 g.
func USDARenamed ¶
USDARenamed returns the foods of the table that the module renames, so an app can update them in a table it stored before they had those names.
type FormatError ¶
type FormatError struct {
// contains filtered or unexported fields
}
FormatError means a file isn't the table it should be.
func (FormatError) Error ¶
func (e FormatError) Error() string
type Table ¶
Table is a food table: its edition and its foods.
func BundledUSDA ¶
BundledUSDA reads the USDA table that ships with the module.
func ReadNEVO ¶
ReadNEVO reads the NEVO table from a CSV (any common delimiter), an Excel file, or a zip with either.
Directories
¶
| Path | Synopsis |
|---|---|
|
cmd
|
|
|
usda
command
Command usda turns the FoodData Central CSV downloads from fdc.nal.usda.gov (SR Legacy and Foundation Foods) into the table that ships with the module, data/usda.json.gz.
|
Command usda turns the FoodData Central CSV downloads from fdc.nal.usda.gov (SR Legacy and Foundation Foods) into the table that ships with the module, data/usda.json.gz. |