Skip to content

Commit

Permalink
Progress on #62 addressing only proper capitalization. Missing is sen…
Browse files Browse the repository at this point in the history
…tence full stop.
  • Loading branch information
oggy22 committed Nov 13, 2017
1 parent 854a908 commit ad06779
Show file tree
Hide file tree
Showing 5 changed files with 63 additions and 5 deletions.
8 changes: 8 additions & 0 deletions TranslatorCpp/Languages/English.h
Original file line number Diff line number Diff line change
Expand Up @@ -110,4 +110,12 @@ struct English : public ENGLISH_BASE
wt == word_type::pronoun ||
wt == verb;
}

static bool should_capitalize(const translator::word_form<English> &w)
{
if (w.p_dw->wordtype == word_type::pronoun && w.word == "i")
return true;

return false;
}
};
5 changes: 5 additions & 0 deletions TranslatorCpp/Languages/Serbian.h
Original file line number Diff line number Diff line change
Expand Up @@ -153,4 +153,9 @@ struct Serbian : public SERBIAN_BASE
wt == word_type::придев ||
wt == word_type::прилог;
}

static bool should_capitalize(const translator::word_form<Serbian> &w)
{
return false;
}
};
24 changes: 24 additions & 0 deletions TranslatorCpp/Parse.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -68,6 +68,7 @@ int main(int argc, char *argv[])
if (stLang == "SR")
{
map<int, wstring> sents;
setlocale(LC_ALL, "");
generate_random_sentences<Serbian>(sents, seed);
set_wide();
for (auto const &pair : sents)
Expand Down Expand Up @@ -126,4 +127,27 @@ int main(int argc, char *argv[])

cerr << "Unrecognized command" << endl;
return -1;
}

char translator::capitalize(char c)
{
return toupper(c);
}

wchar_t translator::capitalize(wchar_t c)
{
wchar_t upper = towupper(c);
return upper;
}

void translator::to_lower(basic_string<char>& s)
{
for (basic_string<char>::iterator p = s.begin(); p != s.end(); ++p)
*p = tolower(*p);
}

void translator::to_lower(basic_string<wchar_t>& s)
{
for (basic_string<wchar_t>::iterator p = s.begin(); p != s.end(); ++p)
*p = towlower(*p);
}
8 changes: 8 additions & 0 deletions TranslatorCpp/language.h
Original file line number Diff line number Diff line change
Expand Up @@ -68,6 +68,10 @@ namespace translator
}
}

void to_lower(basic_string<char>& s);

void to_lower(basic_string<wchar_t>& s);

template <typename Language, typename letter = Language::letter, typename string_t = Language::string_t>
bool parse(typename Language::string_t s)
{
Expand All @@ -81,6 +85,10 @@ namespace translator

parsing_triangle<std::vector<parsing_node<Language>>> pt(vs.size());

// Lowercase all the words
for (auto &word : vs)
to_lower(word);

// Lexical analysis
for (unsigned int i = 0; i < vs.size(); i++)
{
Expand Down
23 changes: 18 additions & 5 deletions TranslatorCpp/random_sentence.h
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,18 @@

namespace translator
{
char capitalize(char c);

wchar_t capitalize(wchar_t c);

template<typename string_t>
const string_t capitalize_string(const string_t& st)
{
string_t copy = st;
copy[0] = capitalize(copy[0]);
return copy;
}

template<typename Language>
const rule<Language>& get_random_rule(std::default_random_engine& device, typename Language::word_type wt = Language::Sentence)
{
Expand Down Expand Up @@ -144,23 +156,24 @@ namespace translator
}
}

string_t ToString() const
string_t ToString(bool first_word=false) const
{
switch (t)
{
case RNode::string:
return st;
return first_word ? capitalize_string(st) : st;
case RNode::word:
{
const word_form<Language>& w = p_dw->find_word_form(am);
return w.word;
return first_word || Language::should_capitalize(w) ? capitalize_string(w.word) : w.word;
}
case RNode::node:
{
string_t stSum;
for (auto &child : children)
{
stSum += child.ToString();
stSum += child.ToString(first_word);
first_word = false;
stSum += string_t::value_type(' ');
}
return stSum.substr(0, stSum.size() - 1);
Expand All @@ -184,6 +197,6 @@ namespace translator

RNode<Language> node(r, device);
node.consolidate_attributes(device);
return node.ToString();
return node.ToString(true);
}
}

0 comments on commit ad06779

Please sign in to comment.