Skip to content

Commit

Permalink
Merge pull request #139 from kronos-et-al/129-geflügel-aus-dem-gerich…
Browse files Browse the repository at this point in the history
…tsnamen-erkennen

Poultry recogniser
  • Loading branch information
worldofjoni authored Apr 19, 2024
2 parents f2ec05a + ab985dc commit ff35ef5
Show file tree
Hide file tree
Showing 13 changed files with 48 additions and 15 deletions.

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

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

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

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

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

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

3 changes: 3 additions & 0 deletions backend/migrations/20240313130248_poultry_checker.down.sql
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
-- Add down migration script here
UPDATE food SET food_type = 'UNKNOWN' WHERE food_type = 'POULTRY';

5 changes: 5 additions & 0 deletions backend/migrations/20240313130248_poultry_checker.up.sql
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
-- Add up migration script here
ALTER TYPE meal_type add value 'POULTRY';
COMMIT;

UPDATE food SET food_type = 'POULTRY' WHERE name ~ '(?i)ente|chicken|pute|geflügel|h[üäua]hn';
25 changes: 21 additions & 4 deletions backend/src/layer/data/swka_parser/html_parser.rs
Original file line number Diff line number Diff line change
Expand Up @@ -133,6 +133,8 @@ lazy_static! {
static ref VOLUME_REGEX: Regex = Regex::new(r"([0-9]*),([0-9]{2}) l").expect(REGEX_PARSE_E_MSG);

static ref ID_REGEX: Regex = Regex::new(r"[0-9]{18,}").expect(REGEX_PARSE_E_MSG);

static ref POULTRY_REGEX: Regex = Regex::new(r"(?i)ente|chicken|pute|geflügel|h[üäua]hn").expect(REGEX_PARSE_E_MSG);
}

const DISH_NODE_CLASS_SELECTOR_PREFIX: &str = "tr.mt-";
Expand Down Expand Up @@ -316,12 +318,13 @@ impl HTMLParser {
}

fn get_dish(dish_node: &ElementRef) -> Option<Dish> {
let name = Self::get_dish_name(dish_node)?;
Some(Dish {
name: Self::get_dish_name(dish_node)?,
food_type: Self::get_food_type(dish_node, &name).unwrap_or(FoodType::Unknown),
name,
price: Self::get_dish_price(dish_node),
allergens: Self::get_dish_allergens(dish_node).unwrap_or_default(),
additives: Self::get_dish_additives(dish_node).unwrap_or_default(),
food_type: Self::get_dish_type(dish_node).unwrap_or(FoodType::Unknown),
env_score: Self::get_dish_env_score(dish_node),
nutrition_data: Self::get_dish_nutrition_data(dish_node),
})
Expand Down Expand Up @@ -397,14 +400,28 @@ impl HTMLParser {
.collect()
}

fn get_dish_type(dish_node: &ElementRef) -> Option<FoodType> {
fn get_food_type(dish_node: &ElementRef, name: &str) -> Option<FoodType> {
let preliminary_dish_type = Self::get_preliminary_food_type(dish_node);
if (preliminary_dish_type.is_none() || Some(FoodType::Unknown) == preliminary_dish_type)
&& Self::is_poultry(name)
{
return Some(FoodType::Poultry);
}
preliminary_dish_type
}

fn get_preliminary_food_type(dish_node: &ElementRef) -> Option<FoodType> {
let dish_type_node = dish_node.select(&DISH_TYPE_NODE_CLASS_SELECTOR).next()?;
dish_type_node
.value()
.attr(DISH_TYPE_ATTRIBUTE_NAME)
.map(FoodType::parse)
}

fn is_poultry(name: &str) -> bool {
POULTRY_REGEX.captures(name).is_some()
}

fn get_dish_env_score(dish_node: &ElementRef) -> Option<ParseEnvironmentInfo> {
let env_info = ParseEnvironmentInfo {
co2_rating: Self::get_co2_rating(dish_node)?,
Expand Down Expand Up @@ -589,7 +606,7 @@ mod tests {
let file_contents = read_from_file(path).unwrap();
let canteen_data = HTMLParser::new().transform(&file_contents, 42_u32).unwrap();

//let _ = write_output_to_file(path, &canteen_data);
//let remove_for_producton = write_output_to_file(path, &canteen_data);
let expected = read_from_file(&path.replace(".html", ".txt"))
.unwrap()
.replace("\r\n", "\n");
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -297,7 +297,7 @@
We,
],
additives: [],
food_type: Unknown,
food_type: Poultry,
env_score: Some(
ParseEnvironmentInfo {
co2_rating: 1,
Expand Down
6 changes: 3 additions & 3 deletions backend/src/layer/data/swka_parser/test_data/test_normal.txt
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@
We,
],
additives: [],
food_type: Unknown,
food_type: Poultry,
env_score: Some(
ParseEnvironmentInfo {
co2_rating: 1,
Expand Down Expand Up @@ -783,7 +783,7 @@
We,
],
additives: [],
food_type: Unknown,
food_type: Poultry,
env_score: None,
nutrition_data: None,
},
Expand Down Expand Up @@ -4829,7 +4829,7 @@
Colorant,
AntioxidantAgents,
],
food_type: Unknown,
food_type: Poultry,
env_score: None,
nutrition_data: None,
},
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -315,7 +315,7 @@ mod test {
assert!(450.0 < average);
assert!(460.0 > average);

let dishes = vec![];
let dishes = [];
let average = RelationResolver::<MealplanManagementDatabaseMock>::average(dishes.iter());
assert!((average - 0.0).abs() < f64::EPSILON);
}
Expand Down
2 changes: 2 additions & 0 deletions backend/src/util.rs
Original file line number Diff line number Diff line change
Expand Up @@ -139,6 +139,8 @@ pub enum FoodType {
PorkAw,
/// This meal contains fish.
Fish,
/// This meal contains poultry, such as chicken, turkey, or similar
Poultry,
/// It is unknown whether this meal contains any meat or not.
Unknown,
}
Expand Down

0 comments on commit ff35ef5

Please sign in to comment.