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

More lenient food description parsing of amount #252

Merged
merged 9 commits into from
Dec 24, 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
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ quantity
;

unit
: WORD+ | WORD* LEFT_PAREN (WHOLE_NUMBER | FRACTION | WORD | SLASH | PERIOD)+ RIGHT_PAREN
: WORD+ | WORD* LEFT_PAREN (WHOLE_NUMBER | FRACTION | WORD | SLASH | PERIOD)* RIGHT_PAREN?
;

WORD
Expand Down
56 changes: 56 additions & 0 deletions src/features/suggestions/generate/autoSuggestion.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -259,3 +259,59 @@ test("unknown unit from input, choose the measurement whose unit is also unknown
}
});
})

test("unit with parenthesis should generate serving suggestion", () => {
const autoCompletion = {
foodName: "Doughnut",
amount: "1 (3 1/4 inch dia.)"
}
const suggestions = [
{
foodName: "Doughnut, cake (plain)",
amount: "1 (3 1/4 inch dia.)",
serving: {
carbohydrate: 0.5,
fat: 3,
sweet: 0.5,
}
}
]
const result = generateAutoSuggestion(autoCompletion, suggestions);
expect(result).toEqual({
foodName: "Doughnut",
amount: "1 (3 1/4 inch dia.)",
serving: {
carbohydrate: 0.5,
fat: 3,
sweet: 0.5,
}
});
})

test("unit with open parenthesis should not generate serving suggestion", () => {
const autoCompletion = {
foodName: "Doughnut",
amount: "1 ("
}
const suggestions = [
{
foodName: "Doughnut, cake (plain)",
amount: "1 (3 1/4 inch dia.)",
serving: {
carbohydrate: 0.5,
fat: 3,
sweet: 0.5,
}
}
]
const result = generateAutoSuggestion(autoCompletion, suggestions);
expect(result).toEqual({
foodName: "Doughnut",
amount: "1 (3 1/4 inch dia.)",
serving: {
carbohydrate: 0.5,
fat: 3,
sweet: 0.5,
}
});
})
3 changes: 2 additions & 1 deletion src/features/suggestions/generate/autoSuggestion.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ import _ from 'lodash';
import { Suggestion } from "../Suggestion";
import baseOn from './calculateServing';
import { PredefinedSuggestion } from "../search/PredefinedSuggestion";
import { isAmountParsable } from './isAmountParsable';

export function generateAutoSuggestion(autoCompletion: Suggestion, suggestions: PredefinedSuggestion[]) {
if (_.size(suggestions) === 0) return null;
Expand All @@ -16,7 +17,7 @@ function createAutoSuggestion(nameSuggestion: Suggestion, suggestion: Predefined
...suggestion,
foodName
};
if (amount) {
if (amount && isAmountParsable(amount)) {
const serving = baseOn(suggestion).servingFor(amount);
return {
...autoSuggestion,
Expand Down
15 changes: 9 additions & 6 deletions src/features/suggestions/generate/generateSuggestion.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,24 +5,27 @@ import findAutoCompletions from '../search/autoCompletion';
import { findSuggestions } from '../search/foodNameSearch';
import { Suggestion } from '../Suggestion';
import { generateAutoSuggestion } from './autoSuggestion';
import { isAmountParsable } from './isAmountParsable';

function unitOfMeasurement(amount?: string) {
if (_.isUndefined(amount)) return undefined;

const { measurement } = parseAmount(amount);
return measurement.unit;
if (amount && isAmountParsable(amount)) {
const { measurement } = parseAmount(amount);
return measurement.unit;
}
return undefined;
}

export function generateSuggestions(
foodDescriptionRef: React.MutableRefObject<string>,
foodDescriptionRef: React.RefObject<string>,
callback: (suggestions: Suggestion[]) => void
) {
const foodDescription = decompose(foodDescriptionRef.current + "");
const autoCompletions = findAutoCompletions(foodDescription);

const firstAutoCompletion = autoCompletions[0];
const servingSuggestions = findSuggestions(foodDescription.foodName, { convertibleFrom: unitOfMeasurement(firstAutoCompletion.amount) });
const allSuggestions = _.concat(autoCompletions, generateAutoSuggestion(firstAutoCompletion, servingSuggestions), servingSuggestions);
const autoSuggestions = generateAutoSuggestion(firstAutoCompletion, servingSuggestions);
const allSuggestions = _.concat(autoCompletions, autoSuggestions, servingSuggestions);
const results = _.uniqWith(_.compact(allSuggestions), _.isEqual)
.slice(0, 5);
return callback(results);
Expand Down
14 changes: 14 additions & 0 deletions src/features/suggestions/generate/isAmountParsable.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
import _ from 'lodash';

/**
* Amount string is parsable if it has matching parentheses. Default to true if there is no parentheses.
*
* @param amount Amount string to check if it is parsable
* @returns true if amount is parsable, false otherwise
*/
export function isAmountParsable(amount: string) {
if (_.endsWith(amount, ")")) {
return amount.includes("(");
}
return !amount.includes("(");
}
12 changes: 12 additions & 0 deletions src/features/suggestions/parser/foodDescription.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -247,4 +247,16 @@ test("parenthesis in unit includes /", () => {
}
}
);
})

test("parenthesis in unit with no content", () => {
expect(parseFoodDescription("Muffin 1 large (")).toMatchObject(
{
foodName: "Muffin",
amount: "1 large (",
measurement: {
unitText: "large (",
}
}
);
})
12 changes: 8 additions & 4 deletions src/features/suggestions/search/autoCompletion.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,16 +4,20 @@ import { createSuggestion, Suggestion } from '../Suggestion';
import autoCompleteUnit from './unitMiniSearch';
import { DecomposedFoodDescription } from '../parser/DecomposedFoodDescription';
import { findNameSuggestions } from './foodNameSearch';
import { isAmountParsable } from '../generate/isAmountParsable';

export default function findAutoCompletions(foodDescription: DecomposedFoodDescription): Suggestion[] {
const { foodName, amount, foodNameCompleted, unitCompleted } = foodDescription;
if (foodNameCompleted) {
if (amount && !unitCompleted) {
const suggestionWithAmount = _.partial(createSuggestion, foodName);
const amountAutoCompletions = findAmountAutoCompletions(parseAmount(amount))
.map(suggestionWithAmount);
if (_.size(amountAutoCompletions) > 0)
return amountAutoCompletions;
if (isAmountParsable(amount)) {
const amountAutoCompletions = findAmountAutoCompletions(parseAmount(amount))
.map(suggestionWithAmount);
if (_.size(amountAutoCompletions) > 0) {
return amountAutoCompletions;
}
}
}
return [createSuggestion(foodName, amount)];
}
Expand Down
Loading