diff --git a/src/main/java/InvertedIndex.java b/src/main/java/InvertedIndex.java index 684fc0a..472a7ca 100644 --- a/src/main/java/InvertedIndex.java +++ b/src/main/java/InvertedIndex.java @@ -58,7 +58,6 @@ public void indexFile(File file) throws IOException { public Set search(ArrayList wordsToFind) { Set answer = new HashSet<>(); for (String words : wordsToFind) { - String word = words.toLowerCase(); for (String key : indexedWords.keySet()) { Matcher matcher = Pattern.compile(word).matcher(key); @@ -113,10 +112,9 @@ public Set findCommonWords(ArrayList> wordsToFindCommon) { private static class Tuple { private final int fileNumber; - public Tuple(int fileno) { - this.fileNumber = fileno; + public Tuple(int fileNumber) { + this.fileNumber = fileNumber; } - } } \ No newline at end of file diff --git a/src/main/java/Main.java b/src/main/java/Main.java index 3163bf3..370ac02 100644 --- a/src/main/java/Main.java +++ b/src/main/java/Main.java @@ -4,11 +4,12 @@ public class Main { public static void main(String[] args) { + preprocesses(); + } + public static void preprocesses() { File directoryPath = new File("C:\\Users\\ASUS\\IdeaProjects\\codestar\\src\\main\\resources\\EnglishData"); File[] filesList = directoryPath.listFiles(); - - try { InvertedIndex idx = new InvertedIndex(); assert filesList != null; diff --git a/src/main/java/TakeInput.java b/src/main/java/TakeInput.java index 3e790f4..512392e 100644 --- a/src/main/java/TakeInput.java +++ b/src/main/java/TakeInput.java @@ -1,4 +1,5 @@ package main.java; + import java.util.*; import java.util.regex.Matcher; import java.util.regex.Pattern; @@ -6,7 +7,6 @@ public class TakeInput { public TakeInput(InvertedIndex idx) { - System.out.println("1"); getOrder(idx); } @@ -15,26 +15,40 @@ public void getOrder(InvertedIndex idx) { while (true) { String input = scanner.nextLine(); String[] inputSplited = input.split("(\\s+)"); + if (input.equals("--back")) + break; ArrayList plusStrings = new ArrayList<>(); ArrayList minusStrings = new ArrayList<>(); ArrayList normalStrings = new ArrayList<>(); - for (String string : inputSplited) addItemToOneOfThreeArrayLists(string, plusStrings, minusStrings, normalStrings); - Set answer = idx.search(plusStrings); - Set toDelete = idx.search(minusStrings); - ArrayList> commons = new ArrayList<>(); - for (String normalString : normalStrings) { - ArrayList arrayList = new ArrayList<>(); - arrayList.add(normalString); - commons.add(idx.search(arrayList)); - } - answer = idx.findCommonFiles(answer, commons); - System.out.println(answer); - answer = idx.deleteGivenFiles(answer, toDelete); - for (String s : answer) System.out.println(s); + for (String string : inputSplited) + addItemToOneOfThreeArrayLists(string, plusStrings, minusStrings, normalStrings); + showResult(processes(idx, plusStrings, minusStrings, normalStrings)); + } + } + + private void showResult(Set answer) { + for (String s : answer) System.out.println(s); + + } + + private Set processes(InvertedIndex idx, ArrayList plusStrings, + ArrayList minusStrings, ArrayList normalStrings) { + Set answer = idx.search(plusStrings); + Set toDelete = idx.search(minusStrings); + ArrayList> commons = new ArrayList<>(); + for (String normalString : normalStrings) { + ArrayList arrayList = new ArrayList<>(); + arrayList.add(normalString); + commons.add(idx.search(arrayList)); } + answer = idx.findCommonFiles(answer, commons); + System.out.println(answer); + answer = idx.deleteGivenFiles(answer, toDelete); + return answer; } - private void addItemToOneOfThreeArrayLists(String string, ArrayList plusStrings, ArrayList minusStrings, ArrayList normalStrings) { + private void addItemToOneOfThreeArrayLists(String string, ArrayList plusStrings, + ArrayList minusStrings, ArrayList normalStrings) { Pattern pattern = Pattern.compile("^\\+(.+)$"); Matcher matcher = pattern.matcher(string); Pattern pattern1 = Pattern.compile("^-(.+)$");