-
Notifications
You must be signed in to change notification settings - Fork 5
/
StatsCalc.java
145 lines (127 loc) · 4.22 KB
/
StatsCalc.java
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
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
/*
Twidor: the twiddler typing tutor.
Copyright (C) 2005 James Fusia
This program is free software; you can redistribute it and/or
modify it under the terms of the GNU General Public License
as published by the Free Software Foundation; either version 2
of the License, or (at your option) any later version.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with this program; if not, write to the Free Software
Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301,
USA.
*/
/**
* CCG: Twidor- The Twiddler Tutor!
* <pre>
* Stats.java, what I expect to be the most heavily modified file here.
* This file is meant to hold the stats for a user/session/whatever. For
* however long. And automatically do the calculations. Etc.
*
* Revisions:
* 0.5 17 July 2003
* Completed Tutor
* 0.1 22 May 2003
* Created class Stats
* </pre>
* @author <a href="mailto:[email protected]">James Fusia</a>
* @version Version 0.5; 17 July 2003
*/
import java.io.*;
import java.util.Vector;
import java.lang.Math;
public class StatsCalc implements TwidorConstants {
public static int[] statsMSD (String sentence, String typed) {
int i, j;
String a = " " + sentence;
String b = " " + typed;
int[][] msd = new int[a.length()][b.length()];
int[][] mfa = new int[a.length()][b.length()];
int min, sub, notInA;
/* initialization */
for (i = 0; i < a.length(); i++) {
msd[i][0] = i;
mfa[i][0] = 0;
}
for (j = 0; j < b.length(); j++) {
msd[0][j] = j;
mfa[0][j] = j;
}
/* calculation */
for (i = 1; i < a.length(); i++) {
for (j = 1; j < b.length(); j++) {
min = msd[i - 1][j] + 1;
notInA = msd[i][j - 1] + 1;
sub = msd[i - 1][j - 1];
if (a.charAt(i) != b.charAt(j)) {
sub++;
}
mfa[i][j] = mfa[i - 1][j];
if (sub < min) {
min = sub;
mfa[i][j] = mfa[i - 1][j - 1];
}
if (notInA < min) {
min = notInA;
mfa[i][j] = mfa[i][j - 1] + 1;
}
msd[i][j] = min;
}
}
/* return value */
int[] ret = new int[2];
ret[0] = msd[a.length() - 1][b.length() - 1];
ret[1] = mfa[a.length() - 1][b.length() - 1];
return ret;
}// end statsMSD (String,String)
public static int statsF (Vector keyPresses) {
int ret = 0;
KeyElement temp;
for (int i = 0; i < keyPresses.size(); i++) {
temp = ((StatsElement)keyPresses.elementAt(i)).getTyped();
if (temp.getNumber() == KEY_BACKSPACE || temp.getNumber() == KEY_DELETE) {
ret++;
}
}
return ret;
}// end statsF (Vector)
public static int statsIF (String sentence, String typed, Vector keyPresses) {
//return (keyPresses.size() - typed.length()) - statsF(keyPresses);
return statsF(keyPresses) - (typed.length() - sentence.length());
}// end statsIF (String,String,Vector)
public static int statsC (String sentence, String typed) {
int[] msd = statsMSD(sentence, typed);
int length = Math.min(typed.length(), sentence.length());
return length - (msd[0] - msd[1]);
}// end statsC (String,String)
public static int statsINF (String sentence, String typed) {
return statsMSD(sentence, typed)[0];
}// end statsINF (String,String)
/**
* return value is {C, F, IF, INF}
*/
public static int[] statsALL (String sentence, String typed, Vector keyPresses) {
int[] ret = new int[4];
int[] msd = statsMSD(sentence, typed);
ret[C] = Math.min(typed.length(), sentence.length()) - (msd[0] - msd[1]);
ret[F] = statsF(keyPresses);
ret[IF] = ret[F] - (typed.length() - sentence.length());
ret[INF] = msd[0];
return ret;
}
public static void main (String[] argv) {
int c, inf, f, ifix;
String sentence = "thank you for your help";
String typed = "shank you for your help";
Vector keyPresses = new Vector();
c = statsC(sentence, typed);
inf = statsINF(sentence, typed);
f = statsF(keyPresses);
ifix = statsIF(sentence, typed, keyPresses);
System.out.println(c + " " + inf + " " + f + " " + ifix);
System.out.println(((double)(inf + ifix)) / ((double)(c + inf + ifix)));
}// end main (String)
}// end class StatsCalc