Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Match prep method on search #228

Merged
merged 8 commits into from
Nov 2, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "diet-diary",
"version": "2.26.4",
"version": "2.27.0",
"private": true,
"homepage": ".",
"dependencies": {
Expand Down
36 changes: 23 additions & 13 deletions src/features/suggestions/generate/calculateServing.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,26 +4,36 @@ import { multiply } from '../../../model/servingFunction';
import parseAmount, { DecomposedAmount } from '../parser/DecomposedAmount';
import convert, { isMeasurementConvertible, Unit } from '../convert';

function measurementFor(unit: Unit, { measurement, alternateMeasurement }: DecomposedAmount) {
if (unit === "unknown") {
if (alternateMeasurement && alternateMeasurement.unit === "unknown") {
return alternateMeasurement;
function measurementFor(unit: Unit, { measurement, alternateMeasurement }: DecomposedAmount, prepMethod?: string) {
const isUnitConvertibleTo = _.partial(isMeasurementConvertible, unit);
const allMeasurements = alternateMeasurement ? [measurement, alternateMeasurement] : [measurement];
const canBeConverted = _.filter(allMeasurements, isUnitConvertibleTo);

if (prepMethod) {
const containsPrepMethod = (m: typeof measurement) => {
const words = _.words(_.lowerCase(m.unitText));
return _.includes(words, prepMethod);
}
return measurement;
};
const matchedPrepMethod = _.filter(canBeConverted, containsPrepMethod);
if (matchedPrepMethod) {
return _.defaultTo(_.head(matchedPrepMethod), measurement);
}
}
return _.defaultTo(_.head(canBeConverted), measurement);
}

const isUnitConvertibleTo = _.partial(isMeasurementConvertible, unit);
if (isUnitConvertibleTo(measurement)) {
return measurement;
} else if (alternateMeasurement && isUnitConvertibleTo(alternateMeasurement)) {
return alternateMeasurement;
function prepMethod(unitText: string | undefined) {
const words = _.words(_.lowerCase(unitText));
if (_.size(words) > 1) {
return words[1];
} else {
return undefined;
}
return measurement;
}

function servingFor(unitServing: Serving, servingAmount: DecomposedAmount, amount: string) {
const from = parseAmount(amount).measurement;
const to = measurementFor(from.unit, servingAmount);
const to = measurementFor(from.unit, servingAmount, prepMethod(from.unitText));

try {
const normalizedQuantity = convert(from.quantity, from.unit, to.unit);
Expand Down
25 changes: 25 additions & 0 deletions src/features/suggestions/generate/generateSuggestion.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -311,3 +311,28 @@ test("diameter size calculation", () => {
generateSuggestions(new MockRefObject("cheese pizza 1/8 of 12-inch "), assert);

})

test("use prep method -- word after unit e.g. 1 cup cooked to determine the measurement", () => {
const assert = (suggestions: Suggestion[]) => {
expect(_.size(suggestions)).toBeGreaterThanOrEqual(2);
// the input
expect(suggestions[0]).toEqual(
{
foodName: "cabbage, green or red",
amount: "1 cup cooked",
}
);
expect(suggestions[1]).toMatchObject(
{
foodName: "cabbage, green or red",
amount: "1 cup cooked",
serving: {
"vegetable": 2,
}
}
);

}
generateSuggestions(new MockRefObject("cabbage, green or red 1 cup cooked"), assert);

})