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

Merge #36

Merged
merged 11 commits into from
Mar 31, 2019
43 changes: 17 additions & 26 deletions src/planmysem/common/Messages.java
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package planmysem.common;

import java.time.LocalDate;
import java.util.List;
import java.util.Map;
import java.util.Set;

Expand Down Expand Up @@ -55,6 +56,7 @@ public static String craftSelectedMessage(Set<String> tags) {
sb.append("\n");
count++;
}
sb.append("\nEnter 'list n/{name} OR t/{tag}' to list all slots related to the name/tag\n");

return sb.toString();
}
Expand Down Expand Up @@ -85,40 +87,29 @@ public static String craftSelectedMessage(String header,
/**
* Craft selected message via weighted Set of Pairs.
*/
public static String craftListMessage(Set<WeightedName> pairs) {
public static String craftListMessage(List<WeightedName> tries) {
StringBuilder sb = new StringBuilder();
sb.append("Here are the closest matching names/tags: \n");

int count = 1;
for (WeightedName p : pairs) {
sb.append(count);
sb.append(".\t");
sb.append(p.getName());
sb.append("\n");
count++;
}
sb.append("\nEnter 'list n/{name} OR t/{tag}' to list all slots related to the name/tag\n");

return sb.toString();
}

/**
* Craft selected message via weighted Set of Pairs.
*/
public static String craftSelectedMessagePair(Set<WeightedName> pairs) {
StringBuilder sb = new StringBuilder();
sb.append("Here are the closest matching names/tags: \n");

int count = 1;
for (WeightedName p : pairs) {
sb.append(count);
sb.append(".\t");
sb.append(p.getName());
for (WeightedName wn : tries) {
sb.append("\n");
sb.append(count + ".\t");
sb.append("Name: ");
sb.append(wn.getName());
sb.append(",\n\t");
sb.append("Date: ");
sb.append(wn.getMap().getKey().toString());
sb.append(",\n\t");
sb.append("Start Time: ");
sb.append(wn.getSlot().getStartTime());
sb.append("\n\t");
sb.append("Tags: ");
sb.append(wn.getSlot().getTags());
sb.append("\n");
count++;
}
sb.append("\nEnter 'list n/{name} OR t/{tag}' to list all slots related to the name/tag\n");

return sb.toString();
}

Expand Down
51 changes: 26 additions & 25 deletions src/planmysem/logic/commands/FindCommand.java
Original file line number Diff line number Diff line change
Expand Up @@ -2,19 +2,24 @@
package planmysem.logic.commands;

import java.time.LocalDate;
import java.util.ArrayList;
import java.util.Comparator;
import java.util.List;
import java.util.Map;
import java.util.PriorityQueue;
import java.util.Queue;
import java.util.Set;
import java.util.TreeSet;

import javafx.util.Pair;

import planmysem.common.Messages;
import planmysem.common.Utils;
import planmysem.logic.CommandHistory;
import planmysem.model.Model;
import planmysem.model.semester.Day;
import planmysem.model.semester.ReadOnlyDay;
import planmysem.model.semester.WeightedName;
import planmysem.model.slot.ReadOnlySlot;
import planmysem.model.slot.Slot;

/**
Expand Down Expand Up @@ -53,21 +58,8 @@ public int compare(WeightedName p1, WeightedName p2) {
}
});

private Set<WeightedName> selectedNames = new TreeSet<>(new Comparator<WeightedName>() {
@Override
public int compare(WeightedName p1, WeightedName p2) {
String n1 = p1.getName();
String n2 = p2.getName();
int d1 = p1.getDist();
int d2 = p2.getDist();

if (d1 != d2) {
return d1 - d2;
} else {
return n1.compareTo(n2);
}
}
});
private List<WeightedName> selectedSlots = new ArrayList<>();
private List<Pair<LocalDate, Pair<ReadOnlyDay, ReadOnlySlot>>> lastShownList = new ArrayList<>();

public FindCommand(String name, String tag) {
this.keyword = (name == null) ? tag.trim() : name.trim();
Expand All @@ -79,11 +71,11 @@ public CommandResult execute(Model model, CommandHistory commandHistory) {
for (Map.Entry<LocalDate, Day> entry : model.getDays().entrySet()) {
for (Slot slot : entry.getValue().getSlots()) {
if (isFindByName) {
generateDiscoveredNames(keyword, slot.getName());
generateDiscoveredNames(keyword, slot.getName(), entry, slot);
} else {
Set<String> tagSet = slot.getTags();
for (String tag : tagSet) {
generateDiscoveredNames(keyword, tag);
generateDiscoveredNames(keyword, tag, entry, slot);
}
}
}
Expand All @@ -93,19 +85,28 @@ public CommandResult execute(Model model, CommandHistory commandHistory) {
return new CommandResult(MESSAGE_SUCCESS_NONE);
}

while (!weightedNames.isEmpty() && weightedNames.peek().getDist() < minDistance + 2) {
selectedNames.add(weightedNames.poll());
while (!weightedNames.isEmpty() && weightedNames.peek().getDist() < minDistance + 1) {
selectedSlots.add(weightedNames.poll());
}

for (WeightedName entry : selectedSlots) {
ReadOnlyDay day = entry.getMap().getValue();
ReadOnlySlot slot = entry.getSlot();
Pair<ReadOnlyDay, ReadOnlySlot> pair = new Pair<>(day, slot);
lastShownList.add(new Pair<>(entry.getMap().getKey(), pair));
}
model.setLastShownList(lastShownList);

return new CommandResult(String.format(MESSAGE_SUCCESS, selectedNames.size(),
Messages.craftListMessage(selectedNames)));
return new CommandResult(String.format(MESSAGE_SUCCESS, selectedSlots.size(),
Messages.craftListMessage(selectedSlots)));
}

/**
* If a slot entry is found, calculates the Levenshtein Distance between the name and the keyword.
* Updates the weightedNames PQ with the new WeightedName pair containing the name and its weight.
*/
private void generateDiscoveredNames(String keyword, String compareString) {
private void generateDiscoveredNames(String keyword, String compareString,
Map.Entry<LocalDate, Day> entry, Slot slot) {
if (compareString == null) {
return;
}
Expand All @@ -115,8 +116,8 @@ private void generateDiscoveredNames(String keyword, String compareString) {
minDistance = dist;
}
//System.out.println(compareString + " vs key: " + keyword + " dist: " + dist);
WeightedName distNamePair = new WeightedName(compareString, dist);
WeightedName distNameTrie = new WeightedName(entry, slot, dist);

weightedNames.add(distNamePair);
weightedNames.add(distNameTrie);
}
}
8 changes: 8 additions & 0 deletions src/planmysem/logic/parser/ParserManager.java
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,8 @@
import planmysem.logic.commands.HelpCommand;
import planmysem.logic.commands.HistoryCommand;
import planmysem.logic.commands.ListCommand;
import planmysem.logic.commands.RedoCommand;
import planmysem.logic.commands.UndoCommand;
import planmysem.logic.commands.ViewCommand;
import planmysem.logic.parser.exceptions.ParseException;

Expand Down Expand Up @@ -72,6 +74,12 @@ public Command parseCommand(String userInput) throws ParseException {
case HistoryCommand.COMMAND_WORD:
return new HistoryCommand();

case UndoCommand.COMMAND_WORD:
return new UndoCommand();

case RedoCommand.COMMAND_WORD:
return new RedoCommand();

case ClearCommand.COMMAND_WORD:
return new ClearCommand();

Expand Down
24 changes: 21 additions & 3 deletions src/planmysem/model/semester/WeightedName.java
Original file line number Diff line number Diff line change
@@ -1,23 +1,41 @@
//@@author marcus-pzj
package planmysem.model.semester;

import java.time.LocalDate;
import java.util.Map;

import planmysem.model.slot.Slot;

/**
* WeightedName of integer and string
*/

public class WeightedName {
private int dist;
private String name;
private Map.Entry<LocalDate, Day> map;
private Slot slot;

public WeightedName(String name, int dist) {
this.name = name;
public WeightedName(Map.Entry<LocalDate, Day> map, Slot slot, int dist) {
this.map = map;
this.dist = dist;
this.slot = slot;
this.name = slot.getName();
}

public int getDist() {
return this.dist;
}

public Map.Entry<LocalDate, Day> getMap() {
return map;
}

public String getName() {
return name;
return this.name;
}

public Slot getSlot() {
return this.slot;
}
}