private IKalos CreateResponse(IKalos originalMessage, IList <IWhatIs> results) { var responseText = $"{originalMessage.Sender}, "; var builder = new StringBuilder(); builder.Append(responseText); for (int i = 0; i < results.Count; i++) { var currentResult = results[i]; if (i != 0) { builder.Append(", "); } if (i == results.Count - 1 && i != 0) { builder.Append("and "); } if (!string.IsNullOrEmpty(currentResult.Object)) { builder.Append($"{currentResult.Subject}'s {currentResult.Object} is {currentResult.Answer}"); } else { builder.Append($"{currentResult.Subject} is {currentResult.Answer}"); } } builder.Append("."); responseText = builder.ToString(); var outMessage = MessageCreator.CreateOutgoingMessage(originalMessage.Origin, HumanInterface.Name, responseText); return(outMessage); }
public async Task <IPurposeFullfillment> WorkAsync(IContext context, IKalos message) { var purposeFullfillment = new PurposeFullfillment(); if (!context.AddressedToSelf) { purposeFullfillment.Weight = -1; return(purposeFullfillment); } var matchedWords = new List <IKeyword>(); await Task.Run(() => { foreach (var keyword in OvermindAccessor.GetAllKeywords()) { if (message.Msg.Contains(keyword.Word)) { var previousMatch = matchedWords.FirstOrDefault(); if (previousMatch == null) { matchedWords.Add(keyword); } else if (previousMatch.Word.Length < keyword.Word.Length) { matchedWords.Remove(previousMatch); matchedWords.Add(keyword); } } } }); if (matchedWords.Count == 0) { purposeFullfillment.Weight = -1; return(purposeFullfillment); } if (!LastTimeKeywordUsed.ContainsKey(matchedWords[0].Word)) { LastTimeKeywordUsed.Add(matchedWords[0].Word, DateTime.Now); } else { if (LastTimeKeywordUsed[matchedWords[0].Word] > DateTime.Now.AddMinutes(-1)) { purposeFullfillment.Weight = -1; return(purposeFullfillment); } LastTimeKeywordUsed[matchedWords[0].Word] = DateTime.Now; } purposeFullfillment.Weight = 100; var responses = matchedWords[0].Response.Split('\n'); foreach (var response in responses) { var outMessage = MessageCreator.CreateOutgoingMessage(message.Origin, HumanInterface.Name, response); purposeFullfillment.AddMessage(outMessage); } return(purposeFullfillment); }
public async Task <IPurposeFullfillment> ObserveMessageAsync(IKalos message) { var activeTasks = new List <Task <IPurposeFullfillment> >(); foreach (var purpose in Purposes.Where(p => p.Value.Active)) { activeTasks.Add(purpose.Value.WorkAsync(EvaluateContext(message), message)); } var bestScore = 0; var returnedMessages = new List <IPurposeFullfillment>(); while (bestScore < ConfidenceThreshhold && activeTasks.Count > 0) { var completedTask = await Task.WhenAny(activeTasks); activeTasks.Remove(completedTask); var returnMessage = await completedTask; if (returnMessage.Weight > bestScore) { bestScore = returnMessage.Weight; } returnedMessages.Add(returnMessage); } return(returnedMessages.OrderBy(p => p.Weight).Reverse().FirstOrDefault()); }
public async Task <IPurposeFullfillment> WorkAsync(IContext context, IKalos message) { var purposeFullfillment = new PurposeFullfillment(); var messageLower = message.Msg.ToLowerInvariant(); var obj = ExtractObject(messageLower); if (string.IsNullOrEmpty(obj)) { var subject = ExtractIndependentSubject(messageLower); if (string.IsNullOrEmpty(subject) || subject.Length < 2) { purposeFullfillment.Weight = -1; return(purposeFullfillment); } IList <IWhatIs> results = null; await Task.Run(() => { results = OvermindAccessor.GetMatchingWhatIsAnswers(subject, string.Empty, "definition"); }); if (results.Count == 0) { purposeFullfillment.Weight = -1; return(purposeFullfillment); } var response = CreateResponse(message, results); purposeFullfillment.AddMessage(response); purposeFullfillment.Weight = 1000001; return(purposeFullfillment); } else { var subject = ExtractDependentSubject(messageLower); if (string.IsNullOrEmpty(subject)) { purposeFullfillment.Weight = -1; return(purposeFullfillment); } IList <IWhatIs> results = null; await Task.Run(() => { results = OvermindAccessor.GetMatchingWhatIsAnswers(subject, obj, "attribute"); }); if (results.Count == 0) { purposeFullfillment.Weight = -1; return(purposeFullfillment); } var response = CreateResponse(message, results); purposeFullfillment.AddMessage(response); purposeFullfillment.Weight = 1000001; return(purposeFullfillment); } }
private IContext EvaluateContext(IKalos message) { var context = new Context(); //for now, just check if message contains "riritia" if (message.Msg.ToLowerInvariant().Contains("riritia")) { context.AddressedToSelf = true; } return(context); }
public async Task <IPurposeFullfillment> WorkAsync(IContext context, IKalos message) { var purposeFullfillment = new PurposeFullfillment(); if (!context.AddressedToSelf) { purposeFullfillment.Weight = -1; return(purposeFullfillment); } var outgoingMessage = MessageCreator.CreateOutgoingMessage(message.Origin, HumanInterface.Name, Response); purposeFullfillment.AddMessage(outgoingMessage); purposeFullfillment.Weight = 1; return(purposeFullfillment); }
public void AddMessageToOutgoing(IKalos message) { try { using (var streamWriter = File.AppendText("../tempChatLog.txt")) { streamWriter.WriteLine($"Sender: {message.Sender}|Origin:{message.Origin}|Timestamp:{message.Timestamp}|Message:{message.Msg}"); } } catch (Exception e) { } if (!Communicators.ContainsKey(message.Target)) { return; } Communicators[message.Target].AddMessage(message); }
public async Task AddMessageToIncomingAsync(IKalos message) { try { using (var streamWriter = File.AppendText("../tempChatLog.txt")) { streamWriter.WriteLine($"Sender: {message.Sender}|Origin:{message.Origin}|Timestamp:{message.Timestamp}|Message:{message.Msg}"); } } catch (Exception e) { } var outgoingMessages = await PurposeObserver.ObserveMessageAsync(message); foreach (var outMessage in outgoingMessages.Messages) { if (string.IsNullOrEmpty(outMessage.Target)) { outMessage.Target = message.Target; } AddMessageToOutgoing(outMessage); } }
public void AddMessage(IKalos message) { OutgoingMessages.Add(message); }
public void AddMessageToOutgoing(IKalos message) { throw new NotImplementedException(); }
public Task AddMessageToIncomingAsync(IKalos message) { throw new NotImplementedException(); }
public void AddMessage(IKalos message) { MessagesInternal.Add(message); }