Design Pattern Examples
Overview of object-oriented design patterns
Interpreter_Class.cpp
Go to the documentation of this file.
1
5
6#include <sstream>
7
8#include "helpers/_countof.h"
9#include "helpers/formatstring.h"
10#include "helpers/titlecase.h"
11
12#include "Interpreter_Class.h"
13
15{
16
18 {
19 "the",
20 "be",
21 "to",
22 "of",
23 "and",
24 "a",
25 "in",
26 "that",
27 "have",
28 "I",
29 "it",
30 "for",
31 "not",
32 "on",
33 "with",
34 "he",
35 "as",
36 "you",
37 "do",
38 "at",
39 "this",
40 "but",
41 "his",
42 "by",
43 "from",
44 "they",
45 "we",
46 "say",
47 "her",
48 "she",
49 "or",
50 "an",
51 "will",
52 "my",
53 "one",
54 "all",
55 "would",
56 "there",
57 "their",
58 "what",
59 };
60
61
63 {
64 std::string tokenAsString = "";
65
66 // Rule 1: token is between 0 and the number of common words.
67 if (token >= 0 && token < static_cast<int>(_countof(_commonwords)))
68 {
69 tokenAsString = _commonwords[token];
70 }
71 else
72 {
73 // Rule 1: token can also be a PERIOD
74 if (token == PERIOD)
75 {
76 tokenAsString = ".";
77 }
78 // Rule 1: or the token can also be a QUESTION
79 else if (token == QUESTION)
80 {
81 tokenAsString = "?";
82 }
83 else
84 {
85 // Rule 1: Invalid tokens returned in a std::string.
86 tokenAsString = Helpers::formatstring("<UNKNOWN TOKEN %d>", token);
87 }
88 }
89 return tokenAsString;
90 }
91
92
94 {
95 std::ostringstream output;
96
97 size_t numTokens = tokens.size();
98
99 for (size_t tokenIndex = 0; tokenIndex < numTokens; ++tokenIndex)
100 {
101 // Rule 1: Interpret token
102 std::string tokenAsString = _InterpretToken(tokens[tokenIndex]);
103 if (tokenIndex == 0)
104 {
105 // Rule 2: First word in sentence gets capitalized according to local rules.
106 tokenAsString = Helpers::titlecase(tokenAsString);
107 }
108 output << tokenAsString;
109
110 // Rule 4: No space between last two tokens (if the following expression is false)
111 if (tokenIndex + 2 < numTokens)
112 {
113 // Rule 3: Separate all words by a single space.
114 output << " ";
115 }
116 }
117
118 return output.str();
119 }
120
121} // end namespace
Declaration of the Interpreter_Class class used in the Interpreter Pattern.
std::string Interpret(IntList tokens)
Given an array of integer tokens, convert the tokens into a single std::string of space-delimited wor...
std::string _InterpretToken(int token)
Helper method to convert the token into its corresponding word or punctuation mark.
static const char * _commonwords[]
The 40 most common words in English (in order but that doesn't really matter here)....
#define _countof(w)
The namespace containing all Design Pattern Examples implemented in C++.
std::vector< int > IntList
Alias to make it easier to work with a vector of vectors of int.
std::string formatstring(const char *fmt,...)
Use the given string and arguments to return a buffer containing the formatted string....
std::string titlecase(const std::string &s)
Convert the first word (or only word) in the given string to lowercase then make the first letter upp...
Definition: titlecase.cpp:11