BuildIndex is the class that reads the list of words to index from an input file
ID: 3640716 • Letter: B
Question
BuildIndex is the class that reads the list of words to index from an input file, collects occurrences of these words using IndexBuilder objects, and outputs the final index. IndexBuilder is a class takes a source file and the array of words to index and finds all of the occurrences of each word in the file. The Index builder objects are:
By calling methods of each IndexBuilder object below, Write a BuildIndex class that should collect the total count of each word and where it appears in each file. The methods of IndexBuilder are:
public int getNoOfWords(){
return no_of_words;
}
public int getNoOfLines(){
return no_of_lines_in_file;
}
public List getLinesWithIndexWords(){
return lines_with_index_words;
}
Explanation / Answer
Following programs displays the Number of words, Lines and index of all the words in the file. Please rate. It has two classes IndexBuilder and BuildIndex and a text file in.txt. BuildIndex instantiates IndexBuilder and it prints No of words, lines and index of all the words. package words; import java.io.BufferedReader; import java.io.File; import java.io.FileReader; import java.io.IOException; import java.io.LineNumberReader; import java.io.StreamTokenizer; import java.util.ArrayList; import java.util.List; public class IndexBuilder { int no_of_words; int no_of_lines_in_file; List lines_with_index_words; public IndexBuilder() { try { Words(); Lines(); IndexOfAllWords(); } catch (Exception e) { e.printStackTrace(); } } public int getNoOfWords() { return no_of_words; } public int getNoOfLines() { return no_of_lines_in_file; } public List getLinesWithIndexWords() { return lines_with_index_words; } public ArrayList IndexOfAllWords() throws IOException { LineNumberReader rdr = new LineNumberReader(new FileReader("in.txt")); ArrayList results = new ArrayList(); try { String line; while ((line = rdr.readLine()) != null) { String[] strarr = line.split(" "); for (String str : strarr) { if (line.indexOf(str) >= 0) { results.add(rdr.getLineNumber()); System.out.println(str + " " + " occurs in Line " + rdr.getLineNumber()); } } } } finally { rdr.close(); } return results; } public void Words() throws IOException // count no. of words. { File f = new File("in.txt"); FileReader fr = new FileReader(f); BufferedReader br = new BufferedReader(fr); StreamTokenizer stz = new StreamTokenizer(br); int index = 0; int numWords = 0; while (index != StreamTokenizer.TT_EOF) { index = stz.nextToken(); numWords++; } System.out.println("Number of words in in.txt " + numWords); this.setNo_of_words(numWords); } public void Lines() throws IOException { File f = new File("in.txt"); FileReader fr = new FileReader(f); BufferedReader br = new BufferedReader(fr); LineNumberReader ln = new LineNumberReader(br); int count = 0; while (ln.readLine() != null) { count++; } System.out.println("Number of lines in in.txt " + count); this.setNo_of_lines_in_file(count); } public int getNo_of_words() { return this.no_of_words; } public void setNo_of_words(int noOfWords) { this.no_of_words = noOfWords; } public int getNo_of_lines_in_file() { return this.no_of_lines_in_file; } public void setNo_of_lines_in_file(int noOfLinesInFile) { this.no_of_lines_in_file = noOfLinesInFile; } public List getLines_with_index_words() { return this.lines_with_index_words; } public void setLines_with_index_words(List linesWithIndexWords) { this.lines_with_index_words = linesWithIndexWords; } } package words; public class BuildIndex { public static void main(String[] args) { IndexBuilder inb = new IndexBuilder(); } } in.txt - put the txt inside the Project folder. in the sam place where the words place is created. BuildIndex is the class that reads the list of words to index from an input file. Collects occurrences of these words using IndexBuilder objects, and outputs the final index. IndexBuilder is a class takes a source file and the array of words to index and finds all of the occurrences of each word in the file. The Index builder objects are: By calling methods of each IndexBuilder object below, Write a BuildIndex class that should collect the total count of each word and where it appears in each file. Ouput: Number of words in in.txt 93 Number of lines in in.txt 6 BuildIndex occurs in Line 1 is occurs in Line 1 the occurs in Line 1 class occurs in Line 1 that occurs in Line 1 reads occurs in Line 1 the occurs in Line 1 list occurs in Line 1 of occurs in Line 1 words occurs in Line 1 to occurs in Line 1 index occurs in Line 1 from occurs in Line 1 an occurs in Line 1 input occurs in Line 1 file. occurs in Line 1 Collects occurs in Line 2 occurrences occurs in Line 2 of occurs in Line 2 these occurs in Line 2 words occurs in Line 2 using occurs in Line 2 IndexBuilder occurs in Line 2 objects, occurs in Line 2 and occurs in Line 2 outputs occurs in Line 2 the occurs in Line 2 final occurs in Line 2 index. occurs in Line 2 IndexBuilder occurs in Line 3 is occurs in Line 3 a occurs in Line 3 class occurs in Line 3 takes occurs in Line 3 a occurs in Line 3 source occurs in Line 3 file occurs in Line 3 and occurs in Line 3 the occurs in Line 3 array occurs in Line 3 of occurs in Line 3 words occurs in Line 3 to occurs in Line 3 index occurs in Line 3 and occurs in Line 4 finds occurs in Line 4 all occurs in Line 4 of occurs in Line 4 the occurs in Line 4 occurrences occurs in Line 4 of occurs in Line 4 each occurs in Line 4 word occurs in Line 4 in occurs in Line 4 the occurs in Line 4 file. occurs in Line 4 The occurs in Line 4 Index occurs in Line 4 builder occurs in Line 4 objects occurs in Line 4 are: occurs in Line 4 By occurs in Line 5 calling occurs in Line 5 methods occurs in Line 5 of occurs in Line 5 each occurs in Line 5 IndexBuilder occurs in Line 5 object occurs in Line 5 below, occurs in Line 5 Write occurs in Line 6 a occurs in Line 6 BuildIndex occurs in Line 6 class occurs in Line 6 that occurs in Line 6 should occurs in Line 6 collect occurs in Line 6 the occurs in Line 6 total occurs in Line 6 count occurs in Line 6 of occurs in Line 6 each occurs in Line 6 word occurs in Line 6 and occurs in Line 6 where occurs in Line 6 it occurs in Line 6 appears occurs in Line 6 in occurs in Line 6 each occurs in Line 6 file. occurs in Line 6Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.