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

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;
        }
    }
}

Overview The purpose of this assignment is to serve as an introduction sequences, sets, and relations and their realizations in Java through the List and Map interfaces. Background Consider the following problem: given a text, for every word in the text, count both the number of times that the word occurs, and also the line numbers on which the word occurs in the text. Such a program is a simplified concordance, and has a broad range of application in numerous fields such as legal or theological analysis. Specification For this assignment you are to define a program Concordance, which for every word in the given text file(s) outputs the word, the number of times the word appears in the text, and the set of unique line numbers on which the word appears (in order). Words, are to be understood as consecutive sequences of alphanumeric characters; lines are defined by the presence of the newline, In',character and are enumerated starting with 1. Equality for words is defined by the member function equalslgnoreCase of the String class File Format For this assignment you may assume the following file format: 1. Files will be saved as plain text. 2. If a file is not empty, then it will be readable and not contain any data that will cause an IOException to be thrown while reading the file 3. All data in a file may be read by the read) method associated with a BufferedReader. 4. Every file given as input to the program exists, and is readable. 5. Files do not contain any punctuation symbols

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 :)

Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
Chat Now And Get Quote