Not logged in.  Login/Logout/Register | List snippets | | Create snippet | Upload image | Upload data

76
LINES

< > BotCompany Repo | #1007880 // englishToConceptLanguage_xyz [LIVE]

JavaX fragment (include)

1  
sbool englishToConceptLanguage_xyz_debug, englishToConceptLanguage_useBrackets = true;
2  
sbool englishToConceptLanguage_unquote;
3  
4  
static new ThreadLocal<Int> englishToConceptLanguage_xyz_level;
5  
6  
sS englishToConceptLanguage_xyz(S s, O preprocess) {
7  
  assertNotNull("Input", s);
8  
  /*int level = englishToConceptLanguage_xyz_level.get();
9  
  if (level >= englishToConceptLanguage_xyz_maxLevel) fail("max level");
10  
  englishToConceptLanguage_xyz_level.set(level+1);
11  
  try {*/
12  
  s = postProcess(preprocess, s);
13  
  
14  
  // pattern matching all concepts against full string
15  
  for (AIConcept c : englishToConceptLanguage_concepts()) {
16  
    if (empty(c.name)) {
17  
      print("Warning, empty name: " + c.globalID);
18  
      continue;
19  
    }
20  
    S name = postProcess(preprocess, c.name);
21  
    try answer englishToConceptLanguage_xyz_with(c.globalID, name, s);
22  
  }
23  
  
24  
  // no full string match. go word by word
25  
  L<S> tok = javaTok(s);
26  
  if (l(tok) <= 3) null;
27  
  for (int i = 1; i < l(tok); i += 2) {
28  
    S x = englishToConceptLanguage(unquote(tok.get(i)));
29  
    if (nempty(x)) tok.set(i, conceptQuote(x));
30  
  }
31  
  S x = join(tok);
32  
  ret eq(x, s) ? null : x;
33  
}
34  
35  
sS englishToConceptLanguage_xyz_sub(S name, S original, S s) {
36  
  if (englishToConceptLanguage_unquote) s = unquote(s);
37  
  if (l(s) >= l(original)) fail("Bad sub: " + quote(name) + " " + quote(s));
38  
  S s2 = or2(englishToConceptLanguage(s), s);
39  
  if (englishToConceptLanguage_xyz_debug)
40  
    print("xyz_sub " + quote(s) + " => " + quote(s2));
41  
  if (englishToConceptLanguage_useBrackets)
42  
    ret isIdentifier(s2) || isInteger(s2) || isProperlyQuoted(s2) ? s2 : "(" + s2 + ")";
43  
  ret conceptQuote(s2);
44  
}
45  
46  
sS englishToConceptLanguage_xyz_with(S id, S s) {
47  
  ret englishToConceptLanguage_xyz_with(getAIConcept(id), s);
48  
}
49  
50  
sS englishToConceptLanguage_xyz_with(AIConcept c, S s) {
51  
  if (c == null) null;
52  
  ret englishToConceptLanguage_xyz_with(c.globalID, c.name, s);
53  
}
54  
55  
sS englishToConceptLanguage_xyz_with(S id, S name, S s) {
56  
  new Matches m;
57  
  L<S> tok = javaTokC(name);
58  
  if (l(tok) < 2) null;
59  
  //replace(tok, "*", "\\*");
60  
  if (tok.contains("*")) null;
61  
  int n = numberOfXYZVars_c(tok);
62  
  if (n == 0) null;
63  
  S pat = formatXYZ(name, rep(n, "*"));
64  
  bool yes = flexMatchIC2(pat, s, m, false);
65  
  if (englishToConceptLanguage_xyz_debug && yes)
66  
    print("xyz: " + n + " " + pat + " - " + s + " => " + (yes ? struct(m) : "-"));
67  
  if (!yes) null;
68  
  if (n != l(m.m)) null;
69  
  new TreeMap<Int, S> map;
70  
  for i over tok: {
71  
    int x = xyzVarToIndex(tok.get(i));
72  
    if (x != 0)
73  
      map.put(i, englishToConceptLanguage_xyz_sub(name, s, m.m[x-1]));
74  
  }
75  
  ret aiUsing(id) + " " + join(" ", values(map));
76  
}

Author comment

Began life as a copy of #1007424

download  show line numbers  debug dex  old transpilations   

Travelled to 13 computer(s): aoiabmzegqzx, bhatertpkbcr, cbybwowwnfue, cfunsshuasjs, gwrvuhgaqvyk, ishqpsrjomds, lpdgvwnxivlt, mqqgnosmbjvj, pyentgdyhuwx, pzhvpgtvlbxg, tslmcundralx, tvejysmllsmz, vouqrxazstgt

No comments. add comment

Snippet ID: #1007880
Snippet name: englishToConceptLanguage_xyz [LIVE]
Eternal ID of this version: #1007880/9
Text MD5: 4ac9d9c0dbfb9a828dcf927340c84f00
Author: stefan
Category: javax / a.i.
Type: JavaX fragment (include)
Public (visible to everyone): Yes
Archived (hidden from active list): No
Created/modified: 2017-07-16 18:33:02
Source code size: 2699 bytes / 76 lines
Pitched / IR pitched: No / No
Views / Downloads: 611 / 774
Version history: 8 change(s)
Referenced in: [show references]