Skip to content
Snippets Groups Projects
Commit 8d11b5cd authored by Pedro Pablo Cardona Arroyave's avatar Pedro Pablo Cardona Arroyave
Browse files

Merge branch '263-enhance-the-match-algorithm-for-weekly-menu' into 'main'

All the bugs related to the alogrithms was fixed

Closes #263

See merge request idatt2106-v23-03/backend!214
parents 26b5fc7d 495e474d
No related branches found
No related tags found
No related merge requests found
...@@ -17,34 +17,50 @@ import java.util.regex.Pattern; ...@@ -17,34 +17,50 @@ import java.util.regex.Pattern;
*/ */
public class ProductUtil { public class ProductUtil {
public static Optional<List<String>> getVolumeFromProduct(Product product) {
String total = product.getName() + " " + product.getDescription();
double amount = parseAmount(total);
String unit = parseUnit(total);
if (unit.equals("STK") && total.matches(".*\\d+x\\d+.*")) {
// If no unit was found but the description matches "number x number", assume a multiplication and calculate the result
Pattern pattern = Pattern.compile("(\\d+)x(\\d+)");
Matcher matcher = pattern.matcher(total);
if (matcher.find()) {
amount = Integer.parseInt(matcher.group(1)) * Integer.parseInt(matcher.group(2));
}
}
return Optional.of(List.of(amount + "", unit));
}
private static final String VOLUME_REGEX = "(\\d+(\\.\\d+)?)\\s*(mls?|centiliters?|deciliters?|liters?|grams?|kilograms?)"; private static String parseUnit(String input) {
private static final Map<String, List<String>> VOLUME_UNIT_VARIATIONS = Map.of( Pattern pattern = Pattern.compile("(\\d+(\\.\\d+)?)\\s*(g|kg|l|ml|dl|cl|Kg|L|STK)\\b", Pattern.CASE_INSENSITIVE);
"ml", List.of("ml", "milliliters", "millilitres"), Matcher matcher = pattern.matcher(input);
"cl", List.of("cl", "centiliters", "centilitres"), if (matcher.find()) {
"dl", List.of("dl", "deciliters", "decilitres"), String unit = matcher.group(3).toLowerCase();
"l", List.of("l", "liters", "litres"), if (!unit.equals("stk")) {
"g", List.of("g", "grams"), return unit;
"kg", List.of("kg", "kilograms") }
); }
return "STK";
}
public static Optional<List<String>> getVolumeFromProduct(Product product) { private static double parseAmount(String input) {
String desc = product.getName() + " " + product.getDescription(); Pattern pattern = Pattern.compile("(\\d+(\\.\\d+)?)\\s*(g|kg|l|ml|dl|cl|Kg|L|STK)\\b", Pattern.CASE_INSENSITIVE);
Matcher matcher = Pattern.compile(VOLUME_REGEX).matcher(desc); Matcher matcher = pattern.matcher(input);
if (matcher.find()) { if (matcher.find()) {
String volumeString = matcher.group(1); double amount = Double.parseDouble(matcher.group(1));
double volume = Double.parseDouble(volumeString); Pattern multiplicationPattern = Pattern.compile("(\\d+)x(\\d+)");
String unitString = matcher.group(3); Matcher multiplicationMatcher = multiplicationPattern.matcher(input);
for (Map.Entry<String, List<String>> entry : VOLUME_UNIT_VARIATIONS.entrySet()) { if (multiplicationMatcher.find()) {
if (entry.getValue().contains(unitString)) { amount *= Integer.parseInt(multiplicationMatcher.group(1));
return Optional.of(List.of(String.valueOf(volume), entry.getKey()));
}
} }
return amount;
} }
return Optional.empty(); return 1.0;
} }
/** /**
* Checks if a string contains any numbers * Checks if a string contains any numbers
* @param s The string to check * @param s The string to check
......
...@@ -116,7 +116,7 @@ public class StatisticUtil { ...@@ -116,7 +116,7 @@ public class StatisticUtil {
if(diffInDays >= 365){ if(diffInDays >= 365){
return co2Sum / (((double) (diffInDays +1) / 365.0) * (double) numberOfPerson); return co2Sum / (((double) (diffInDays +1) / 365.0) * (double) numberOfPerson);
}else { }else {
return (118.0/diffInDays) + (( co2Sum / (((double) (diffInDays +1) / 365.0) * (double) numberOfPerson))/(365.0-(double) diffInDays)); return (118.0/((double) diffInDays +1.0)) + (( co2Sum / (((double) (diffInDays +1) / 365.0) * (double) numberOfPerson))/(365.0-(double) diffInDays));
} }
} }
......
package ntnu.idatt2016.v233.SmartMat.util; package ntnu.idatt2016.v233.SmartMat.util;
import ntnu.idatt2016.v233.SmartMat.entity.product.Product; import ntnu.idatt2016.v233.SmartMat.entity.product.Product;
import org.junit.jupiter.api.BeforeEach; import org.junit.jupiter.api.MethodOrderer;
import org.junit.jupiter.api.Order;
import org.junit.jupiter.api.Test; import org.junit.jupiter.api.Test;
import org.junit.jupiter.api.TestMethodOrder;
import java.util.List; import java.util.List;
import java.util.Optional;
import static org.junit.jupiter.api.Assertions.*; import static org.junit.jupiter.api.Assertions.assertEquals;
@TestMethodOrder(MethodOrderer.OrderAnnotation.class)
class ProductUtilTest { class ProductUtilTest {
Product product; private Product product;
@BeforeEach
void setUp() {
this.product = Product.builder()
.ean(123456789)
.name("Pepsi Original 24x500 ml")
.description("Pepsi Original 24x500 ml")
.build();
}
@Test @Test @Order(1)
void getVolumeFromProduct() { void getVolumeFromProduct() {
assertEquals(List.of("500.0", "ml"), ProductUtil.getVolumeFromProduct(product).get()); product = Product.builder()
this.product = Product.builder()
.ean(123456789) .ean(123456789)
.name("test") .name("Pepsi Original")
.description("Pepsi Original 24x500 ml") .description("500 ml")
.build(); .build();
assertEquals(List.of("500.0", "ml"), ProductUtil.getVolumeFromProduct(product).get()); assertEquals(List.of("500.0", "ml"), ProductUtil.getVolumeFromProduct(product).get());
}
this.product = Product.builder() @Test @Order(2)
void getVolumeFromProductMultipleUnits() {
product = Product.builder()
.ean(123456789) .ean(123456789)
.name("Pepsi Original 24x500ml") .name("Pepsi Original 24x500ml")
.description("test") .description("test")
.build(); .build();
assertEquals(List.of("12000.0", "ml"), ProductUtil.getVolumeFromProduct(product).get());
}
@Test @Order(3)
void getVolumeFromProductCombined() {
product = Product.builder()
.ean(123456789)
.name("test")
.description("Pepsi Original 24x500 ml")
.build();
assertEquals(List.of("12000.0", "ml"), ProductUtil.getVolumeFromProduct(product).get());
assertEquals(List.of("500.0", "ml"), ProductUtil.getVolumeFromProduct(product).get()); product = Product.builder()
this.product = Product.builder()
.ean(123456789) .ean(123456789)
.name("test") .name("test")
.description("Pepsi Original 24x500ml") .description("Pepsi Original 24x500ml")
.build(); .build();
assertEquals(List.of("12000.0", "ml"), ProductUtil.getVolumeFromProduct(product).get());
assertEquals(List.of("500.0", "ml"), ProductUtil.getVolumeFromProduct(product).get());
} }
} }
\ No newline at end of file
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment