Class WordStatic
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 Summary
-
Method Summary
Modifier and TypeMethodDescriptionstatic int
numberOfLines
(List<String> lines) Calculates the number of lines in a list of strings.static int
numberOfWords
(List<String> lines) Calculates the number of words in a list of strings.Reads all lines from the default file and returns them as a list of strings.readAllLinesAndReturn
(String filePath) Reads all lines from a file and returns them as a list of strings.static int
Counts the number of matches of a regular expression in a given text.sortByValue
(Map<String, Long> wordFrequencies) Sorts a map of word frequencies by value in descending order.wordFrequencies
(List<String> lines) Calculates the frequency of each word in a list of strings.
-
Field Details
-
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
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
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
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
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
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
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
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
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.
-