mirror of
https://github.com/Rushilwiz/APCS.git
synced 2025-04-06 05:20:21 -04:00
122 lines
3.0 KiB
Java
122 lines
3.0 KiB
Java
// Name: B6-24
|
|
// Date: 3/8/2020
|
|
|
|
import java.io.*;
|
|
import java.util.*;
|
|
|
|
/* This program takes a text file, creates an index (by line numbers)
|
|
* for all the words in the file and writes the index
|
|
* into the output file. The program prompts the user for the file names.
|
|
*/
|
|
public class IndexMakerMap
|
|
{
|
|
public static void main(String[] args) throws IOException
|
|
{
|
|
Scanner keyboard = new Scanner(System.in);
|
|
System.out.print("\nEnter input file name: ");
|
|
String infileName = keyboard.nextLine().trim();
|
|
Scanner inputFile = new Scanner(new File(infileName));
|
|
|
|
DocumentIndex index = makeIndex(inputFile);
|
|
|
|
//System.out.println( index.toString() );
|
|
PrintWriter outputFile = new PrintWriter(new FileWriter("fishIndex.txt"));
|
|
outputFile.println(index.toString());
|
|
inputFile.close();
|
|
outputFile.close();
|
|
System.out.println("Done.");
|
|
}
|
|
|
|
public static DocumentIndex makeIndex(Scanner inputFile)
|
|
{
|
|
DocumentIndex index = new DocumentIndex();
|
|
int lineNum = 0;
|
|
while(inputFile.hasNextLine())
|
|
{
|
|
lineNum++;
|
|
index.addAllWords(inputFile.nextLine(), lineNum);
|
|
}
|
|
return index;
|
|
}
|
|
}
|
|
|
|
class DocumentIndex extends TreeMap<String, TreeSet<Integer>>
|
|
{
|
|
|
|
/** Extracts all the words from str, skipping punctuation and whitespace
|
|
* and for each word calls addWord(word, num). A good way to skip punctuation
|
|
* and whitespace is to use String's split method, e.g., split("[., \"!?]")
|
|
*/
|
|
public void addAllWords(String str, int lineNum)
|
|
{
|
|
String[] words = str.split("[., \"!?]");
|
|
for (String word : words)
|
|
if (!word.equals(""))
|
|
addWord(word, lineNum);
|
|
}
|
|
|
|
/** Makes the word uppercase. If the word is already in the map, updates the lineNum.
|
|
* Otherwise, adds word and ArrayList to the map, and updates the lineNum
|
|
*/
|
|
public void addWord(String word, int lineNum)
|
|
{
|
|
word = word.toUpperCase();
|
|
if (containsKey(word)) {
|
|
TreeSet<Integer> temp = get(word);
|
|
temp.add(lineNum);
|
|
put (word, temp);
|
|
} else {
|
|
TreeSet<Integer> temp = new TreeSet<>();
|
|
temp.add(lineNum);
|
|
put (word, temp);
|
|
}
|
|
|
|
|
|
}
|
|
|
|
public String toString()
|
|
{
|
|
String out = "";
|
|
|
|
for (String word : keySet()) {
|
|
out += word + " ";
|
|
String lineNums = get(word).toString();
|
|
out += lineNums.substring(1, lineNums.length() - 1) + "\n";
|
|
}
|
|
|
|
return out.substring(0, out.length() - 1);
|
|
}
|
|
}
|
|
|
|
/**********************************************
|
|
----jGRASP exec: java -ea IndexMakerMap
|
|
|
|
Enter input file name: fish.txt
|
|
Done.
|
|
|
|
----jGRASP: operation complete.
|
|
|
|
************************************************/
|
|
/****************** fishIndex.txt **************
|
|
A 12, 14, 15
|
|
ARE 16
|
|
BLACK 6
|
|
BLUE 4, 7
|
|
CAR 14
|
|
FISH 1, 2, 3, 4, 6, 7, 8, 9, 16
|
|
HAS 11, 14
|
|
LITTLE 12, 14
|
|
LOT 15
|
|
NEW 9
|
|
OF 16
|
|
OLD 8
|
|
ONE 1, 11, 14
|
|
RED 3
|
|
SAY 15
|
|
STAR 12
|
|
THERE 16
|
|
THIS 11, 14
|
|
TWO 2
|
|
WHAT 15
|
|
************************/
|