-
Notifications
You must be signed in to change notification settings - Fork 0
Phase02 clean code corrected #4
base: main
Are you sure you want to change the base?
Conversation
for (String word : words) { | ||
Set<String> searchResult = WordFileNameFinder.searchWord(word, invertedIndex); | ||
if(finalResult.isEmpty()){ | ||
finalResult.addAll(searchResult); | ||
} | ||
else{ |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
extract method
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
method extracted
e.printStackTrace(); | ||
} | ||
} else { | ||
System.out.println("Specified path is not a directory."); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
exception
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
exception is used.
Map<String, Set<String>> invertedIndex = new HashMap<>(); | ||
|
||
for (String fileName : fileNamesInFolder) { | ||
try { | ||
File file = new File(directoryPath + "/" + fileName); | ||
Scanner myReader = new Scanner(file); | ||
|
||
while (myReader.hasNextLine()) { | ||
Set<String> documentIds = new HashSet<String>(); | ||
documentIds.add(fileName); | ||
|
||
String data = myReader.nextLine(); | ||
String[] splittedData = data.split(" "); | ||
|
||
for (String word : splittedData) { | ||
// Ignoring empty words | ||
if (word == "") { | ||
continue; | ||
} | ||
// Inserting new entry to inverted index table | ||
invertedIndex.computeIfAbsent(word, k -> new HashSet<>()).addAll(documentIds); | ||
} | ||
} | ||
myReader.close(); | ||
} catch (FileNotFoundException e) { | ||
System.out.println("An error occurred."); | ||
e.printStackTrace(); | ||
} | ||
} |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
extract class
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
class extracted.
// Getting directory path and user input | ||
Scanner myScanner = new Scanner(System.in); | ||
|
||
System.out.print("Enter Data Folder Path: "); | ||
final String directoryPath = myScanner.nextLine(); | ||
|
||
System.out.print("Enter Search Query: "); | ||
final String userInput = myScanner.nextLine(); | ||
|
||
myScanner.close(); | ||
|
||
// Finding name of all files that are in EnglishData directory | ||
ArrayList<String> fileNamesInFolder = FileReader.getFileNames(directoryPath); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
extract method
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
method extracted.
|
||
// Categorizing command words into or_words, necessary and forbidden groups | ||
ArrayList<String> necessaryWords = WordCategorizer.categorizer(userInput, ""); | ||
ArrayList<String> orWords = WordCategorizer.categorizer(userInput, "+"); | ||
ArrayList<String> notWords = WordCategorizer.categorizer(userInput, "-"); | ||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
extract method
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
method extracted.
ArrayList<String> orWords = WordCategorizer.categorizer(userInput, "+"); | ||
ArrayList<String> notWords = WordCategorizer.categorizer(userInput, "-"); | ||
|
||
// Initializing some sets for forming final answer |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
extract class
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
class extracted.
|
||
public class WordFileNameFinder { | ||
// Searchs a word in inverted index then returns name of files that are containing that word | ||
public static Set<String> searchWord(String word, Map<String, Set<String>> invertedIndex) { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
invertedIndex should be a class
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
now we have another class called InvertedIndexWordSearch.
…egorizer extracted in two classes.
اصول SOLID و clean code روی فاز قبلی پروژه اعمال شدند.