-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathMSWord.java
More file actions
117 lines (95 loc) · 2.65 KB
/
MSWord.java
File metadata and controls
117 lines (95 loc) · 2.65 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
104
105
106
107
108
109
110
111
112
113
114
115
116
117
import java.util.*;
/**
* MSWord beschreibt ein Wort und stellt eine Methode zum
* Bewerten eines Wortes zur Verfuegung.
*
* @author Goetz und Dominik
* @version 9.11
*/
public class MSWord
{
private int rating;
private String word;
private Map table;
private int difficulty;
public MSWord(String word)
{
// Instanzvariable initialisieren
this.word = word;
this.table = this.create_table();
this.rating = this.rating_method(word);
this.difficulty = this.set_difficulty();
}
public int get_rating()
{
return this.rating;
}
public String get_word()
{
return this.word;
}
public int get_difficulty(){
return this.difficulty;
}
public int rating_method(String word)
{
int score = 0;
word = word.toUpperCase();
// L = Length
int L = word.length();
// Re = Repetitions
int Re = 0;
// Ra = Rarity
int Ra = 0;
for (int i = 0; i < L; i++)
{
//System.out.println(i);
char chr = word.charAt(i);
if (this.countLetter(word, chr) > 1){
Re += 1;
}
String tmp = word.substring(i,i+1);
Integer tmp_2 = (Integer)this.table.get(tmp);
Ra += tmp_2;
}
score = L + Ra + Re;
return score;
}
private static int countLetter(String str, char letter) {
str = str.toLowerCase();
letter = Character.toLowerCase(letter);
int count = 0;
for (int i = 0; i < str.length(); i++) {
char currentLetter = str.charAt(i);
if (currentLetter == letter){
count++;
}
}
return count;
}
public Map create_table(){
Map<String, Integer> table = new HashMap<String, Integer>();
String[] parts;
String chr;
Integer value;
In.open("ht.txt");
String tmp = In.readWord();
while(In.done()){
parts = tmp.split(";");
chr = parts[0];
value = Integer.valueOf(parts[1]);
table.put(chr, value);
// Out.println(chr + " : " + value);
tmp = In.readWord();
}
In.close();
return table;
}
public int set_difficulty(){
int diff = 0;
int score = this.get_rating();
// Diese Formel kann noch optimiert werden:
diff = 50 - (score / 100);
return diff;
}
}