I am having an issue checking for a duplicate sentence in a text file. First off the way my program works is: the input sentence is scanned for a matching word(s) and if the word(s) is found in a text file the sentence containing the word(s) is outputted. In addition i am checking for the synonyms of both words shoes and bag so if a user enters some synonym of shoes or bag i will still search the text file. But let's say for example, the text file contains the bag is on the floor. the bag and shoes are near you.... etc
if a user enters " where are the bag and shoes?" and bag,shoes are the keywords I want to output the sentence the bag and shoes are near you only instead of both sentences simultaneously. My issue is both sentences being outputted oppose to one containing both keywords
Can someone assist me?
Here is my code
public static class DicEntry {
String key;
String[] syns;
Pattern pattern;
public DicEntry(String key, String... syns) {
this.key = key;
this.syns = syns;
pattern = Pattern.compile(".*(?:"
+ Stream.concat(Stream.of(key), Stream.of(syns))
.map(x -> "\\b" + Pattern.quote(x) + "\\b")
.collect(Collectors.joining("|")) + ").*");
}
}
public static void parseFile(String s) throws IOException {
File file = new File("data.txt");
Scanner forget = new Scanner(System.in);
Scanner scanner = new Scanner(file);
int flag_found = 0;
while (scanner.hasNextLine()) {
final String lineFromFile = scanner.nextLine();
if (lineFromFile.contains(s)) {
// a match!
System.out.println(lineFromFile);
}
}
}
private static List<DicEntry> populateSynonymMap() {
List<DicEntry> responses = new ArrayList<>();
responses.add(new DicEntry("bag", "purse","black"));
responses.add(new DicEntry("shoe", "heels","gas"));
return responses;
}
public static void getinput() throws IOException {
List<DicEntry> synonymMap = populateSynonymMap(); // populate the map
Scanner scanner = new Scanner(System.in);
String input = null;
/* End Initialization */
System.out.println("Welcome ");
System.out.println("What would you like to know?");
System.out.print("> ");
input = scanner.nextLine().toLowerCase();
String[] inputs = input.split(" ");
int flag_found = 0;
for (DicEntry entry : synonymMap) { // iterate over each word of the
// sentence.
if (entry.pattern.matcher(input).matches()) {
// System.out.println(entry.key);
parseFile(entry.key);
}
}
}
public static void main(String args[]) throws ParseException, IOException {
/* Initialization */
getinput();
}
}
Here is my text file:
the bag and shoes are near you
the bag is over there
Aucun commentaire:
Enregistrer un commentaire