-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathWordUtils.java
More file actions
103 lines (82 loc) · 2.99 KB
/
Copy pathWordUtils.java
File metadata and controls
103 lines (82 loc) · 2.99 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
import java.io.*;
import java.util.ArrayList;
import java.util.Collection;
import java.util.Random;
public class WordUtils {
public static int minWordLength = 6;
public static String srcFileName = "words_orig.txt";
public static String targetFileName = "words.txt";
private static final ArrayList<String> wordList = new ArrayList<>();
public static final String alphabet = "абвгдеёжзийклмнопрстуфхцчшщъыьэюя";
// функция для удаления из исходного словаря слов короче minWordLength,
// а также содержащих пробел и/или дефис
public static void removeShortWords(){
try(BufferedWriter writer = new BufferedWriter(new FileWriter(targetFileName));
BufferedReader reader = new BufferedReader(new FileReader(srcFileName)))
{
String line;
while ((line = reader.readLine()) != null){
if (line.length() >= minWordLength && !line.contains(" ") && !line.contains("-")){
writer.write(line + "\n");
}
}
}
catch (IOException e){
e.printStackTrace();
}
}
public static boolean isValidSymbol(String symbol) {
if (symbol == null) {
return false;
} else if (symbol.length() > 1) {
return false;
} else return alphabet.contains(symbol);
}
public static String getRandomWord(){
if (!wordList.isEmpty()){
// Random rnd = new Random();
int randomIdx = (new Random()).nextInt(wordList.size());
return wordList.get(randomIdx);
}
try(BufferedReader reader = new BufferedReader(new FileReader(targetFileName)))
{
String line;
while ((line = reader.readLine()) != null){
wordList.add(line);
}
}
catch (IOException e){
e.printStackTrace();
}
if (!wordList.isEmpty()) {
int randomIdx = new Random().nextInt(wordList.size());
return wordList.get(randomIdx);
}
else {
return null;
}
}
public static boolean listContainsAllLettersOfString(String s, Collection<Character> list){
if (s.isEmpty()) {
System.out.println("String is empty");
return false;}
for(char chr: s.toCharArray()) {
if (!list.contains(chr)) {
return false;
}
}
return true;
}
public static String getPartOfWord(String word, Collection<Character> shownLetters){
StringBuilder resultString = new StringBuilder();
char[] wordArr = word.toCharArray();
for (char c : wordArr) {
if (shownLetters.contains(c)) {
resultString.append(c);
} else {
resultString.append("*");
}
}
return resultString.toString();
}
}