Skip to content

Instantly share code, notes, and snippets.

@jimregan
Created November 16, 2015 11:34
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save jimregan/7d547e754c6833354366 to your computer and use it in GitHub Desktop.
Save jimregan/7d547e754c6833354366 to your computer and use it in GitHub Desktop.
#include <iostream>
#include <vector>
using namespace std;
vector<string>
getNames(string in)
{
vector<string> out;
char c = in[0];
char prev = 0;
string buf = "";
bool grabtag = false;
int i=0;
while(i<in.length())
{
if(grabtag)
{
if (c != '%')
{
buf += c;
}
else
{
grabtag = false;
out.push_back(buf);
buf = "";
}
}
if(c =='%' && prev == '<')
{
grabtag = true;
}
prev = c;
c = in[++i];
}
return out;
}
wstring
mergeSurfaceForms(wstring in)
{
bool inword = false;
bool inanalysis = false;
wstring out = L"";
wstring blank = L"";
wchar_t cur = 0;
for(int i=0; i<=in.size(); i++)
{
cur = in[i];
if(cur == L'^')
{
inword = true;
inanalysis = false;
if(blank != L"")
{
out += L" ";
blank = L"";
}
cur = in[++i];
}
if(inword)
{
if(cur == L'/')
{
inword = false;
inanalysis = true;
}
else if(cur == L'\\')
{
out += L'\\';
out += in[++i];
}
else
{
out += cur;
}
}
if(inanalysis)
{
if(cur == L'$')
{
inanalysis = false;
}
}
if(!inword && !inanalysis)
{
blank += cur;
}
}
return out;
}
int main (int argc, char** argv)
{
vector<string> s = getNames("^testing<adj><%nbr%>$ ^foo<n><%nbr%>");
int count = 1;
vector<string>::iterator it;
int i;
for(it = s.begin(), i=0; it != s.end(); it++, i++)
{
cout << count << ' ' << s[i] << endl;
count++;
}
wcout << mergeSurfaceForms(L"^thing/thing<n>$[<b>]^to/to<foo>$ ^check/check<n>$") << endl;
wcout << mergeSurfaceForms(L"^th\\/ing/thing<n>$[<b>]^to/to<foo>$ ^check/check<n>$^2/2<num$") << endl;
return 0;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment