-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathDictionary.java
More file actions
39 lines (30 loc) · 1.3 KB
/
Dictionary.java
File metadata and controls
39 lines (30 loc) · 1.3 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
package classwork22_08;
import java.util.HashMap;
import java.util.Map;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
public class Dictionary {
/*
Постройте частотный словарь букв русского (или английского) алфавита.
для решения можно использовать Array или HashMap ( на ваше усмотрение)..
Пример
text = "Привет, как дела?"
{'а': 2, 'в': 1, 'д': 1, 'е': 2, 'и': 1, 'к': 2, 'л': 1, 'п': 1, 'р': 1, 'т': 1, 'я': 1}
*/
public static void main(String[] args) {
String text = "The quick fox jumps over the lazy dog";
Map<Character, Integer> frequency = dictionary(text);
System.out.println(frequency);
}
public static Map<Character, Integer> dictionary(String text) {
text = text.toLowerCase();
Pattern pattern = Pattern.compile("[a-z]");
Matcher matcher = pattern.matcher(text);
Map<Character, Integer> frequencyDict = new HashMap<>();
while (matcher.find()) {
char letter = matcher.group().charAt(0);
frequencyDict.put(letter, frequencyDict.getOrDefault(letter, 0) + 1);
}
return frequencyDict;
}
}