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;
*/
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 final Map<String, List<String>> VOLUME_UNIT_VARIATIONS = Map.of(
"ml", List.of("ml", "milliliters", "millilitres"),
"cl", List.of("cl", "centiliters", "centilitres"),
"dl", List.of("dl", "deciliters", "decilitres"),
"l", List.of("l", "liters", "litres"),
"g", List.of("g", "grams"),
"kg", List.of("kg", "kilograms")
);
private static String parseUnit(String input) {
Pattern pattern = Pattern.compile("(\\d+(\\.\\d+)?)\\s*(g|kg|l|ml|dl|cl|Kg|L|STK)\\b", Pattern.CASE_INSENSITIVE);
Matcher matcher = pattern.matcher(input);
if (matcher.find()) {
String unit = matcher.group(3).toLowerCase();
if (!unit.equals("stk")) {
return unit;
}
}
return "STK";
}
public static Optional<List<String>> getVolumeFromProduct(Product product) {
String desc = product.getName() + " " + product.getDescription();
Matcher matcher = Pattern.compile(VOLUME_REGEX).matcher(desc);
private static double parseAmount(String input) {
Pattern pattern = Pattern.compile("(\\d+(\\.\\d+)?)\\s*(g|kg|l|ml|dl|cl|Kg|L|STK)\\b", Pattern.CASE_INSENSITIVE);
Matcher matcher = pattern.matcher(input);
if (matcher.find()) {
String volumeString = matcher.group(1);
double volume = Double.parseDouble(volumeString);
String unitString = matcher.group(3);
for (Map.Entry<String, List<String>> entry : VOLUME_UNIT_VARIATIONS.entrySet()) {
if (entry.getValue().contains(unitString)) {
return Optional.of(List.of(String.valueOf(volume), entry.getKey()));
}
double amount = Double.parseDouble(matcher.group(1));
Pattern multiplicationPattern = Pattern.compile("(\\d+)x(\\d+)");
Matcher multiplicationMatcher = multiplicationPattern.matcher(input);
if (multiplicationMatcher.find()) {
amount *= Integer.parseInt(multiplicationMatcher.group(1));
}
return amount;
}
return Optional.empty();
return 1.0;
}
/**
* Checks if a string contains any numbers
* @param s The string to check
......
......@@ -116,7 +116,7 @@ public class StatisticUtil {
if(diffInDays >= 365){
return co2Sum / (((double) (diffInDays +1) / 365.0) * (double) numberOfPerson);
}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;
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.TestMethodOrder;
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 {
Product product;
@BeforeEach
void setUp() {
this.product = Product.builder()
.ean(123456789)
.name("Pepsi Original 24x500 ml")
.description("Pepsi Original 24x500 ml")
.build();
}
private Product product;
@Test
@Test @Order(1)
void getVolumeFromProduct() {
assertEquals(List.of("500.0", "ml"), ProductUtil.getVolumeFromProduct(product).get());
this.product = Product.builder()
product = Product.builder()
.ean(123456789)
.name("test")
.description("Pepsi Original 24x500 ml")
.name("Pepsi Original")
.description("500 ml")
.build();
assertEquals(List.of("500.0", "ml"), ProductUtil.getVolumeFromProduct(product).get());
}
this.product = Product.builder()
@Test @Order(2)
void getVolumeFromProductMultipleUnits() {
product = Product.builder()
.ean(123456789)
.name("Pepsi Original 24x500ml")
.description("test")
.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());
this.product = Product.builder()
product = Product.builder()
.ean(123456789)
.name("test")
.description("Pepsi Original 24x500ml")
.build();
assertEquals(List.of("500.0", "ml"), ProductUtil.getVolumeFromProduct(product).get());
assertEquals(List.of("12000.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