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

91
LINES

< > BotCompany Repo | #1007960 // englishToConceptLanguage_xyz [enabling "*"]

JavaX fragment (include)

sbool englishToConceptLanguage_xyz_debug, englishToConceptLanguage_useBrackets = true;

static new ThreadLocal<Int> englishToConceptLanguage_xyz_level;

sS englishToConceptLanguage_xyz(S s, O preprocess) {
  assertNotNull("Input", s);
  /*int level = englishToConceptLanguage_xyz_level.get();
  if (level >= englishToConceptLanguage_xyz_maxLevel) fail("max level");
  englishToConceptLanguage_xyz_level.set(level+1);
  try {*/
  s = postProcess(preprocess, s);
  
  // pattern matching all concepts against full string
  for (AIConcept c : englishToConceptLanguage_concepts()) {
    if (empty(c.name)) {
      print("Warning, empty name: " + c.globalID);
      continue;
    }
    S name = postProcess(preprocess, c.name);
    try answer englishToConceptLanguage_xyz_with(c.globalID, name, s);
  }
  
  // no full string match. go word by word
  L<S> tok = javaTok(s);
  if (l(tok) <= 3) null;
  for (int i = 1; i < l(tok); i += 2) {
    S x = englishToConceptLanguage(unquote(tok.get(i)));
    if (nempty(x)) tok.set(i, conceptQuote(x));
  }
  S x = join(tok);
  ret eq(x, s) ? null : x;
}

sS englishToConceptLanguage_xyz_sub(S name, S original, S s) {
  if (l(s) >= l(original)) fail("Bad sub: " + quote(name) + " " + quote(s));
  S s2 = or2(englishToConceptLanguage(s), s);
  if (englishToConceptLanguage_xyz_debug)
    print("xyz_sub " + quote(s) + " => " + quote(s2));
  if (englishToConceptLanguage_useBrackets)
    ret isIdentifier(s2) || isInteger(s2) || isProperlyQuoted(s2) ? s2 : "(" + s2 + ")";
  ret conceptQuote(s2);
}

sS englishToConceptLanguage_xyz_with(S id, S s) {
  ret englishToConceptLanguage_xyz_with(getAIConcept(id), s);
}

sS englishToConceptLanguage_xyz_with(AIConcept c, S s) {
  if (c == null) null;
  ret englishToConceptLanguage_xyz_with(c.globalID, c.name, s);
}

sS englishToConceptLanguage_xyz_with(S id, S name, S s) {
  new Matches m;
  L<S> originalTok = javaTok(name), tok = cloneList(originalTok);
  if (l(tok) < 5) null; // need 2 code tokens
  /*if (englishToConceptLanguage_xyz_debug)
    print("xyz name " + name + " originalTok1 " + struct(originalTok));*/
  bool stars = tok.contains("*");
  if (stars)
    tok = replace(tok, "*", "**");
  /*if (englishToConceptLanguage_xyz_debug)
    print("xyz name " + name + " originalTok2 " + struct(originalTok));*/
  int n = numberOfXYZVars(tok);
  if (n == 0) null;
  tok = formatXYZ(tok, rep(n, "*"));
  L<S> toks = javaTok_cached(s);
  if (stars)
    toks = replace(cloneList(toks), "*", "**");
  bool yes = flexMatchIC2(tok, toks, m, false);
  if (englishToConceptLanguage_xyz_debug && yes)
    print("xyz: " + n + " " + struct(tok) + " - " + struct(toks) + " => " + (yes ? struct(m) : "-"));
  if (!yes) null;
  if (n != l(m.m)) null;
  new TreeMap<Int, S> map;
  if (englishToConceptLanguage_xyz_debug)
    print("xyz originalTok " + struct(originalTok));
  for (int i = 1; i < l(originalTok); i += 2) {
    S t = originalTok.get(i);
    int x = xyzVarToIndex(t);
    if (englishToConceptLanguage_xyz_debug)
      print("xyz " + t + " => " + x);
    if (x != 0) {
      S sub = englishToConceptLanguage_xyz_sub(name, s, m.m[x-1]);
      if (englishToConceptLanguage_xyz_debug)
        print("xyz sub " + t + ": " + sub);
      map.put(i, sub);
    }
  }
  ret aiUsing(id) + " " + join(" ", values(map));
}

Author comment

Began life as a copy of #1007880

download  show line numbers  debug dex  old transpilations   

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

No comments. add comment

Snippet ID: #1007960
Snippet name: englishToConceptLanguage_xyz [enabling "*"]
Eternal ID of this version: #1007960/15
Text MD5: b7d7534f940c4943c9606e2319d92022
Author: stefan
Category: javax / a.i.
Type: JavaX fragment (include)
Public (visible to everyone): Yes
Archived (hidden from active list): No
Created/modified: 2017-04-17 21:45:31
Source code size: 3388 bytes / 91 lines
Pitched / IR pitched: No / No
Views / Downloads: 554 / 1260
Version history: 14 change(s)
Referenced in: #1007956 - Thought Bubble 1 prelude 3: Parsing test with precedence [WORKS]
#1007962 - Thought Bubble 1 prelude 4: Parsing test with brackets
#1007966 - englishToConceptLanguage_multi [multiple outs, dev.]
#1007968 - Thought Bubble 1 prelude 4b: _multi
#1007970 - Thought Bubble 1 prelude 5: englishToLisp_multi