Permalink
Browse files

Words - SendWordsSynonyms

  • Loading branch information...
bobbymcr committed Dec 1, 2018
1 parent bce6ddd commit 06c513f0f1eee847e8973487c2f0337436671599
Showing with 51 additions and 6 deletions.
  1. +25 −2 core/src/Words.cs
  2. +22 −0 core/test/SentenceParserTest.cs
  3. +4 −4 sample/src/Game.cs
@@ -4,13 +4,36 @@

namespace Adventure
{
using System.Collections.Generic;

public sealed class Words
{
public Word this[string key]
private readonly Dictionary<string, string> words;

public Words()
{
this.words = new Dictionary<string, string>();
}

public Word this[string actual]
{
get
{
return new Word(key, key);
if (!this.words.TryGetValue(actual, out string primary))
{
primary = string.Empty;
}

return new Word(primary, actual);
}
}

public void Add(string primary, params string[] synonyms)
{
this.words.Add(primary, primary);
foreach (string synonym in synonyms)
{
this.words.Add(synonym, primary);
}
}
}
@@ -46,5 +46,27 @@ public void SendAfterDispose()

sentences.Should().BeEmpty();
}

[Theory]
[InlineData("see home", "look|house")]
[InlineData("inspect house", "look|house")]
[InlineData("examine home", "look|house")]
[InlineData("look house", "look|house")]
[InlineData("watch building", "|")]
public void SendWordsSynonyms(string input, string output)
{
MessageBus bus = new MessageBus();
List<string> sentences = new List<string>();
Action<SentenceMessage> onSentence = m => sentences.Add(m.Verb.Primary + "|" + m.Noun.Primary);
bus.Subscribe(onSentence);
Words words = new Words();
words.Add("look", "see", "inspect", "examine");
words.Add("house", "home");
SentenceParser parser = new SentenceParser(bus, words);

bus.Send(new InputMessage(input));

sentences.Should().ContainSingle().Which.Should().Be(output);
}
}
}
@@ -22,20 +22,20 @@ public void Run()
{
using (CancellationTokenSource cts = new CancellationTokenSource())
using (new SentenceParser(this.bus, new Words()))
using (this.bus.Subscribe<SentenceMessage>(m => this.ProcessVerb(cts, m.Verb.Primary)))
using (this.bus.Subscribe<SentenceMessage>(m => this.ProcessVerb(cts, m.Verb)))
{
this.console.Run(cts.Token);
}
}

private void ProcessVerb(CancellationTokenSource cts, string verb)
private void ProcessVerb(CancellationTokenSource cts, Word verb)
{
string output = null;
if (verb == "hello")
if (verb.Actual == "hello")
{
output = "world";
}
else if (verb == "quit")
else if (verb.Actual == "quit")
{
cts.Cancel();
}

0 comments on commit 06c513f

Please sign in to comment.