Alan remembers your daily tasks
public struct Reminder
{
public int day;
public int month;
public string content;
}
private List<Reminder> reminders = new List<Reminder>();
private async Task SaveReminder()
{
SpeechRecognizer reminder_recog = new SpeechRecognizer(new Windows.Globalization.Language("en-US"));
StorageFile reminder_grammar = await Package.Current.InstalledLocation.GetFileAsync(@"reminder_grammar.xml");
SpeechRecognitionGrammarFileConstraint date_rules = new SpeechRecognitionGrammarFileConstraint(reminder_grammar);
reminder_recog.Constraints.Add(date_rules);
var compilation_status = await reminder_recog.CompileConstraintsAsync();
SpeechRecognitionResult date, content;
bool done = false;
await GUIOutput("For which day?", true);
do
{
date = await reminder_recog.RecognizeAsync();
if(date.Text.Length == 0)
await GUIOutput("Please, repeat what you said.", true);
else
{
await GUIOutput($"You said: {date.Text}. Is it correct?", true);
SpeechRecognitionResult ack = await task_recognizer.RecognizeAsync();
if(ack.Text == "yes")
done = true;
else
await GUIOutput("Please, repeat the date.", true);
}
} while(!done);
await GUIOutput("What should I remember?", true);
done = false;
int cnt = 0;
do
{
content = await task_recognizer.RecognizeAsync();
if(content.Text.Length == 0)
{
cnt++;
if(cnt < 3)
await GUIOutput("Please, repeat what you said.", true);
}
else
{
await GUIOutput($"You said: {content.Text}. Is it correct?", true);
SpeechRecognitionResult ack = await task_recognizer.RecognizeAsync();
if(ack.Text == "yes")
done = true;
else
{
cnt++;
if(cnt < 3)
await GUIOutput("Please, repeat the reminder.", true);
}
}
} while(!done && cnt < 3);
if(cnt < 3)
{
reminders.Add(CreateReminder(date.Text, content.Text));
await GUIOutput("Reminder saved.", true);
}
else
await GUIOutput("I'm sorry, I couldn't create the reminder.", true);
state = AssistantState.BACKGROUND_DEFAULT;
}
private Reminder CreateReminder(string date, string content)
{
string day, month;
Reminder rem;
day = date.Split(' ')[1];
month = date.Split(' ')[0];
int.TryParse(day, out rem.day); // String to integer conversion
switch(month)
{
case "january":
rem.month = 1;
break;
case "february":
rem.month = 2;
break;
case "march":
rem.month = 3;
break;
case "april":
rem.month = 4;
break;
case "may":
rem.month = 5;
break;
case "june":
rem.month = 6;
break;
case "july":
rem.month = 7;
break;
case "august":
rem.month = 8;
break;
case "september":
rem.month = 9;
break;
case "october":
rem.month = 10;
break;
case "november":
rem.month = 11;
break;
case "december":
rem.month = 12;
break;
default:
rem.month = 0;
break;
}
rem.content = content;
return rem;
}
private async Task SearchReminder()
{
var today = System.DateTime.Now;
int rem_count = 0;
for(int i = 0; i < reminders.Count; i++)
{
if(reminders[i].month == today.Month && reminders[i].day == today.Day)
{
rem_count++;
await GUIOutput($"Reminder {rem_count}: {reminders[i].content}.", true);
}
}
if(rem_count == 0)
await GUIOutput("You have no reminders for today.", true);
state = AssistantState.BACKGROUND_DEFAULT;
}
In order to implement this functionality we used the following variables:
- A data structure called
Reminder
that contains all the informations about a reminder (day, month and content). - A list of
Remainder
elements which acts as a calendar.
The function SaveReminder
allows the user to vocally create a new reminder:
it asks the user to firstly enter the date (more details here), and then the content of the reminder. The functions
also handles misunderstandings that might occur during the process.
The function CreateReminder
is the one that actually creates a new reminder
struct (it acts like a constructor): it converts day and month from their string
value to two unsigned integers and fills the content field of the struct with the
information provided by the user.
The function SearchReminder
is called when the user command is "Any plans
for today?" and finds inside the reminder list all the elements that have a date equal to
the current day. If there isn't any, the string passed to the GUIOutput
function is "You have no reminders for today" otherwise the function outputs the
content of the reminders.