Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

Develop a concordance generator program. The concordance will be a list of all t

ID: 3772288 • Letter: D

Question

Develop a concordance generator program. The concordance will be a list of all the words found in a text file with a count of the number of times the word occurred and a comma delimited list of the line numbers of where the word occurred. For each line of text, all punctuation must be removed and all letters in the string forced to lowercase. Then, separate out the words and check each word against your list.   Keep track of the current line number and check if the word has been found before. If the word has not been seen before, put it in some sort of collection data structure (an array or vector for example) and keep track of the line number. If the word has been found before, store the line number of where the word occurred. When the text file has been scanned, print the words and line numbers in the following format:

word : count : linenumber,linenumber,linenumber

Hint: you may need to use several arrays or vectors to store the words and the line numbers.

I have posted this question three times. That way the person who answers it can get credit for three answers

Explanation / Answer

Complete Program:

// File: Concordance.java
import java.io.*;
import java.util.*;
public class Concordance
{
public static void main(String[] args) throws FileNotFoundException
{  
  final int MAX_WORDS = 500;
  Vector<String> words = new Vector<String>(MAX_WORDS);
  Vector<Vector<Integer>> lineNumbers = new Vector<Vector<Integer>>(MAX_WORDS);
  
  for(int i = 0; i < MAX_WORDS; i++)
   lineNumbers.add(new Vector<Integer>());
  
  int lineCount = 0;
  
  Scanner infile = new Scanner(new File("concordance.txt"));  
  while(infile.hasNextLine() && lineCount < MAX_WORDS)
  {
   lineCount++;
   String line = infile.nextLine();
   
   // replace each punctuation by a space
   String[] tokens = line.replaceAll("[^a-zA-Z ]", " ").toLowerCase().split("\s+");
   
   for(int i = 0; i < tokens.length; i++)
   {
    String term = tokens[i];
    
    if(!words.contains(term))
     words.add(term);
     
    int index = words.indexOf(term);    
    lineNumbers.get(index).add(lineCount);
   }   
  }
   
  sort(words, lineNumbers);
  print(words, lineNumbers);  
}

public static void sort(Vector<String> words, Vector<Vector<Integer>> lineNumbers)
{
  for(int i = 0; i < words.size() - 1; i++)
  {
   int minPos = i;

   for (int j = i + 1; j < words.size() ; j++)
   {
    if(((Comparable<String>)words.get(j)).compareTo(words.get(minPos)) < 0)
     minPos = j;
   }

   if(minPos != i)
   {
    String temp1 = words.get(i);
    words.set(i, words.get(minPos));
    words.set(minPos, temp1);
    
    Vector<Integer> temp2 = lineNumbers.get(i);
    lineNumbers.set(i, lineNumbers.get(minPos));
    lineNumbers.set(minPos, temp2);
   }
  }
}

public static void print(Vector<String> words, Vector<Vector<Integer>> lineNumbers)
{
  for(int i = 0; i < words.size(); i++)
  {
   System.out.printf("%-15s : ", words.elementAt(i));
   
   Vector<Integer> nums = lineNumbers.get(i);
   
   System.out.printf("%3d : ", nums.size());
   
   for(int j = 0; j < nums.size(); j++)
   {
    System.out.print(nums.get(j));
    
    if(j < nums.size() - 1)
     System.out.print(",");
   }
   System.out.println();
  }
}
}

Input file: concordance.txt

word's : count : li4nen@umber, line%number, lin!en$umber count
word's : count : li4nen@umber, line%number, lin!en$umber count


Sample Output: