Skip to content
This repository has been archived by the owner on Aug 17, 2021. It is now read-only.

Commit

Permalink
Feature: added ability to check IWordMorpher can morph word; some opt…
Browse files Browse the repository at this point in the history
…imization and refactoring.
  • Loading branch information
d0rj committed Feb 1, 2021
1 parent d25ccf4 commit afd9624
Show file tree
Hide file tree
Showing 5 changed files with 51 additions and 15 deletions.
19 changes: 19 additions & 0 deletions Meerkat/Library/AdjectiveMorpher.cs
Original file line number Diff line number Diff line change
Expand Up @@ -31,5 +31,24 @@ public override void Cache(string key)

cached.Add(key, adj);
}


public bool CanMorph(string word)
{
if (IsCached(word))
return true;
else
{
try
{
Cache(word);
return true;
}
catch (UnknownWordException)
{
return false;
}
}
}
}
}
6 changes: 6 additions & 0 deletions Meerkat/Library/Caching.cs
Original file line number Diff line number Diff line change
Expand Up @@ -18,5 +18,11 @@ public T GetCached(string key)

return cached[key];
}


public bool IsCached(string key)
{
return cached.ContainsKey(key);
}
}
}
1 change: 1 addition & 0 deletions Meerkat/Library/Interfaces/IWordMorpher.cs
Original file line number Diff line number Diff line change
Expand Up @@ -6,5 +6,6 @@ namespace Meerkat.Library.Interfaces
public interface IWordMorpher
{
string Morph(string word, string command, string modifier);
bool CanMorph(string word);
}
}
19 changes: 19 additions & 0 deletions Meerkat/Library/NounMorpher.cs
Original file line number Diff line number Diff line change
Expand Up @@ -48,5 +48,24 @@ public override void Cache(string key)

cached.Add(key, noun);
}


public bool CanMorph(string word)
{
if (IsCached(word))
return true;
else
{
try
{
Cache(word);
return true;
}
catch (UnknownWordException)
{
return false;
}
}
}
}
}
21 changes: 6 additions & 15 deletions Meerkat/Library/TemplateEngine.cs
Original file line number Diff line number Diff line change
Expand Up @@ -77,34 +77,25 @@ public string ProcessSingle(string template)

if (Variables.ContainsKey(parsedVar))
{
string processedWord = "";
Exception exception = null;
var processedWord = string.Empty;
string word = Variables[parsedVar];

foreach (var morpher in wordMorphers)
{
try
if (morpher.CanMorph(word))
{
processedWord = morpher.Morph(
Variables[parsedVar],
command,
modifier);

exception = null;
processedWord = morpher.Morph(word, command, modifier);
break;
}
catch (UnknownWordException e)
{
exception = e;
}
}

if (exception == null)
if (processedWord != string.Empty)
{
processedWord = HandleUppercase(template, processedWord);
return processedWord;
}
else
throw exception;
throw new UnknownWordException(word);
}
else
{
Expand Down

0 comments on commit afd9624

Please sign in to comment.