Package org.example

Class WordStatic

java.lang.Object
org.example.WordStatic

public class WordStatic extends Object
A utility class for performing various text analysis operations, such as counting lines, words, and word frequencies. This class provides static methods for reading text files, calculating line and word counts, and calculating word frequencies. It also includes a method for sorting word frequencies by value in descending order.

The class uses a regular expression pattern to match words in a given text. The pattern matches one or more word characters, hyphens, or apostrophes. Word characters include uppercase and lowercase letters, digits, and underscores.

The class also provides a method for running a test with a regular expression and a given text. This method counts the number of matches of the regular expression in the text.

The default file path for the Peach Story text file is specified in the FILE_PATH constant. This constant can be used when reading or writing data from/to the file.

The class is thread-safe and can be used in a multi-threaded environment.

  • Field Details

    • FILE_PATH

      public static final String FILE_PATH
      The file path for the Peach Story text file. This constant specifies the location of the "peachStory.txt" file within the project's resources directory. Use this path when reading or writing data from/to the file.
      See Also:
    • MATCH_ALL_WORDS_PATTERN

      public static final Pattern MATCH_ALL_WORDS_PATTERN
      A regular expression pattern that matches all words in a given text.

      The pattern matches one or more word characters, hyphens, or apostrophes. Word characters include uppercase and lowercase letters, digits, and underscores.

      This pattern is used to extract words from a text for further analysis, such as counting the number of words or calculating word frequencies.

  • Method Details

    • readAllLinesAndReturn

      public static List<String> readAllLinesAndReturn()
      Reads all lines from the default file and returns them as a list of strings. This is a convenience method that calls the readAllLinesAndReturn(String filePath) method with the default file path defined in the FILE_PATH constant.
      Returns:
      A list of strings, where each string is a line from the default file. If an error occurs while reading the file, an empty list is returned.
    • readAllLinesAndReturn

      public static List<String> readAllLinesAndReturn(String filePath)
      Reads all lines from a file and returns them as a list of strings.
      Parameters:
      filePath - The path to the file to be read.
      Returns:
      A list of strings, where each string is a line from the file. If an error occurs while reading the file, an empty list is returned.
    • numberOfLines

      public static int numberOfLines(List<String> lines)
      Calculates the number of lines in a list of strings.
      Parameters:
      lines - A list of strings, where each string represents a line.
      Returns:
      The number of lines in the list.
    • numberOfWords

      public static int numberOfWords(List<String> lines)
      Calculates the number of words in a list of strings.
      Parameters:
      lines - A list of strings, where each string represents a line.
      Returns:
      The total number of words in all lines.
    • runTest

      public static int runTest(String regex, String text)
      Counts the number of matches of a regular expression in a given text.
      Parameters:
      regex - The regular expression to be matched.
      text - The text in which to search for matches.
      Returns:
      The number of matches found in the text.
    • wordFrequencies

      public static Map<String,Long> wordFrequencies(List<String> lines)
      Calculates the frequency of each word in a list of strings.
      Parameters:
      lines - A list of strings, where each string represents a line.
      Returns:
      A map where the keys are the words and the values are their frequencies.
    • sortByValue

      public static Map<String,Long> sortByValue(Map<String,Long> wordFrequencies)
      Sorts a map of word frequencies by value in descending order.
      Parameters:
      wordFrequencies - A map where the keys are the words and the values are their frequencies.
      Returns:
      A new map with the same entries as the input map, but sorted by value in descending order.