-
Notifications
You must be signed in to change notification settings - Fork 0
/
sh_transcriptor.cpp
116 lines (105 loc) · 3.9 KB
/
sh_transcriptor.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
#include "sh_transcriptor.h"
sh_transcriptor::sh_transcriptor(const char *analyses):transcriptor(analyses){
}
sh_transcriptor::~sh_transcriptor(){
logger::singleton()==NULL?(void)0:logger::singleton()->log(3,"destructing sh transcriptor");
}
std::string sh_transcriptor::args_to_shfun_arglist(std::vector<std::string>& arguments){
std::string arglist;
for(auto&& i:arguments){
arglist+="\"$"+i+"\" ";
}
if(arglist.empty()==false) arglist.pop_back();
return arglist;
}
std::string sh_transcriptor::args_to_shfun_pmlist(std::vector<std::string>& arguments){
std::string arglist;
for(auto&& i:arguments){
arglist+=i+" ";
}
if(arglist.empty()==false) arglist.pop_back();
return arglist;
}
std::string sh_transcriptor::transcribeDependencies(rapidjson::Value& morphology,rapidjson::Value& syntax,rapidjson::Value& dependencies,rapidjson::Value& functors,std::vector<std::string>& argList,bool first_call){
std::string script;
std::vector<std::string> arguments;
for(unsigned int i=0;i<dependencies.Size();++i){
rapidjson::Value& dependency=dependencies[i];
std::string functorID;
if(dependency.HasMember("functor id")==true){
functorID=dependency["functor id"].GetString();
}
if(functorID.empty()==true){
functorID=dependency["functor"].GetString();
}
std::string dependencyID=dependency["id"].GetString();
std::string functionName=functorID+"_"+dependencyID;
std::string argument=functionName+"_out";
argList.push_back(argument);
if(dependency.HasMember("dependencies")==true){
script+=transcribeDependencies(morphology,syntax,dependency["dependencies"],functors,arguments);
}
std::string morphologyArg;
if(dependency.HasMember("morpheme id")==true){
std::string morphemeID=dependency["morpheme id"].GetString();
for(unsigned int j=0;j<morphology.Size();++j){
rapidjson::Value& morphologyObj=morphology[j];
if(morphologyObj["morpheme id"].GetString()==morphemeID){
morphologyArg=value_to_string(morphologyObj);
find_replace(morphologyArg,"\"","\\\"");
break;
}
}
}
std::string morphologyVarName=functionName+"_morphology";
arguments.push_back(morphologyVarName);
std::string tags;
std::string tagsVarName;
if(dependency.HasMember("tags")==true){
for(auto& tagObj: dependency["tags"].GetArray()){
std::string tag_quoted;
for(auto& tag: tagObj.GetObject()){
tag_quoted+="\""+std::string(tag.name.GetString())+"\":\""+std::string(tag.value.GetString())+"\",";
//quoting here breaks sh script
//find_replace(tag_quoted,"\"","\\\"");
}
if(tag_quoted.empty()==false){
tag_quoted.pop_back();
tags+="{"+tag_quoted+"},";
}
}
if(tags.empty()==false){
tags.pop_back();
tags="["+tags+"]";
tagsVarName=functionName+"_tags";
arguments.push_back(tagsVarName);
}
}
std::string argStr=args_to_shfun_arglist(arguments);
std::string parameterList=args_to_shfun_pmlist(arguments);
std::string functorDef;
for(unsigned int j=0;j<functors.Size();++j){
rapidjson::Value& functor=functors[j];
if(functor["functor id"].GetString()==functorID){
functorDef=functor["definition"].GetString();
if(functorDef.empty()==false){
functorDef=functionName+"(){ "+functorDef+" };";
}
break;
}
}
if(functorDef.empty()==true){
//TODO:if functor has no definition but has dependencies, propagate the outgoing result of its dependencies to the parent.
//However, if there are more than one functor having no definition it'll be necessary to pass on which is the last
//functor that has a definition in order that the arguments are propagated to the right level.
script+=functionName+"_out="+"\""+morphologyArg+"\";";
}
else{
script+=morphologyVarName+"="+"\""+morphologyArg+"\";";
if(tags.empty()==false) script+=tagsVarName+"="+"\""+tags+"\";";
script+=functorDef+functionName+" \""+functionName+"\" \""+parameterList+"\" "+argStr+";";
}
arguments.clear();
}
return script;
}