This repository was archived by the owner on Dec 18, 2019. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathTerminBot.pde
More file actions
242 lines (190 loc) · 6.92 KB
/
TerminBot.pde
File metadata and controls
242 lines (190 loc) · 6.92 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
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
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
import java.io.File;
import java.util.List;
import java.util.regex.*;
import java.io.BufferedWriter;
import java.io.FileWriter;
PFont f;
// Variable to store text currently being typed
String eingabe = "";
String filePath = "C:/....[FILE PATH]";
// Variable to store saved text when return is hit
String saved = "";
int state = 0;
Headtracking head;
TextToSpeech speech;
Capture video;
OpenCV opencv;
void setup() {
size(800, 600);
f = createFont("Arial",16);
video = new Capture(this, 320/2, 240/2);
opencv = new OpenCV(this, 320/2, 240/2);
head = new Headtracking(video, opencv);
opencv.loadCascade(OpenCV.CASCADE_FRONTALFACE);
video.start();
speech = new TextToSpeech(new Minim(this));
}
void draw() {
// Terminbot-Fenster beim Start im Focus
frame.toFront();
frame.requestFocus();
background(255);
textFont(f);
fill(0, 88, 132);
switch (state) {
case 1:
if(key == '\n' && state == 1 ){
writeTermin();
}
break;
case 2:
if(key == '\n' && state == 2 ){
text ("Bot: Möchtest du den Termin wirklich löschen? ", 25, 100);
text ("[JA] - Kopf nacht rechts [NEIN] - Kopf nach links ", 25, 120);
if(head.isRight()){ // Wenn Kopf nach rechts --> Lösche eingegebenen Termin
deleteTermin();
text ("Bot: Der Termin wurde gelöscht ", 25, 150);
state = 0;}
else if(head.isLeft()){
state = 0;}
}
break;
case 3:
text ("Bot: Das sind all deine Termine: ", 25, 100);
break;
case 4:
text ("Bot: Das sind all deine Termine: ", 25, 100);
allDates();
break;
case 99:
text ("Bot: Das habe ich leider nicht verstanden. :(", 25, 100);
}
text("Bot: Wie kann ich dir helfen? ", 25, 40);
text ("Befehle: [anlegen] [ausgeben] [löschen]", 25, 64);
fill(0);
text("Du: " + eingabe,25,500);
head.track();
if (head.isNeutral()){ // Wenn Kopf mittig --> zeige grünen Punkt
fill(12, 127, 0);
circle(200, 10, 5);
}else{ // ansonsten zeige roten Punkt
fill(255, 0, 0);
circle(200, 10, 5);
}
fill(0, 88, 132);
}
void captureEvent(Capture c) {
c.read();
}
void keyPressed() {
// Wenn Enter gedrückt wird überprüfe den String
if (key == '\n' ) {
saved = eingabe;
if(eingabe.contains("anlegen")) {
state = 1;
} else if(eingabe.contains("löschen")) {
state = 2;
//head.resetHead();
} else if(eingabe.contains("ausgeben")) {
state = 3;
readTermin();
}else if (eingabe.contains("alle termine") || eingabe.contains("alle Termine") || eingabe.contains("Alle Termine")) {
state = 4;
} else if(eingabe.contains("beenden") || eingabe.contains("exit") || eingabe.contains("schließen")) {
exit();
return;
} else{ //<>//
state = 99;
}
// String leeren
eingabe = "";
}
else if(key == BACKSPACE) {
if (eingabe.length() > 0) {
eingabe = eingabe.substring(0, eingabe.length() - 1);
}
}
else if(key == TAB){
eingabe = eingabe + " ";
}
else if(key != CODED) {
// Ansonsten erweitere den String
eingabe = eingabe + key;
}
}
static Iterable<MatchResult> findMatches( String pattern, CharSequence s )
{
List<MatchResult> results = new ArrayList<MatchResult>();
for ( Matcher m = Pattern.compile(pattern).matcher(s); m.find(); )
results.add( m.toMatchResult() );
return results;
}
/***** TERMINE ANLEGEN *****/
void writeTermin(){
try {
// Wort beginnend mit großem Buchstaben
String aktion = "[A-ZÄÖÜ][a-zäöü]*";
MatchResult matchAction = findMatches( aktion, saved ).iterator().next();
//Tag Monat Jahr durch Punkte getrennt
String datum = "[0-3]?[0-9]\\.[0-1]?[0-9]\\.[1-2][0-9][0-9][0-9]";
MatchResult matchDate = findMatches( datum, saved ).iterator().next();
// mindestens ein Zahl, endet mit Uhr -> z.B 9 Uhr oder 09 Uhr oder 09:00 Uhr...
String uhrzeit = "[0-2]?[0-9]\\:?[0-9]?[0-9]? Uhr";
MatchResult matchTime = findMatches( uhrzeit, saved ).iterator().next();
String terminDate = matchDate.group();
String terminTime = matchTime.group();
String terminAction = matchAction.group();
saved = terminAction + " am " + terminDate + " um " + terminTime;
final String archive = dataPath(terminDate + ".txt");
String[] list = split(saved, ',');
saveStrings(archive, list);
println("\n\nDatei gespeichert!\n");
text ("Bot: Ich habe den Termin >> " + terminAction + " am " + terminDate + " um " + terminTime + " << gespeichert.", 25, 120);
} catch (Exception error_write) {
System.out.println("error_write");
text("Bot: Keinen Termin in der Eingabe gefunden. ", 25, 100);
}
}
/***** TERMINE LÖSCHEN *****/
void deleteTermin(){
try{
String datum = "[0-3][0-9]\\.[0-1][0-9]\\.[1-2][0-9][0-9][0-9]";
MatchResult matchDate = findMatches( datum, saved ).iterator().next();
String terminDate = matchDate.group();
String textDatei = dataPath(terminDate + ".txt");
File del = new File(textDatei);
del.delete();
text ("Bot: Ich habe alle Termine am " + terminDate + " gelöscht. ", 25, 100);
System.out.println("Bot: Ich habe alle Termine am " + terminDate + " gelöscht. ");
} catch (Exception error_del){
System.out.println("error_del");
text ("Bot: Es existieren keine Termine, die gelöscht werden können.", 25, 120);
}
}
/***** TERMINE AUSGEBEN *****/
void readTermin(){
try {
String datum = "[0-3]?[0-9]\\.[0-1]?[0-9]\\.[1-2][0-9][0-9][0-9]";
MatchResult matchDate = findMatches( datum, saved ).iterator().next();
String terminDate = matchDate.group();
String ausgabe[] = loadStrings(terminDate + ".txt");
for (int i = 0 ; i < ausgabe.length; i++) {
text((i+1) + ". " + ausgabe[i], 25, 140+(i*20));
speech.playString(ausgabe[i]);
delay(6000); // wartet nach jeder Ausgabe 6 Sekunden damit .mp3 komplett abgespielt wird
}
}catch (Exception error_read) {
text("Bot: Keinen Termin gefunden. ", 25, 100);
System.out.println("error_read");
}
}
/***** ALLE TERMIN-DATEIEN AUS VERZEICHNIS AUSGEBEN *****/
void allDates(){
File[] files = listFiles(filePath);
for (int i = 0; i < files.length; i++) {
File f = files[i];
if(f.getName() != "sounds"){
text( i+1 + ". Termine: " + f.getName(), 25, 140+(i*20));
}
}
}