private async Task <string> CreateReminder(ICommandContext context, string message) { var result = FuzzyParsing.Moment(message); string error = null; if (!result.IsValid) { error = result.ErrorMessage; } else if (result.Value < DateTime.UtcNow) { error = PastValueErrorMessage.Replace("$moment$", result.Value.ToString(CultureInfo.InvariantCulture)); } if (error != null) { return(error); } else { var triggerTime = result.Value; var duration = triggerTime - DateTime.UtcNow; //Add some context to the message var prelude = $"{context.Message.Author.Mention} Reminder from {DateTime.UtcNow.Humanize(dateToCompareAgainst: triggerTime, culture: CultureInfo.GetCultureInfo("en-gn"))}..."; var msg = $"remind me {message}"; //Save to database var n = await _reminders.Create(triggerTime, prelude, msg, context.Message.Channel.Id, context.User.Id); var friendlyId = new FriendlyId32(n.ID); return($"I will remind you in {duration.Humanize(2, maxUnit: TimeUnit.Year, minUnit: TimeUnit.Second, toWords: true)} (id: `{friendlyId}`)"); } }
private static bool C([NotNull] string input) { var extr = FuzzyParsing.BooleanChoice(input); Console.WriteLine($"{input} : {extr.Value} ({extr.Confidence})"); return(extr.Value); }
public void ParseComplexTime() { var time = FuzzyParsing.Moment("Midday tomorrow"); Assert.IsTrue(time.IsValid); var tomorrow = DateTime.UtcNow.AddDays(1); var expected = new DateTime(tomorrow.Year, tomorrow.Month, tomorrow.Day, 12, 00, 0); Assert.AreEqual(expected, time.Value); }
public void ParseSimpleTimeWithTz() { var time = FuzzyParsing.Moment("15:35 BST tomorrow"); Assert.IsTrue(time.IsValid); var tomorrow = DateTime.UtcNow.AddDays(1); var expected = new DateTime(tomorrow.Year, tomorrow.Month, tomorrow.Day, 14, 35, 0); Assert.AreEqual(expected, time.Value); }
private static (string, decimal)? M([NotNull] string input) { var extr = FuzzyParsing.CurrencyAndAmount(input); Console.WriteLine($"{input} : {extr.Amount} {extr.Currency}"); if (!extr.IsValid) { return(null); } return(extr.Currency, extr.Amount); }
public void DateTimeRange() { var r1 = DateTimeRecognizer.RecognizeDateTime("next week", "en-gb"); Console.WriteLine(JsonConvert.SerializeObject(r1)); var r2 = FuzzyParsing.MomentRange("next week"); Console.WriteLine(JsonConvert.SerializeObject(r2)); var r3 = FuzzyParsing.Moment("monday"); Console.WriteLine(JsonConvert.SerializeObject(r3)); }
[ItemNotNull] private async Task <string> GetTime([CanBeNull] string tz = null) { var extract = FuzzyParsing.TimeOffset(tz ?? ""); var offset = extract.IsValid ? extract.UtcOffset : TimeSpan.Zero; string FormatTime(DateTime dt) => (dt).ToString("HH:mm:ss tt"); if (extract.IsValid || tz == null) { return($"The time is {FormatTime(DateTime.UtcNow + offset)} UTC{offset.Hours:+00;-00;+00}:{offset.Minutes:00}"); } else { return($"I'm not sure what timezone you mean, assuming UTC it's {FormatTime(DateTime.UtcNow)}"); } }
private async Task <string> CreateReminder(ICommandContext context, string message) { DateTime triggerMoment; // Parse a moment from message var exactTimeResult = FuzzyParsing.Moment(message); if (exactTimeResult.IsValid) { if (exactTimeResult.Value < DateTime.UtcNow) { return(PastValueErrorMessage.Replace("$moment$", exactTimeResult.Value.ToString(CultureInfo.InvariantCulture))); } triggerMoment = exactTimeResult.Value; } else { // Attempt to parse a time range instead of an exact time (e.g. `next week`) var rangeTimeResult = FuzzyParsing.MomentRange(message); if (!rangeTimeResult.IsValid) { return("That doesn't seem to be a valid moment."); } // Send the reminder just after the start of the range triggerMoment = rangeTimeResult.Value.Item1 + (rangeTimeResult.Value.Item2 - rangeTimeResult.Value.Item1) * 0.05f; } var duration = triggerMoment - DateTime.UtcNow; // Add some context to the message var prelude = $"{context.Message.Author.Mention} Reminder from {DateTime.UtcNow.Humanize(dateToCompareAgainst: triggerMoment, culture: CultureInfo.GetCultureInfo("en-gn"))}..."; var msg = $"remind me {message}"; // Save to database var n = await _reminders.Create(triggerMoment, prelude, msg, context.Message.Channel.Id, context.User.Id); var friendlyId = new BalderHash32(n.ID); return($"I will remind you in {duration.Humanize(2, maxUnit: TimeUnit.Year, minUnit: TimeUnit.Second, toWords: true)} (id: `{friendlyId}`)"); }
public async Task MaybeCat() { var real = _roll.Flip(); var stream = await(real ? _cats.GetCatPictureAsync() : _mlcats.GetCatPictureAsync()); stream.Seek(0, SeekOrigin.Begin); //Upload image await Context.Channel.SendFileAsync(stream, "this might be a cat.png"); await ReplyAsync("Is this a real cat or not?"); var next = await NextMessageAsync(true, true, TimeSpan.FromSeconds(10)); if (next == null) { await ReplyAsync("Too slow!"); } else { var choice = FuzzyParsing.BooleanChoice(next.Content); if (choice.Confidence < 0.5f) { await ReplyAsync("Sorry, I don't understand you :("); } else if (choice.Value == real) { await ReplyAsync("Correct!"); } else if (real) { await TypingReplyAsync("Wrong! This is a real kitty"); } else { await TypingReplyAsync("Wrong! This is a machine kitty"); } } }
private static string GetTime(string?tz = null) { var extract = FuzzyParsing.TimeOffset(tz ?? ""); var offset = extract.IsValid ? extract.UtcOffset : TimeSpan.Zero;