-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathNLPTemplateEngine.m
More file actions
730 lines (538 loc) · 27.2 KB
/
NLPTemplateEngine.m
File metadata and controls
730 lines (538 loc) · 27.2 KB
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
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
(*
NLP Template Engine Mathematica package
Copyright (C) 2021 Anton Antonov
This program is free software: you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with this program. If not, see <http://www.gnu.org/licenses/>.
Written by Anton Antonov,
antononcube @ posteo . net,
Windermere, Florida, USA.
*)
(*
Mathematica is (C) Copyright 1988-2021 Wolfram Research, Inc.
Protected by copyright law and international treaties.
Unauthorized reproduction or distribution subject to severe civil
and criminal penalties.
Mathematica is a registered trademark of Wolfram Research, Inc.
*)
(* Created by the Wolfram Language Plugin for IntelliJ, see http://wlplugin.halirutan.de/ *)
(* :Title: NLPTemplateEngine *)
(* :Context: NLPTemplateEngine` *)
(* :Author: Anton Antonov *)
(* :Date: 2021-07-19 *)
(* :Package Version: 0.5 *)
(* :Mathematica Version: 12.3 *)
(* :Copyright: (c) 2021 Anton Antonov *)
(* :Keywords: *)
(* :Discussion:
## Problem formulation
We want to have a system that:
- Generates relevant, correct, executable programming code based natural language specifications of computational workflows
- Can automatically recognize the workflow types
- Can generate code for different programming languages and related software packages
The points above are given in order of importance; the most important are placed first.
## Examples
Consider the following -- intentionally short and non-specific -- computational workflow specifications:
```mathematica
lsCommands = {
"Create a random dataset.",
"Do quantile regression over findData.",
"Make a classifier over dsTitanic."};
```
Here we generate the code -- note the only the list of commands is given to the function Concretize, [AAp1]:
```mathematica
aRes = Concretize[lsCommands];
```
Here we tabulate the code generation (templates fill-in) results:
```mathematica
ResourceFunction["GridTableForm"][List @@@ Normal[aRes], TableHeadings -> {"Spec", "Code"}]
```
## References
[AAp1] Anton Antonov,
Computational workflow type classifier Mathematica package,
(2021),
[NLP-Template-Engine at GitHub/antononcube](https://github.com/antononcube/NLP-Template-Engine).
*)
(***********************************************************)
(* Load packages *)
(***********************************************************)
If[ Length[DownValues[MonadicContextualClassification`ClConUnit]] == 0,
Echo["MonadicContextualClassification.m", "Importing from GitHub:"];
Import["https://raw.githubusercontent.com/antononcube/MathematicaForPrediction/master/MonadicProgramming/MonadicContextualClassification.m"];
];
If[ Length[DownValues[MonadicQuantileRegression`QRMonUnit]] == 0,
Echo["MonadicQuantileRegression.m", "Importing from GitHub:"];
Import["https://raw.githubusercontent.com/antononcube/MathematicaForPrediction/master/MonadicProgramming/MonadicQuantileRegression.m"];
];
If[ Length[DownValues[MonadicStructuralBreaksFinder`QRMonFindChowTestLocalMaxima]] == 0,
Echo["MonadicStructuralBreaksFinder.m", "Importing from GitHub:"];
Import["https://raw.githubusercontent.com/antononcube/MathematicaForPrediction/master/MonadicProgramming/MonadicStructuralBreaksFinder.m"];
];
If[ Length[DownValues[MonadicLatentSemanticAnalysis`LSAMonUnit]] == 0,
Echo["MonadicLatentSemanticAnalysis.m", "Importing from GitHub:"];
Import["https://raw.githubusercontent.com/antononcube/MathematicaForPrediction/master/MonadicProgramming/MonadicLatentSemanticAnalysis.m"];
];
If[ Length[DownValues[MonadicSparseMatrixRecommender`SMRMonUnit]] == 0,
Echo["MonadicSparseMatrixRecommender.m", "Importing from GitHub:"];
Import["https://raw.githubusercontent.com/antononcube/MathematicaForPrediction/master/MonadicProgramming/MonadicSparseMatrixRecommender.m"];
];
(*If[ Length[DownValues[MonadicEpidemiologyCompartmentalModeling`ECMMonUnit]] == 0,*)
(* Echo["MonadicEpidemiologyCompartmentalModeling.m", "Importing from GitHub:"];*)
(* Import["https://raw.githubusercontent.com/antononcube/SystemModeling/master/Projects/Coronavirus-propagation-dynamics/WL/MonadicEpidemiologyCompartmentalModeling.m"];*)
(*];*)
If[Length[DownValues[NLPTemplateEngineData`NLPTemplateEngineData]] == 0,
Echo["NLPTemplateEngineData.m", "Importing from GitHub:"];
Import["https://raw.githubusercontent.com/antononcube/NLP-Template-Engine/main/Packages/WL/NLPTemplateEngineData.m"];
];
(***********************************************************)
(* Package definition *)
(***********************************************************)
BeginPackage["NLPTemplateEngine`"];
(* Exported symbols added here with SymbolName::usage *)
GetRawAnswers::usage = "GetRawAnswers[wfSpec, spec, opts] \
finds probability-scored answers of parameters questions for the computational workflow template wfSpec \
over the computational specification spec.";
GetAnswers::usage = "GetAnswers[wfSpec, spec, opts] \
finds precise answers of parameters questions for the computational workflow template wfSpec \
over the computational specification spec.";
Concretize::usage = "Concretize[ttype : (_String | Automatic), spec_String, opts] \
finds parameter values to fill-in the slots for the ttype template based on the natural language specification spec \
and creates corresponding executable expression. \
If ttype is Automatic then a dedicated classifier function used to guess the template type.";
NLPTemplateEngineAddData::usage = "NLPTemplateEngineAddData[dsSpecs_Dataset] \
adds the template engine data specifications dsSpecs into Concretize[\"Data\"].";
NLPTemplateEngineReplaceData::usage = "NLPTemplateEngineReplaceData[dsSpecs_Dataset] \
replaces the data in Concretize[\"Data\"] with template engine data specifications dsSpecs.";
ConvertCSVData::usage = "ConvertCSVData is an internal function.";
ConvertCSVDataForType::usage = "ConvertCSVDataForType is an internal function.";
Begin["`Private`"];
Needs["NLPTemplateEngineData`"];
(***********************************************************)
(* GetRawAnswers *)
(***********************************************************)
Clear[GetRawAnswers];
GetRawAnswers::nwft =
"The first argument is an unknown workflow type. The first argument is expected to be one of `1`.";
Options[GetRawAnswers] = Options[FindTextualAnswer];
GetRawAnswers[workflowTypeArg_String, command_String, nAnswers_Integer : 4, opts : OptionsPattern[]] :=
Block[{workflowType = workflowTypeArg, aShortcuts, aQuestions, aRes},
aShortcuts = Concretize["Data"]["Shortcuts"];
aQuestions = Concretize["Data"]["Questions"];
workflowType = workflowType /. aShortcuts;
If[! MemberQ[Values[aShortcuts], workflowType],
Message[GetRawAnswers::nwft, Union[Keys[aShortcuts], Values[aShortcuts]]];
Return[$Failed]
];
aRes =
Association@
Map[# ->
FindTextualAnswer[command, #, nAnswers, {"String", "Probability"},
opts] &, Keys@aQuestions[workflowType]];
Map[Association[Rule @@@ #] &, aRes]
];
(***********************************************************)
(* GetAnswers *)
(***********************************************************)
ClearAll[TakeLargestKey];
TakeLargestKey[args__] := StringTrim@First@Keys@TakeLargest[args];
ClearAll[RemoveContextWords];
RemoveContextWords[s : (_String | {_String ..}), {} ] := s;
RemoveContextWords[s : (_String | {_String ..}), words : {_String..} ] :=
StringTrim[StringReplace[ s, Thread[Thread[StringExpression[WordBoundary, words, WordBoundary]] -> ""], IgnoreCase -> True ]];
RemoveContextWords[s_, args___] := (Message[GetAnswers::rw];s);
Clear[GetAnswers];
GetAnswers::ncrw = "Problematic \"ContextWordsToRemove\" specification";
Options[GetAnswers] = Join[ Options[GetRawAnswers], {"RemoveByThreshold" -> True}];
GetAnswers[workflowTypeArg_String, command_String, nAnswers_Integer : 4, opts : OptionsPattern[]] :=
Block[{workflowType = workflowTypeArg, aShortcuts, aQuestions, aRes, aParameterQuestions, parVal},
aShortcuts = Concretize["Data"]["Shortcuts"];
aQuestions = Concretize["Data"]["Questions"];
(*
We have multiple questions for each parameter in order to capture relevant answers
from different types of phrasings.
We hope that the (1) multiplicity of questions and (2) passing threshold and redundant words per question
would suffice to get correct answers (most of the time.)
*)
(*"Normalize" the specified workflow type with replacement rules.*)
workflowType = workflowType /. aShortcuts;
(*Get raw answers.*)
(*For each question we get an association of scored answers. The answers are keys. *)
aRes = GetRawAnswers[workflowType, command, nAnswers, FilterRules[{opts}, Options[GetRawAnswers]]];
If[TrueQ[aRes === $Failed],
Return[$Failed]
];
(*Filter out candidates with too low probabilities.*)
(*For each question apply the threshold filtering.*)
If[ TrueQ[OptionValue[GetAnswers, "RemoveByThreshold"]],
aRes =
Association@
KeyValueMap[
Function[{k, v},
k -> Select[v, # >= aQuestions[workflowType, k, "Threshold"] &]], aRes];
aRes = Select[aRes, Length[#] > 0 &];
];
(*Remove specified "redundant" words.*)
(*For each question and each key/answer replace redundant words with empty string.*)
aRes =
Association@
KeyValueMap[
Function[{k, v},
k ->
KeySelect[
KeyMap[RemoveContextWords[#, Lookup[aQuestions[workflowType, k], "ContextWordsToRemove", {}]]&, v],
StringLength[#] > 0&
]
], aRes];
aRes = Select[aRes, Length[#] > 0 &];
(*Group the questions per parameter.*)
aParameterQuestions = GroupBy[aQuestions[workflowType], #["Parameter"] &, Keys];
(*aParameterCandidateValues=Map[Merge[Values@KeyTake[aRes,#],Max]&, aParameterQuestions];*)
(*For each parameter and each question of that parameter extract the top candidate parameter value and associated probability.*)
aRes =
Map[
KeyValueMap[
Function[{k, v},
parVal =
Switch[
ToString[aQuestions[workflowType, k, "TypePattern"]],
"{_String..}",
Map["\"" <> # <> "\"" &,
Select[StringTrim[StringSplit[TakeLargestKey[v, 1], {",", "and"}]], StringLength[#] > 0 &]],
"{_?NumericQ..}" | "{_Integer..}",
ToExpression /@ Select[StringTrim[StringSplit[TakeLargestKey[v, 1], {",", "and"}]], StringLength[#] > 0&],
"_?NumericQ" | "_Integer" | "_Symbol",
ToExpression[First @ Select[StringTrim[StringSplit[TakeLargestKey[v, 1], {",", "and", Whitespace}]], StringLength[#] > 0&]],
"_?BooleanQ" | "(True|False)" | "(False|True)",
MemberQ[
If[KeyExistsQ[aQuestions[workflowType, k], "TrueValues"], aQuestions[workflowType, k, "TrueValues"], {"true", "false"}],
ToLowerCase[TakeLargestKey[v, 1]]
],
_,
TakeLargestKey[v, 1]
];
parVal -> TakeLargest[v, 1][[1]]
],
KeyTake[aRes, Flatten[{#}]]
] &,
aParameterQuestions
];
(*For each parameter take the parameter value candidate with the largest probability *)
aRes = TakeLargestBy[#, #[[2]] &, 1][[1]] & /@ Select[aRes, Length[#] > 0 &];
(*Drop the associated probabilities*)
Keys /@ aRes
];
(***********************************************************)
(* Concretize *)
(***********************************************************)
Clear[Concretize];
SyntaxInformation[Concretize] = {"ArgumentsPattern" -> {_, _., OptionsPattern[]}};
Options[Concretize] =
Join[
Options[GetAnswers],
{"TargetLanguage" -> "WL",
"AvoidMonads" -> False,
"AssociationResult" -> False,
"UserID" -> None,
"CopyToClipboard" -> True }
];
Concretize::tlang = "The value of the option \"TargetLanguage\" is expected to be one of `1`.";
Concretize::aulang = "The automatic programming language detection failed. Continuing by using \"WL\".";
Concretize::nargs = "If one argument is given then the first argument is expected to be a string or a list of strings. \
If two arguments are given then the first argument is expected to be a classifier function or Automatic, \
and the second argument is expected to be a string or a list of strings.";
Concretize["Data"] := NLPTemplateEngineData`NLPTemplateEngineData["Standard"];
Concretize["Templates"] := Concretize["Data"]["Templates"];
Concretize["Questions"] := Concretize["Data"]["Questions"];
Concretize["Defaults"] := Concretize["Data"]["Defaults"];
Concretize["Shortcuts"] := Concretize["Data"]["Shortcuts"];
Concretize[ commands : ( _String | {_String..} ), opts : OptionsPattern[]] :=
Concretize[Automatic, commands, opts];
Concretize[ sf : (Automatic | _ClassifierFunction | _String), commands : {_String..}, opts : OptionsPattern[]] :=
Association @ Map[ # -> Concretize[sf, #, opts]&, commands];
Concretize[Automatic, command_String, opts : OptionsPattern[]] :=
Block[{cf},
If[Length[DownValues[ComputationalWorkflowTypeClassifier`GetComputationalWorkflowTypeClassifier]] == 0,
Echo["ComputationalWorkflowTypeClassifier.m", "Importing from GitHub:"];
Import["https://raw.githubusercontent.com/antononcube/NLP-Template-Engine/main/Packages/WL/ComputationalWorkflowTypeClassifier.m"];
];
cf = ComputationalWorkflowTypeClassifier`GetComputationalWorkflowTypeClassifier[];
If[ TrueQ[OptionValue[Concretize, "AvoidMonads"]],
Concretize[ cf[command], command, opts],
(*ELSE*)
Concretize[ cf[command] /. {"Classification" -> "ClCon", "QuantileRegression" -> "QRMon"}, command, opts]
]
];
Concretize[cf_ClassifierFunction, command_String, opts : OptionsPattern[]] :=
Concretize[ cf[command], command, opts];
Concretize[workflowTypeArg_String, command_String, opts : OptionsPattern[]] :=
Block[{workflowType = workflowTypeArg,
aQuestions, aTemplates, aDefaults, aShortcuts,
lsKnownLanguages, userID, lang, cpcbQ, aRes, code, codeExpr, res},
{aQuestions, aTemplates, aDefaults, aShortcuts} = Values @ KeyTake[ Concretize["Data"], {"Questions", "Templates", "Defaults", "Shortcuts"}];
lsKnownLanguages = Union @ Flatten @ Values[ Keys /@ aTemplates ];
(* User ID *)
userID = OptionValue[Concretize, "UserID"];
If[ MemberQ[{None, Automatic}, userID], userID = ""];
userID = ToString[userID];
(* Programming language *)
lang = OptionValue[Concretize, "TargetLanguage"];
If[ TrueQ[lang === Automatic],
aRes = Join[aDefaults["ProgrammingEnvironment"], GetAnswers["ProgrammingEnvironment", command]];
lang =
Which[
Length[StringCases[ aRes["Language"], WordBoundary ~~ ("WL" | "Mathematica") ~~ WordBoundary]] > 0, "WL",
Length[StringCases[ aRes["Language"], WordBoundary ~~ ("R") ~~ WordBoundary]] > 0, "R",
True,
Message[Concretize::aulang];
"WL"
]
];
If[ TrueQ[StringQ[lang] && ToLowerCase[lang] == "mathematica"], lang = "WL"];
If[ !StringQ[lang] || !MemberQ[ ToUpperCase[lsKnownLanguages], ToUpperCase[lang] ],
Message[Concretize::tlang, Append[lsKnownLanguages, Automatic]];
lang = "WL"
];
(*CopyToClipboard*)
cpcbQ = TrueQ[OptionValue[Concretize, "CopyToClipboard"]];
(*Translation*)
workflowType = workflowType /. aShortcuts;
aRes = GetAnswers[workflowType, command, FilterRules[{opts}, Options[GetAnswers]]];
If[TrueQ[aRes === $Failed],
Return[$Failed]
];
code = Concretize["Templates"][workflowType][lang][Join[aDefaults[workflowType], aRes]];
Which[
lang == "WL",
codeExpr = ToExpression["Hold[" <> code <> "]"],
lang == "R",
code =
StringReplace[
code,
{
WordBoundary ~~ "Automatic" ~~ WordBoundary -> "NULL",
WordBoundary ~~ "True" ~~ WordBoundary -> "TRUE",
WordBoundary ~~ "False" ~~ WordBoundary -> "FALSE",
"{" ~~ x : (Except[Characters["{}"]]..) ~~ "}" :> "c(" <> x <> ")"
}];
codeExpr = "parse( text = '" <> code <> "')",
lang == "Python",
code =
StringReplace[
code,
{
WordBoundary ~~ "Automatic" ~~ WordBoundary -> "None",
"{" ~~ x : (Except[Characters["{}"]]..) ~~ "}" :> "[" <> x <> "]"
}];
codeExpr = code,
lang == "Raku",
code =
StringReplace[
code,
{
WordBoundary ~~ "Automatic" ~~ WordBoundary -> "Whatever",
"{" ~~ x : (Except[Characters["{}"]]..) ~~ "}" :> "[" <> x <> "]"
}];
codeExpr = code,
True,
codeExpr = code
];
res =
If[ TrueQ[OptionValue[Concretize, "AssociationResult"]],
<|
"CODE" -> code,
"USERID" -> userID,
"DSLTARGET" -> lang <> "::" <> workflowType,
"DSL" -> workflowType,
"DSLFUNCTION" -> With[{wf = workflowType, l = lang, am = TrueQ[OptionValue[Concretize, "AvoidMonads"]]},
ToString[Concretize[wf, #,
"TargetLanguage" -> l,
"AvoidMonads" -> am,
"AssociationResult" -> True]&]
]
|>,
(*ELSE*)
codeExpr
];
If[cpcbQ, CopyToClipboard[res]];
res
];
Concretize[___] :=
Block[{},
Message[Concretize::nargs];
$Failed
];
(***********************************************************)
(* Introspection data preparation functions *)
(***********************************************************)
Clear[ToWorkflowTemplate];
ToWorkflowTemplate[spec_String] := Concretize["Templates"][spec]["WL"][Concretize["Defaults"][spec]];
ToWorkflowTemplate[x_] := x;
Clear[SplitWorkflowType];
SplitWorkflowType[name_String] := StringRiffle[StringCases[name, CharacterRange["A", "Z"] ~~ (CharacterRange["a", "z"] ..)]];
SplitWorkflowType[name_String] := StringRiffle[{StringReplace[name, "Mon" -> ""], "monad"}] /; StringMatchQ[name, __ ~~ "Mon"];
SplitWorkflowType["ClCon"] := "Classification monad";
(***********************************************************)
(* Introspection data *)
(***********************************************************)
lsWorkflowTypes = Keys[Concretize["Templates"]];
txtWorkflowTypes = StringRiffle[lsWorkflowTypes, ", "];
txtSQASDescription1 = "The current specialized Question Answering System (QAS) has the workflow types " <> txtWorkflowTypes <> ".";
txtSQASDescription1 =
txtSQASDescription1 <> "\nThe known workflow types are " <> txtWorkflowTypes <> ".";
txtSQASDescription1 =
txtSQASDescription1 <> "\nThe implemented workflow template types are " <> txtWorkflowTypes <> ".";
txtSQASDescription1 =
txtSQASDescription1 <> "\nThe number of implemented workflow templates is " <> ToString[Length@lsWorkflowTypes] <> ".";
txtSQASDescription1 =
txtSQASDescription1 <> "\nThere are " <> ToString[Length@lsWorkflowTypes] <> " workflows.";
txtSQASDescription1 =
txtSQASDescription1 <> "\nThere are " <> ToString[Length@lsWorkflowTypes] <> " templates per programming language.";
txtSQASDescription2 =
Map["The template for " <> # <> " is: WorkflowTemplate-" <> # <> "." &, Keys[Concretize["Templates"]]];
txtSQASDescription2 =
Join[
txtSQASDescription2,
Map["The template for " <> SplitWorkflowType[#] <> " is: WorkflowTemplate-" <> # <> "." &, Keys[Concretize["Templates"]]]
];
txtSQASDescription2 =
Join[
txtSQASDescription2,
Map["The " <> SplitWorkflowType[#] <> " workflow template is: WorkflowTemplate-" <> # <> "." &, Keys[Concretize["Templates"]]]
];
txtSQASDescription2 =
Join[
txtSQASDescription2,
StringReplace[Select[txtSQASDescription2, Length[StringCases[#, "LatentSemanticAnalysis"]] > 0 &], WhitespaceCharacter ~~ "LatentSemanticAnalysis" ~~ WhitespaceCharacter -> " LSAMon "],
StringReplace[Select[txtSQASDescription2, Length[StringCases[#, "Recommendations"]] > 0 &], WhitespaceCharacter ~~ "Recommendations" ~~ WhitespaceCharacter -> " SMRMon "]
];
txtSQASDescription2 = StringRiffle[Union[txtSQASDescription2], "\n"];
txtSQASDescription = StringJoin[txtSQASDescription1, "\n", txtSQASDescription2];
(***********************************************************)
(* Concretize continued *)
(***********************************************************)
Concretize["Introspection", query_String, opts : OptionsPattern[]] :=
Block[{res},
res = FindTextualAnswer[txtSQASDescription, query, FilterRules[{opts}, Options[FindTextualAnswer]]];
res =
StringReplace[
res,
"WorkflowTemplate-" ~~ (x : (LetterCharacter..)) ~~ WordBoundary :> Concretize["Templates"][x]["WL"][Concretize["Defaults"][x]]
];
res
];
(***********************************************************)
(* Convert CSV data into associations *)
(***********************************************************)
Clear[ConvertCSVData];
ConvertCSVData::cnames = "The first argument is expected to be a dataset with column names `1`.";
ConvertCSVData[ dsTESpecs_Dataset ] :=
Block[{lsExpectedColumnNames},
(* Check correctness of the dataset. *)
lsExpectedColumnNames = {"DataType", "WorkflowType", "Group", "Key", "Value"};
If[ Length[Intersection[Normal@Keys@First@dsTESpecs, lsExpectedColumnNames]] < Length[lsExpectedColumnNames],
Message[ConvertCSVData::cnames, ToString[lsExpectedColumnNames]];
Return[$Failed]
];
Association @ Map[ # -> ConvertCSVDataForType[ dsTESpecs, #]&, {"Questions", "Templates", "Defaults", "Shortcuts"}]
];
Clear[ConvertCSVDataForType];
ConvertCSVDataForType[ dsTESpecs_Dataset, dataType : "Questions" ] :=
Block[{dsQuery, aRes},
dsQuery = dsTESpecs[Select[#DataType == dataType&]];
If[ Length[dsQuery] == 0, Return[<||>]];
dsQuery = dsQuery[ All, If[ #Key == "ContextWordsToRemove", Append[#, "Value" -> ToExpression[#Value]], #]&];
dsQuery = Normal[dsQuery[Values]];
aRes = ResourceFunction["AssociationKeyDeflatten"][ Map[ Most[#] -> Last[#]&, dsQuery] ];
aRes[dataType]
];
ConvertCSVDataForType[ dsTESpecs_Dataset, dataType : "Templates" ] :=
Block[{dsQuery, aRes},
dsQuery = dsTESpecs[Select[#DataType == dataType&]];
If[ Length[dsQuery] == 0, Return[<||>]];
(* We drop the "Key" column that has to have "Template" value,
since that column was added to fit the global long-format CSV. *)
dsQuery = dsQuery[ All, KeyDrop[#, "Key"]&];
dsQuery = Normal[dsQuery[Values]];
aRes = ResourceFunction["AssociationKeyDeflatten"][ Map[ Most[#] -> If[ StringQ[Last[#]], ToExpression[Last[#]], Last[#]]&, dsQuery] ];
aRes[dataType]
];
ConvertCSVDataForType[ dsTESpecs_Dataset, dataType : "Templates" ] :=
Block[{dsQuery, ToTemplateExpression, aRes},
dsQuery = dsTESpecs[Select[#DataType == dataType&]];
If[ Length[dsQuery] == 0, Return[<||>]];
(* We drop the "Key" column that has to have "Template" value,
since that column was added to fit the global long-format CSV. *)
dsQuery = dsQuery[ All, KeyDrop[#, "Key"]&];
dsQuery = Normal[dsQuery[Values]];
ToTemplateExpression = If[ StringMatchQ[ #, StartOfString ~~ "TemplateObject[" ~~ __], ToExpression[#], StringTemplate[#]]&;
aRes = ResourceFunction["AssociationKeyDeflatten"][ Map[ Most[#] -> ToTemplateExpression[Last[#]] &, dsQuery] ];
aRes[dataType]
];
ConvertCSVDataForType[ dsTESpecs_Dataset, dataType : "Defaults" ] :=
Block[{dsQuery, aRes},
dsQuery = dsTESpecs[Select[#DataType == dataType&]];
If[ Length[dsQuery] == 0, Return[<||>]];
(* We drop the "Group" column that has to have "All" value,
since that column was added to fit the global long-format CSV. *)
(* Meaning, currently the defaults depend only from the workflow type,
not the workflow type and the target language.*)
dsQuery = dsQuery[ All, KeyDrop[#, "Group"]&];
dsQuery = Normal[dsQuery[Values]];
aRes = ResourceFunction["AssociationKeyDeflatten"][ Map[ Most[#] -> ToExpression[Last[#]]&, dsQuery] ];
aRes[dataType]
];
ConvertCSVDataForType[ dsTESpecs_Dataset, dataType : "Shortcuts" ] :=
Block[{dsQuery},
dsQuery = dsTESpecs[Select[#DataType == dataType&]];
If[ Length[dsQuery] == 0, Return[<||>]];
(* We only need "Key" and "Value" for the shortcuts. *)
dsQuery = dsQuery[ All, {"Key", "Value"}];
Normal @ dsQuery[ Association, #Key -> #Value& ]
];
(***********************************************************)
(* Add template engine data *)
(***********************************************************)
Clear[NLPTemplateEngineAddData];
NLPTemplateEngineAddData::nargs = "The first argument is expected to be a dataset.";
NLPTemplateEngineAddData[ dsTESpecs_Dataset ] :=
Block[{aRes},
aRes = ConvertCSVData[dsTESpecs];
Map[
With[{k = #, ascGlobal = Concretize["Data"],
asc = Concretize["Data"][#], newAsc = aRes[#]},
Concretize["Data"] := Append[ascGlobal, k -> Join[asc, newAsc]]
] &,
Keys[aRes]
];
Concretize["Data"]
];
NLPTemplateEngineAddData[___] :=
Block[{},
Message[NLPTemplateEngineAddData::nargs];
$Failed
];
(***********************************************************)
(* Replace template engine data *)
(***********************************************************)
Clear[NLPTemplateEngineReplaceData];
NLPTemplateEngineReplaceData::nargs = "The first argument is expected to be a dataset.";
NLPTemplateEngineReplaceData[ dsTESpecs_Dataset ] :=
Block[{aRes},
aRes = ConvertCSVData[dsTESpecs];
(* Some verification is would be nice. *)
Concretize["Data"] = aRes
];
NLPTemplateEngineReplaceData[___] :=
Block[{},
Message[NLPTemplateEngineReplaceData::nargs];
$Failed
];
End[];
EndPackage[];