Here is my program called wordlist that goes with the other program called Conco
ID: 3859221 • Letter: H
Question
Here is my program called wordlist that goes with the other program called Concordance(Concordance is the program i need help with)
//Program WordList
import java.io.BufferedReader;
import java.io.FileReader;
import java.io.IOException;
import java.util.Arrays;
import java.util.SortedSet;
import java.util.TreeSet;
/**
* The {@code WordList} class presents a simple commandline program which outputs
* all of the unique words in the given file(s) in alphabetical order. This program
* makes a simplifying assumption that words are delimited by any symbol matching
* the regular expression {@code "\W+"}.
*
*/
public class WordList {
private static final String DELIMITER = "\W+"; //special kinda of string (one or more repetitions of the set is the +
// \W means non word characters/ so im going to separate my words
private static SortedSet<String> wordsInFile(String fileName) throws IOException {
SortedSet<String> results = new TreeSet<>(); //takes a sorted string and sets it
try (BufferedReader input = new BufferedReader(new FileReader(fileName))) { //creating buffered reader
String line;
while ((line = input.readLine()) != null) { //while reading a line, going to split according to delimiter
String[] words = line.split(DELIMITER);
for (String word : words) {
if (!word.isEmpty()) {
results.add(word.toLowerCase()); //would add stuff here for actual program
}
}
}
}
return results; //sorted set
}
public static void main(String[] args) {
if (args.length != 0) {
for (String fileName : args) {
try {
SortedSet<String> words = wordsInFile(fileName); //Sorting set
System.out.printf("file: %s words: %s ", fileName, words ); //Output the string
} catch(IOException error) {
System.err.printf("%s: unable to process file ", fileName);
}
}
}
}
}
Concordance Program(The program i need help finishing up in the if loop with the comments talking about it)
import java.io.BufferedReader;
import java.io.FileReader;
import java.io.IOException;
import java.util.Map;
import java.util.SortedSet;
import java.util.TreeMap;
import java.util.TreeSet;
public class Concordance {
static class Entry {
private int numberOfOccurrences;
private SortedSet<Integer> lineNumbers;
private static final String DELIMITER = "\W+";
public Entry(int line) {
setNumberOfOccurrences(1);
setLineNumbers(new TreeSet<>());
updateLineNumbers(line);
}
public void incrementNumberOfOccurrences() {
this.numberOfOccurrences++;
}
public void updateLineNumbers(int line) {
this.lineNumbers.add(line);
}
public void setNumberOfOccurrences(int x) {
this.numberOfOccurrences = x;
}
public void setLineNumbers(SortedSet<Integer> lines) {
this.lineNumbers = lines;
}
public int getNumberOfOccurrences() {
return numberOfOccurrences;
}
public SortedSet<Integer> getLineNumbers() {
return lineNumbers;
}
public String toString() {
return String.format("number: %d lines: %s", getNumberOfOccurrences(), getLineNumbers());
}
private static Map<String, Entry> generateConcordanceForFile(String fileName) throws IOException {
Map<String, Entry> results = new TreeMap<>();
try (BufferedReader input = new BufferedReader(new FileReader(fileName))) { //
String line;
int linenumber = 1;
while ((line = input.readLine()) != null) { //
String[] words = line.split(DELIMITER);
for (String word : words) {
if (!word.isEmpty()) {
// Here is where the implementation would go to finish off the program, so making sure once our file is read, we print out the first word from the text file, then the number of times it appears and then the line coordinate that it is on (so like in the example of the assignment)
//
//
//
//
//
//
//
}
}
}
}
return results;
}
}
}
Explanation / Answer
import java.io.BufferedReader;
import java.io.FileReader;
import java.io.IOException;
import java.util.LinkedHashMap;
import java.util.Map;
import java.util.Set;
import java.util.SortedSet;
import java.util.TreeMap;
import java.util.TreeSet;
public class Concordance {
static class Entry {
private int numberOfOccurrences;
private SortedSet<Integer> lineNumbers;
private static final String DELIMITER = "\W+";
public Entry(int line) {
setNumberOfOccurrences(1);
setLineNumbers(new TreeSet<>());
updateLineNumbers(line);
}
public void incrementNumberOfOccurrences() {
this.numberOfOccurrences++;
}
public void updateLineNumbers(int line) {
this.lineNumbers.add(line);
}
public void setNumberOfOccurrences(int x) {
this.numberOfOccurrences = x;
}
public void setLineNumbers(SortedSet<Integer> lines) {
this.lineNumbers = lines;
}
public int getNumberOfOccurrences() {
return numberOfOccurrences;
}
public SortedSet<Integer> getLineNumbers() {
return lineNumbers;
}
public String toString() {
return String.format("number: %d lines: %s", getNumberOfOccurrences(), getLineNumbers());
}
private static Map<String, Entry> generateConcordanceForFile(String fileName) throws IOException {
Map<String, Entry> results = new TreeMap<>(); // will sort in terms of String
// LinkedHashMap<String, Entry> results = new LinkedHashMap<>(); //linkedHashSet maintains insertion order
try (BufferedReader input = new BufferedReader(new FileReader(fileName))) { //
String line;
int linenumber = 1;
while ((line = input.readLine()) != null) { //
String[] words = line.split(DELIMITER);
for (String word : words) {
if (!word.isEmpty()) {
word = word.toLowerCase();
// Here is where the implementation would go to finish off the program, so making sure once our file is read, we print out the first word from the text file, then the number of times it appears and then the line coordinate that it is on (so like in the example of the assignment)
if (results.containsKey(word)) {
results.get(word).incrementNumberOfOccurrences();
results.get(word).updateLineNumbers(linenumber);
}
else {
Entry newEntry = new Entry(linenumber);
results.put(word, newEntry);
}
}
}
}
/*
Set<String> keys = results.keySet();
for (String s:keys){
System.out.println("word: " + s);
System.out.println("number: " + results.get(s).getNumberOfOccurrences());
System.out.println("lines: " + results.get(s).getLineNumbers().toString());
}
*/
}
return results;
}
}
}
Please let me know if you like the answer :)
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.