-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtranscriptor.cpp
79 lines (72 loc) · 2.59 KB
/
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
#include "transcriptor.h"
transcriptor::transcriptor(const char *analyses){
jsondoc.Parse(analyses);
}
void transcriptor::find_replace(std::string& findrep,const std::string& search_for,const std::string& replace_with){
//Avoiding replacement of substrings equal to replace_with works only if search_for is not present more than once in replace_with
size_t pos=findrep.find(search_for);
size_t pos_substr=replace_with.find(search_for);
while(pos!=std::string::npos){
if(pos_substr!=std::string::npos&&pos>=pos_substr){
std::string substr=findrep.substr(pos-pos_substr,pos_substr+1);
if(substr==replace_with){
pos=findrep.find(search_for,++pos);
}
else{
findrep.replace(pos,search_for.size(),replace_with);
pos=findrep.find(search_for,pos+replace_with.size());
}
}
else{
findrep.replace(pos,search_for.size(),replace_with);
pos=findrep.find(search_for,pos+replace_with.size());
}
}
}
std::string transcriptor::value_to_string(rapidjson::Value& jsonvalue){
rapidjson::StringBuffer buffer;
rapidjson::Writer<rapidjson::StringBuffer> writer(buffer);
jsonvalue.Accept(writer);
return buffer.GetString();
}
std::string transcriptor::transcribe(){
std::string script;
std::vector<std::string> arguments;
if(jsondoc.HasMember("analyses")==true){
rapidjson::Value& analysesArray = jsondoc["analyses"];
if(analysesArray.IsArray()==true&&analysesArray.Size()>0){
rapidjson::Value& analysis=analysesArray[0];//analyses are ranked, the first is the best
rapidjson::Value& morphology=analysis["morphology"];
rapidjson::Value syntax;
if(analysis.HasMember("syntax")==true){
syntax=analysis["syntax"];
}
rapidjson::Value relatedSemantics;
if(analysis.HasMember("related semantics")==true){
relatedSemantics=analysis["related semantics"];
}
rapidjson::Value functors;
if(analysis.HasMember("functors")==true){
functors=analysis["functors"];
}
if(analysis.HasMember("semantics")==true&&analysis.HasMember("errors")==false){
rapidjson::Value& semantics=analysis["semantics"];
if(semantics.Size()>0){
script=transcribeDependencies(morphology,syntax,semantics,functors,arguments,true);
if(analysis.HasMember("analysis_deps")==true){
rapidjson::Value& analysis_deps=analysis["analysis_deps"];
std::string analysis_deps_escaped=value_to_string(analysis_deps);
find_replace(analysis_deps_escaped,"\"","\\\"");
find_replace(analysis_deps_escaped,"\'","\\\'");
script="analysis_deps='{\\\"dependencies\\\":"+analysis_deps_escaped+"}';"+script;
}
}
else{
}
}
else{
}
}
}
return script;
}