Skip to content
Snippets Groups Projects
Commit 76b7e375 authored by Jan Trávníček's avatar Jan Trávníček
Browse files

NormalizeAlphabet for strings

parent d0e70ecf
No related branches found
No related tags found
No related merge requests found
/*
* NormalizeAlphabet.cpp
*
* Created on: Dec 9, 2013
* Author: Jan Travnicek
*/
#include "NormalizeAlphabet.h"
#include "std/map.hpp"
#include <deque>
#include <vector>
#include <set>
#include <algorithm>
#include <sstream>
#include <iostream>
#include "exception/AlibException.h"
#include "alphabet/Symbol.h"
namespace string {
namespace simplify {
string::String NormalizeAlphabet::normalize(const string::String& string) {
string::String* out = NULL;
string.getData().Accept((void*) &out, NormalizeAlphabet::NORMALIZE);
string::String res = std::move(*out);
delete out;
return res;
}
string::LinearString NormalizeAlphabet::normalize(const string::LinearString& string) {
int counter = 0;
std::map<alphabet::Symbol, char > normalizationData;
for(const alphabet::Symbol& symbol : string.getContent()) {
if(normalizationData.find(symbol) == normalizationData.end()) {
normalizationData.insert(std::make_pair(symbol, counter++));
}
}
std::set<alphabet::Symbol> alphabet;
for(const auto& symbol : normalizationData) {
alphabet.insert(alphabet::symbolFrom((char) (symbol.second + 'a')));
}
std::vector<alphabet::Symbol> data;
for(const alphabet::Symbol& symbol : string.getContent()) {
data.push_back(alphabet::symbolFrom((char) (normalizationData.find(symbol)->second + 'a')));
}
string::LinearString result(alphabet, data);
return result;
}
void NormalizeAlphabet::Visit(void*, const string::Epsilon&) const {
throw exception::AlibException("Unsupported automaton type Epsilon");
}
void NormalizeAlphabet::Visit(void* data, const string::LinearString& str) const {
string::String* & out = *((string::String**) data);
out = new string::String(this->normalize(str));
}
void NormalizeAlphabet::Visit(void*, const string::CyclicString&) const {
throw exception::AlibException("Unsupported automaton type CyclicString");
}
const NormalizeAlphabet NormalizeAlphabet::NORMALIZE;
} /* namespace simplify */
} /* namespace string */
/*
* NormalizeAlphabet.h
*
* Created on: Dec 9, 2013
* Author: Jan Travnicek
*/
#ifndef NORMALIZE_ALPHABET_H_
#define NORMALIZE_ALPHABET_H_
#include <string/LinearString.h>
#include <string/String.h>
namespace string {
namespace simplify {
class NormalizeAlphabet : public string::VisitableStringBase::const_visitor_type {
public:
/**
* @param dfa automaton to normalize
*/
static string::String normalize(const string::String& str);
static string::LinearString normalize(const string::LinearString& str);
protected:
void Visit(void*, const string::Epsilon& string) const;
void Visit(void*, const string::LinearString& string) const;
void Visit(void*, const string::CyclicString& string) const;
static const NormalizeAlphabet NORMALIZE;
};
} /* namespace simplify */
} /* namespace string */
#endif /* NORMALIZE_ALPHABET_H_ */
...@@ -9,6 +9,7 @@ ...@@ -9,6 +9,7 @@
   
#include "exception/AlibException.h" #include "exception/AlibException.h"
#include "factory/XmlDataFactory.hpp" #include "factory/XmlDataFactory.hpp"
#include "string/simplify/NormalizeAlphabet.h"
#include "automaton/simplify/Normalize.h" #include "automaton/simplify/Normalize.h"
#include "automaton/simplify/SingleInitialState.h" #include "automaton/simplify/SingleInitialState.h"
#include "grammar/convert/ToGrammarLeftRG.h" #include "grammar/convert/ToGrammarLeftRG.h"
...@@ -28,12 +29,18 @@ int main(int argc, char** argv) { ...@@ -28,12 +29,18 @@ int main(int argc, char** argv) {
TCLAP::ValueArg<std::string> form( "f", "form", "Convert to different form", false, "", &formVals); TCLAP::ValueArg<std::string> form( "f", "form", "Convert to different form", false, "", &formVals);
cmd.add( form ); cmd.add( form );
   
std::vector<std::string> inputTypes {"automaton", "grammar" }; std::vector<std::string> inputTypes {"automaton", "grammar"};
TCLAP::ValuesConstraint<std::string> inputTypeVals( inputTypes ); TCLAP::ValuesConstraint<std::string> inputTypeVals( inputTypes );
   
TCLAP::ValueArg<std::string> labels( "l", "labels", "Normalize labels", false, "", &inputTypeVals); TCLAP::ValueArg<std::string> labels( "l", "labels", "Normalize labels", false, "", &inputTypeVals);
cmd.add( labels ); cmd.add( labels );
   
std::vector<std::string> inputTypes2 {"string"};
TCLAP::ValuesConstraint<std::string> inputTypeVals2( inputTypes2 );
TCLAP::ValueArg<std::string> alphabet( "a", "alphabet", "Normalize alphabet", false, "", &inputTypeVals2);
cmd.add( alphabet );
cmd.parse(argc, argv); cmd.parse(argc, argv);
   
std::list<sax::Token> tokens; std::list<sax::Token> tokens;
...@@ -53,6 +60,9 @@ int main(int argc, char** argv) { ...@@ -53,6 +60,9 @@ int main(int argc, char** argv) {
} else if(labels.getValue() == "grammar") { } else if(labels.getValue() == "grammar") {
// alib::XmlDataFactory::toStdout(automaton::simplify::Normalize::normalize(alib::XmlDataFactory::fromTokens<automaton::Automaton>(tokens))); // alib::XmlDataFactory::toStdout(automaton::simplify::Normalize::normalize(alib::XmlDataFactory::fromTokens<automaton::Automaton>(tokens)));
return 0; return 0;
} else if(alphabet.getValue() == "string") {
alib::XmlDataFactory::toStdout(string::simplify::NormalizeAlphabet::normalize(alib::XmlDataFactory::fromTokens<string::String>(tokens)));
return 0;
} else if(form.getValue() == "leftRG") { } else if(form.getValue() == "leftRG") {
alib::XmlDataFactory::toStdout(grammar::convert::ToGrammarLeftRG::convert(alib::XmlDataFactory::fromTokens<grammar::Grammar>( tokens ))); alib::XmlDataFactory::toStdout(grammar::convert::ToGrammarLeftRG::convert(alib::XmlDataFactory::fromTokens<grammar::Grammar>( tokens )));
return 0; return 0;
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment