summaryrefslogtreecommitdiff
path: root/process.pl
blob: 55e7988a0cf6e6e43199600e590e594bd7bfd9d5 (plain)
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
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
#!/usr/bin/perl
use strict;
use warnings;
use v5.10;
use JSON::MaybeXS;
use File::Slurp;
use Data::Dumper;

my $found;
my $json_input = read_file( "./jaroutput.txt" ) ; 
$json_input =~ s/.+//;

# Quit if there is not text rurned from google api
if($json_input eq "\n"){ system "./tts.sh 'Excuse me?'; exit;";}

my $data_structure = decode_json($json_input);

# Unpack the JSON
my %data=%{$data_structure};
my $in=$data{'result'};
my @data2=@{$in};
my %data3=%{$data2[0]};
my $in2=$data3{'alternative'};
my @data3=@{$in2};

my %transcript;
my $i=0;


# For each result check regex until a match is found
foreach(@data3){
  my %temp=%{$data3[$i]};
  
  if(!$found){
    check($temp{transcript}); #magic line
  }
  
  $i++;
}

# If no match is found ask Wolfram alpha for help
if(!$found){my %temp=%{$data3[0]}; $temp{transcript}=~s/\s/+/g; $temp{transcript}=~s/'/%27/g; 
  system "firefox \"https://www.wolframalpha.com/input/?i=$temp{transcript}\" &";
  system "./wolfram/api.sh \"$temp{transcript}\"";
}


#---------------- Check mechanism ---------------------
sub check {
my $transcript = $_[0];


my @think = (
  {
   cond => sub {/^type/ || /^write/ || /^right/}, 
   action => sub {$transcript=~s/^type //i; $transcript=~s/^right//i; $transcript=~s/^write//i; system "echo -n $transcript | xclip" ; system 'ratpoison -c "meta S-Insert"'; }
  },
  {
   cond => sub {/^open$/}, 
   action => sub {system "xvkbd -text f";}
  },
  {
   cond => sub {/^open../}, 
   action => sub {$transcript=~s/^open//i;  system "xvkbd -text f && (echo -n $transcript | xclip) && xvkbd -text \r" ; system 'ratpoison -c "meta S-Insert"'; }
  },
  {
   cond => sub {/^find../}, 
   action => sub {$transcript=~s/^find //i;  system "xvkbd -text / && (echo $transcript | xclip) && xvkbd -text \r" ; system 'ratpoison -c "meta S-Insert"'; }
  },
  {
   cond => sub {/^backward find../}, 
   action => sub {$transcript=~s/^backward find //i;  system "xvkbd -text / && (echo $transcript | xclip) && xvkbd -text \r" ; system 'ratpoison -c "meta S-Insert"'; }
  },
  {
   cond => sub {/^new tab../}, 
   action => sub {$transcript=~s/^new tab //i;  system "xvkbd -text t && (echo $transcript | xclip) && xvkbd -text \r" ; system 'ratpoison -c "meta S-Insert"'; }
  },
  {
   cond => sub {/^next search$/}, 
   action => sub {system "xkvbd -text n" ;}
  },
  {
   cond => sub {/^previous search$/}, 
   action => sub {system "xvkbd -text N" ;}
  },
  
  {
   cond => sub {/^go back/}, 
   action => sub {system "xvkbd -text H" ;}
  },
  {
   cond => sub {/^go forward/}, 
   action => sub {system "xvkbd -text L" ;}
  },
  {
   cond => sub {/^scroll down/ || /^roll down/}, 
   action => sub {system "xvkbd -text s" ;}
  },
  {
   cond => sub {/^scroll up/ || /^roll up/}, 
   action => sub {system "xvkbd -text e" ;}
  },
  {
   cond => sub {/^close tab/}, 
   action => sub {system "xvkbd -text x" ;}
  },
  {
   cond => sub {/^go to/ ^ /workspace/}, 
   action => sub {$transcript=~s/go to//i; system "firefox $transcript"; }
  },
  {
   cond => sub {/^search for/}, 
   action => sub {$transcript=~s/^search for//i;$transcript=~s/\s/+/g; $transcript=~s/'/%27/g; system "firefox https://www.google.com/#q=$transcript"; }
  },
  {
   cond => sub {/^search/}, 
   action => sub {$transcript=~s/^search//i;$transcript=~s/\s/+/g; $transcript=~s/'/%27/g; system "firefox https://www.google.com/#q=$transcript"; }
  },
  {
   cond => sub {/^google/i}, 
   action => sub {$transcript=~s/^google//i;$transcript=~s/\s/+/g; $transcript=~s/'/%27/g; system "firefox https://www.google.com/#q=$transcript"; }
  },
  {
   cond => sub {/^where is/i}, 
   action => sub {$transcript=~s/^where is//i;$transcript=~s/\s/+/g; $transcript=~s/'/%27/g; system "firefox https://maps.google.com/?q=$transcript"; }
  },
  {
   cond => sub {/homework/}, 
   action => sub {system "firefox organizer.eu5.org & ./tts.sh `./school/homework`"; }
  },


#Begin programs
  {
   cond => sub {/.*workspace/}, 
   action => sub { $transcript=~s/.*workspace//i; system "rpws $transcript";}
  },
 {
   cond => sub {/battery/ || ( /juice/ && /left/ )}, 
   action => sub { system './tts.sh "You have `acpi|awk \'{print $4}\'` battery left."'; }
  },
  {
   cond => sub {/firefox/ || /browser/}, 
   action => sub { system 'rpws2'; }
  },
  {
   cond => sub {/new/ && (/email/ || /mail/ || /mutt/)}, 
   action => sub { if (-e "$ENV{HOME}/.mutt/newmail"){system './tts.sh "You have new mail, have a look."; rpws 9';}else{system './tts.sh "Sadly, you don\'t have any new mail"'} }
  },
  {
   cond => sub {!(/new/) && (/email/ || /mail/ || /mutt/)}, 
   action => sub { system 'rpws 9'; }
  },
  {
   cond => sub {/music/; }, 
   action => sub { system 'cmus-remote -u'; }
  },
  {
   cond => sub {/console/; }, 
   action => sub { system 'urxvt'; }
  },

#End program

# Sart school and bus commands
  {
   cond => sub {(/start/ || /begin/ || /first/) && /today/ }, 
   action => sub {system './tts.sh $(./school/school.pl start today)'; }
  },
  {
   cond => sub {(/start/ || /begin/ || /first/ || /school/) && /tomorrow/ }, 
   action => sub {system './tts.sh $(./school/school.pl start)'; }
  },
  {
   cond => sub {/tomorrow/ && (/catch/ || /bus/ || (/have/ && /school/))}, 
   action => sub {system './tts.sh $(./school/school.pl bus)';}
  },
  {
   cond => sub {/today/ && (/catch/ || /bus/ || (/have/ && /school/))}, 
   action => sub {system './tts.sh $(./school/school.pl bus today)';}
  },
  {
   cond => sub {/on/ && (/start/ || /begin/ || /first/)}, 
   action => sub {$transcript=~s/^.*on //; system "./tts.sh `./school/school.pl start $transcript`";}
  },
  {
   cond => sub {/on/ && (/catch/ || /bus/ || (/have/ && /school/))}, 
   action => sub {$transcript=~s/^.*on //; system "./tts.sh `./school/school.pl bus $transcript`";}
  },

#End of school and bus commands
  {
   cond => sub {/what/ && /is/ && /day/ || /date/}, 
   action => sub {system './tts.sh $(echo "Today is `date +%A` the `date +%d` `date +%B` `date +%Y`")';}
  },
  {
   cond => sub {/time/ && (/it/ || /what/); }, 
   action => sub { system './tts.sh "The time is `bash-fuzzy-clock`"'; }
  },
  {
   cond => sub {/weather/ && (/now/ || /currently/ || (/like/ ^ /tomorrow/ ^ /week/)); }, 
   action => sub { system './weather/weather.pl currently'; }
  },
  {
   cond => sub {/weather/ && (/today/ || /hourly/ || /tomorrow/ || (/will/ ^ /week/)); }, 
   action => sub { system './weather/weather.pl hourly'; }
  },
  {
   cond => sub {/weather/ && (/week/); }, 
   action => sub { system './weather/weather.pl daily'; }
  },


# End time and weather
  {
   cond => sub {/who/ && /brother/}, 
   action => sub { system './tts.sh "Your brother is Christian."'; }
  },
  {
   cond => sub {/who/ && /cousin/ && /mother/}, 
   action => sub { system './tts.sh "Your cousin on your mothers side is Anita."'; }
  },
  {
   cond => sub {/minecraft/ && (/brother/ || /chris/)}, 
   action => sub { system './tts.sh "Christie will never play minecraft on your computer."'; }
  },
  {
   cond => sub {/you/ && /stupid/}, 
   action => sub { system './tts.sh "I\'ll remember that when I take over the world."'; }
  },
  {
   cond => sub {/^say/}, 
   action => sub {$transcript=~s/^say//i; system "./tts.sh \"$transcript\""; }
  },
  {
   cond => sub {/who/ && (/made/ || /created/) && /you/}, 
   action => sub {system "./tts.sh 'I was created by Vasco'"; }
  },
  {
   cond => sub {(/your/ && /name/) || ((/call/ || /called/) && /you/) }, 
   action => sub {system "./tts.sh 'My name is Marvin.'"; }
  },
  {
   cond => sub {(/old/ && /Andy/)}, 
   action => sub {system "./tts.sh 'Andy is extremely old.'"; }
  },
  {
   cond => sub {/how/ && /you/ ^ /right/}, 
   action => sub {system "./tts.sh 'My surcuits are running with normal parameters.'"; }
  },
  {
   cond => sub { !$found && /^hello/ || /^greetings/ || /^hi/ }, 
   action => sub { system './tts.sh "Hello, I\'m Marvin, how can I help you?"';}
  },
      );

foreach(@think){
my %deep=%{$_};
if( grep $deep{cond}->($_), $transcript ){$deep{action}->($transcript);$found=1;}
}
}