diff --git a/build.gradle b/build.gradle index 92d317df95ca..0f04c9d56119 100644 --- a/build.gradle +++ b/build.gradle @@ -67,6 +67,7 @@ dependencies { implementation group: 'com.sun.xml.bind', name: 'jaxb-core', version: '2.3.0' implementation group: 'javax.activation', name: 'activation', version: '1.1.1' + implementation 'me.xdrop:fuzzywuzzy:1.2.0' implementation group: 'org.mindrot', name: 'jbcrypt', version: '0.4' diff --git a/src/main/java/seedu/address/logic/commands/machine/FindMachineCommand.java b/src/main/java/seedu/address/logic/commands/machine/FindMachineCommand.java index 0d81f2b5d032..113611faed4e 100644 --- a/src/main/java/seedu/address/logic/commands/machine/FindMachineCommand.java +++ b/src/main/java/seedu/address/logic/commands/machine/FindMachineCommand.java @@ -8,8 +8,7 @@ import seedu.address.logic.commands.CommandResult; import seedu.address.model.Model; import seedu.address.model.machine.MachineNameContainsKeywordsPredicate; - - +import seedu.address.model.machine.MachineNameFuzzyWuzzyPredicate; /** @@ -26,15 +25,21 @@ public class FindMachineCommand extends Command { + "Example: " + COMMAND_WORD + " machine1 machine2 machine3"; private final MachineNameContainsKeywordsPredicate predicate; + private final MachineNameFuzzyWuzzyPredicate fuzzyWuzzyPredicate; - public FindMachineCommand(MachineNameContainsKeywordsPredicate predicate) { + public FindMachineCommand(MachineNameContainsKeywordsPredicate predicate, + MachineNameFuzzyWuzzyPredicate fuzzyWuzzyPredicate) { this.predicate = predicate; + this.fuzzyWuzzyPredicate = fuzzyWuzzyPredicate; } @Override public CommandResult execute(Model model, CommandHistory history) { requireNonNull(model); model.updateFilteredMachineList(predicate); + if (model.getFilteredMachineList().size() < predicate.getNumberOfKeywords()) { + model.updateFilteredMachineList(fuzzyWuzzyPredicate); + } return new CommandResult( String.format(Messages.MESSAGE_MACHINE_LISTED_OVERVIEW, model.getFilteredMachineList().size())); } diff --git a/src/main/java/seedu/address/logic/parser/machine/FindMachineCommandParser.java b/src/main/java/seedu/address/logic/parser/machine/FindMachineCommandParser.java index bb2264cdcf0d..222703f15e5e 100644 --- a/src/main/java/seedu/address/logic/parser/machine/FindMachineCommandParser.java +++ b/src/main/java/seedu/address/logic/parser/machine/FindMachineCommandParser.java @@ -8,6 +8,7 @@ import seedu.address.logic.parser.Parser; import seedu.address.logic.parser.exceptions.ParseException; import seedu.address.model.machine.MachineNameContainsKeywordsPredicate; +import seedu.address.model.machine.MachineNameFuzzyWuzzyPredicate; /** * Parses input arguments and creates a new FindMachineCommand object @@ -28,7 +29,8 @@ public FindMachineCommand parse(String args) throws ParseException { String[] nameKeywords = trimmedArgs.split("\\s+"); - return new FindMachineCommand(new MachineNameContainsKeywordsPredicate(Arrays.asList(nameKeywords))); + return new FindMachineCommand(new MachineNameContainsKeywordsPredicate(Arrays.asList(nameKeywords)), + new MachineNameFuzzyWuzzyPredicate(Arrays.asList(nameKeywords))); } } diff --git a/src/main/java/seedu/address/model/machine/MachineNameContainsKeywordsPredicate.java b/src/main/java/seedu/address/model/machine/MachineNameContainsKeywordsPredicate.java index df5aa49ee8c6..9e033a63b4ba 100644 --- a/src/main/java/seedu/address/model/machine/MachineNameContainsKeywordsPredicate.java +++ b/src/main/java/seedu/address/model/machine/MachineNameContainsKeywordsPredicate.java @@ -11,14 +11,19 @@ public class MachineNameContainsKeywordsPredicate implements Predicate { private final List keywords; + public MachineNameContainsKeywordsPredicate(List keywords) { this.keywords = keywords; } + public Integer getNumberOfKeywords () { + return keywords.size(); + } + @Override public boolean test(Machine machine) { return keywords.stream() - .anyMatch(keyword -> StringUtil.containsWordIgnoreCase(machine.getName().fullName, keyword)); + .anyMatch(keyword -> StringUtil.containsWordIgnoreCase(machine.getName().fullName, keyword)); } @Override diff --git a/src/main/java/seedu/address/model/machine/MachineNameFuzzyWuzzyPredicate.java b/src/main/java/seedu/address/model/machine/MachineNameFuzzyWuzzyPredicate.java new file mode 100644 index 000000000000..a4c94384897f --- /dev/null +++ b/src/main/java/seedu/address/model/machine/MachineNameFuzzyWuzzyPredicate.java @@ -0,0 +1,31 @@ +package seedu.address.model.machine; + +import java.util.List; +import java.util.function.Predicate; + +import me.xdrop.fuzzywuzzy.FuzzySearch; + +/** + * Tests that a {@code Machines}'s {@code Name} fufills the fuzzywuzzy minimum ration. + */ +public class MachineNameFuzzyWuzzyPredicate implements Predicate { + + private final List keywords; + + public MachineNameFuzzyWuzzyPredicate(List keywords) { + this.keywords = keywords; + } + + @Override + public boolean test(Machine machine) { + return keywords.stream() + .anyMatch(keyword -> FuzzySearch.partialRatio(machine.getName().fullName, keyword) > 50); + } + + @Override + public boolean equals(Object other) { + return other == this // short circuit if same object + || (other instanceof MachineNameFuzzyWuzzyPredicate // instanceof handles nulls + && keywords.equals(((MachineNameFuzzyWuzzyPredicate) other).keywords)); // state check + } +}