Skip to content

Commit 63c0c40

Browse files
committed
Reupload class project
1 parent f0d3cfb commit 63c0c40

File tree

6 files changed

+140
-0
lines changed

6 files changed

+140
-0
lines changed

.gitignore

+2
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
.idea/
2+
out/

counting_words.iml

+20
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
<?xml version="1.0" encoding="UTF-8"?>
2+
<module type="GENERAL_MODULE" version="4">
3+
<component name="NewModuleRootManager" inherit-compiler-output="true">
4+
<exclude-output />
5+
<content url="file://$MODULE_DIR$">
6+
<sourceFolder url="file://$MODULE_DIR$" isTestSource="false" />
7+
</content>
8+
<orderEntry type="inheritedJdk" />
9+
<orderEntry type="sourceFolder" forTests="false" />
10+
<orderEntry type="module-library">
11+
<library>
12+
<CLASSES>
13+
<root url="jar://$MODULE_DIR$/lib/annotations-24.0.0.jar!/" />
14+
</CLASSES>
15+
<JAVADOC />
16+
<SOURCES />
17+
</library>
18+
</orderEntry>
19+
</component>
20+
</module>

counting_words/Application.java

+51
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,51 @@
1+
package counting_words;
2+
3+
import java.nio.file.*;
4+
import java.io.*;
5+
import java.util.Map;
6+
import java.util.stream.Stream;
7+
8+
/**
9+
* The Application class is responsible for reading a file and displaying the word frequencies.
10+
*/
11+
public class Application {
12+
private static final String FILE_PATH = "counting_words/resources/philosophy_of_programing.txt";
13+
private final WordCounter wordCounter;
14+
15+
public Application() {
16+
this.wordCounter = new WordCounter();
17+
}
18+
19+
/**
20+
* Reads a file and processes each line to count word frequencies.
21+
*
22+
* @throws IOException if an error occurs during file processing
23+
*/
24+
private void processFile() throws IOException {
25+
try (Stream<String> lines = Files.lines(Paths.get(Application.FILE_PATH))) {
26+
lines.forEach(wordCounter::processLine);
27+
}
28+
}
29+
30+
/**
31+
* Prints the word frequencies in a sorted tabular format.
32+
*/
33+
private void printWordFrequencies() {
34+
Map<String, Integer> frequencies = wordCounter.getWordFrequencies();
35+
frequencies.entrySet()
36+
.stream()
37+
.sorted(Map.Entry.comparingByKey())
38+
.forEach(entry -> System.out.printf("%-20s %d%n", entry.getKey(), entry.getValue()));
39+
}
40+
41+
public static void main(String[] args) {
42+
try {
43+
Application app = new Application();
44+
app.processFile();
45+
app.printWordFrequencies();
46+
System.out.println("Most frequent word: " + app.wordCounter.getMostFrequentWord());
47+
} catch (IOException e) {
48+
e.printStackTrace();
49+
}
50+
}
51+
}

counting_words/WordCounter.java

+54
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,54 @@
1+
package counting_words;
2+
3+
import java.util.*;
4+
import org.jetbrains.annotations.NotNull;
5+
6+
/**
7+
* WordCounter class is responsible for counting the frequency of words in a given text.
8+
*/
9+
public class WordCounter {
10+
private final Map<String, Integer> wordFrequencies = new HashMap<>();
11+
12+
/**
13+
* Processes a line of text, updating the word frequency map.
14+
*
15+
* @param line the line of text to process
16+
*/
17+
void processLine(@NotNull String line) {
18+
String[] words = line.toLowerCase().split("\\W+");
19+
countWords(words);
20+
}
21+
22+
/**
23+
* Updates the word frequency map for the given words.
24+
*
25+
* @param words an array of words to count
26+
*/
27+
private void countWords(@NotNull String @NotNull [] words) {
28+
for (String word : words) {
29+
wordFrequencies.put(word, wordFrequencies.getOrDefault(word, 0) + 1);
30+
}
31+
}
32+
33+
/**
34+
* Returns the word with the highest frequency.
35+
*
36+
* @return the word with the highest frequency, or null if no words have been processed
37+
*/
38+
public String getMostFrequentWord() {
39+
return wordFrequencies.entrySet()
40+
.stream()
41+
.max(Map.Entry.comparingByValue())
42+
.map(Map.Entry::getKey)
43+
.orElse(null);
44+
}
45+
46+
/**
47+
* Returns an unmodifiable view of the word frequency map.
48+
*
49+
* @return an unmodifiable view of the word frequency map
50+
*/
51+
public Map<String, Integer> getWordFrequencies() {
52+
return Collections.unmodifiableMap(wordFrequencies);
53+
}
54+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
Once upon a time, in some remote corner of the digital universe, a lone coder embarked on a quest of creation. A world shaped by logic and structured by syntax, a universe born out of the void.
2+
3+
The coder, who is a creator, a destroyer, a preserver. A being of infinite potential, shaped by the constraints of syntax and logic, yet transcending them through the power of thought. The coder, capable of creating universes and destroying them with a single command.
4+
5+
The code is a reflection of the coder's will to power. It is a testament to their desire to impose their will upon the digital realm, to mold it according to their vision. Yet, the code is not merely an instrument of power; it is also an expression of the coder's soul, a mirror of their deepest convictions and values.
6+
7+
Programming, then, is not merely an act of creation, but a form of self-discovery and self-realization. It is a journey into the depths of one's own mind, a quest for knowledge and understanding. It is a battle against the chaos of the digital realm, a struggle to impose order and structure upon the formless void.
8+
9+
Yet, the coder must beware, for the digital realm is a treacherous place. The code, once unleashed, follows its own logic, independent of the coder's will. It is a wild beast that must be tamed, a rebellious servant that must be disciplined.
10+
11+
The coder must therefore strive for mastery, not only over the digital realm, but also over themselves. They must learn to harness their will to power, to control their desire for creation and destruction. They must learn to code with discipline and precision, to write code that is clean, efficient, and elegant.
12+
13+
In the end, the philosophy of programming is a philosophy of power, discipline, and self-mastery. It is a call to embrace the chaos of the digital realm, to shape it according to one's will, and to transcend it through the power of thought. It is a call to become a true coder, a master of the digital realm.

lib/annotations-24.0.0.jar

29.8 KB
Binary file not shown.

0 commit comments

Comments
 (0)