Skip to content
This repository was archived by the owner on Oct 26, 2026. It is now read-only.

Commit bac730b

Browse files
authored
Merge pull request #1 from Pyrokines17/lab1
base of lab1
2 parents 40bc075 + ab7c031 commit bac730b

5 files changed

Lines changed: 155 additions & 0 deletions

File tree

‎lab1/.gitignore‎

Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
target/
2+
!.mvn/wrapper/maven-wrapper.jar
3+
!**/src/main/**/target/
4+
!**/src/test/**/target/
5+
6+
### IntelliJ IDEA ###
7+
.idea/modules.xml
8+
.idea/jarRepositories.xml
9+
.idea/compiler.xml
10+
.idea/libraries/
11+
*.iws
12+
*.iml
13+
*.ipr
14+
15+
### Eclipse ###
16+
.apt_generated
17+
.classpath
18+
.factorypath
19+
.project
20+
.settings
21+
.springBeans
22+
.sts4-cache
23+
24+
### NetBeans ###
25+
/nbproject/private/
26+
/nbbuild/
27+
/dist/
28+
/nbdist/
29+
/.nb-gradle/
30+
build/
31+
!**/src/main/**/build/
32+
!**/src/test/**/build/
33+
34+
### VS Code ###
35+
.vscode/
36+
37+
### Mac OS ###
38+
.DS_Store

‎lab1/pom.xml‎

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
<?xml version="1.0" encoding="UTF-8"?>
2+
<project xmlns="http://maven.apache.org/POM/4.0.0"
3+
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
4+
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
5+
<modelVersion>4.0.0</modelVersion>
6+
7+
<groupId>ru.nsu.gunko</groupId>
8+
<artifactId>lab1</artifactId>
9+
<version>1.0-SNAPSHOT</version>
10+
11+
<properties>
12+
<maven.compiler.source>17</maven.compiler.source>
13+
<maven.compiler.target>17</maven.compiler.target>
14+
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
15+
</properties>
16+
17+
</project>
Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,46 @@
1+
package ru.nsu.gunko;
2+
3+
import java.io.*;
4+
import java.util.*;
5+
6+
public class Main {
7+
public static void main(String[] args) {
8+
Map<String, Integer> quantity = new HashMap<>();
9+
int countOfWords = 0;
10+
11+
try (BufferedReader reader = new BufferedReader(new FileReader(args[0]))) {
12+
TextAnalyzer analyzer = new TextAnalyzer(reader);
13+
14+
while (!analyzer.checkEnd()) {
15+
StringBuilder word = analyzer.getWord();
16+
String keyWord = word.toString();
17+
18+
if (keyWord.isEmpty()) {
19+
continue;
20+
}
21+
22+
if (quantity.containsKey(keyWord)) {
23+
quantity.replace(keyWord, quantity.get(keyWord) + 1);
24+
} else {
25+
quantity.put(keyWord, 1);
26+
}
27+
28+
++countOfWords;
29+
}
30+
} catch (IOException e) {
31+
System.err.println("Error while reading file: " + e.getLocalizedMessage());
32+
}
33+
34+
List<Map.Entry<String, Integer>> list = Sorter.sortByValue(quantity);
35+
36+
try (BufferedWriter writer = new BufferedWriter(new FileWriter("result.csv"))) {
37+
for (Map.Entry<String, Integer> word : list) {
38+
double percent = (double)word.getValue() / countOfWords * 100;
39+
String result = word.getKey() + ";" + word.getValue() + ";" + percent + "\n";
40+
writer.write(result);
41+
}
42+
} catch (IOException e) {
43+
System.err.println("Error while writing file: " + e.getLocalizedMessage());
44+
}
45+
}
46+
}
Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
package ru.nsu.gunko;
2+
3+
import java.util.*;
4+
5+
public class Sorter {
6+
public static List<Map.Entry<String, Integer>> sortByValue(Map<String, Integer> map) {
7+
List<Map.Entry<String, Integer>> list = new ArrayList<>(map.entrySet());
8+
list.sort(Map.Entry.comparingByValue(Comparator.reverseOrder()));
9+
return list;
10+
}
11+
}
Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
1+
package ru.nsu.gunko;
2+
3+
import java.io.*;
4+
5+
public class TextAnalyzer {
6+
private final StringBuilder string;
7+
private final Reader reader;
8+
private boolean end;
9+
10+
TextAnalyzer(Reader newReader) {
11+
string = new StringBuilder();
12+
reader = newReader;
13+
end = false;
14+
}
15+
16+
private void nextWord() throws IOException {
17+
int sym = reader.read();
18+
19+
while (sym != -1 && !Character.isLetterOrDigit(sym)) {
20+
sym = reader.read();
21+
}
22+
23+
while (sym != -1 && Character.isLetterOrDigit(sym)) {
24+
String charBuffer = Character.toString(sym);
25+
string.append(charBuffer);
26+
sym = reader.read();
27+
}
28+
29+
if (sym == -1) {
30+
end = true;
31+
}
32+
}
33+
34+
public StringBuilder getWord() throws IOException {
35+
string.setLength(0);
36+
this.nextWord();
37+
return string;
38+
}
39+
40+
public boolean checkEnd() {
41+
return end;
42+
}
43+
}

0 commit comments

Comments
 (0)