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

121
LINES

< > BotCompany Repo | #1007424 // englishToConceptLanguage_xyz (old)

JavaX fragment (include) [tags: archive]

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> tok = javaTokC(name);
  //replace(tok, "*", "\\*");
  if (tok.contains("*")) null;
  int iX = tok.indexOf("X");
  if (iX >= 0) {
    int iY = tok.indexOf("Y");
    if (iY >= 0) {
      int iZ = tok.indexOf("Z");
      if (iZ >= 0) {
        int iAA = tok.indexOf("AA");
        if (iAA >= 0) {
          // X, Y, Z and AA
          S pat = formatXYZ(name, "*", "*", "*", "*");
          bool yes = flexMatchIC2(pat, s, m, false);
          if (englishToConceptLanguage_xyz_debug && yes)
            print("xyz: " + pat + " - " + s + " => " + (yes ? struct(m) : "-"));
          if (yes) {
            assertEquals(name, 4, l(m.m));
            new TreeMap<Int, S> map;
            map.put(iX, englishToConceptLanguage_xyz_sub(name, s, m.m[0]));
            map.put(iY, englishToConceptLanguage_xyz_sub(name, s, m.m[1]));
            map.put(iZ, englishToConceptLanguage_xyz_sub(name, s, m.m[2]));
            map.put(iAA, englishToConceptLanguage_xyz_sub(name, s, m.m[3]));
            ret aiUsing(id) + " " + join(" ", values(map));
          }
        } else {
          // X and Y and Z
          S pat = formatXYZ(name, "*", "*", "*");
          bool yes = flexMatchIC2(pat, s, m, false);
          if (englishToConceptLanguage_xyz_debug && yes)
            print("xyz: " + pat + " - " + s + " => " + (yes ? struct(m) : "-"));
          if (yes) {
            assertEquals(name, 3, l(m.m));
            new TreeMap<Int, S> map;
            map.put(iX, englishToConceptLanguage_xyz_sub(name, s, m.m[0]));
            map.put(iY, englishToConceptLanguage_xyz_sub(name, s, m.m[1]));
            map.put(iZ, englishToConceptLanguage_xyz_sub(name, s, m.m[2]));
            ret aiUsing(id) + " " + join(" ", values(map));
          }
        }
      } else {
        // X and Y
        S pat = formatXYZ(name, "*", "*");
        bool complicated = containsSubList(tok, "X", "Y");
        bool yes = complicated ? jmatch(pat, s, m) : flexMatchIC2(pat, s, m, false);
        if (englishToConceptLanguage_xyz_debug && yes)
          print("xyz: " + pat + " - " + s + " => " + (yes ? struct(m) : "-"));
        if (yes) {
          assertEquals(name, 2, l(m.m));
          S a = englishToConceptLanguage_xyz_sub(name, s, m.m[0]);
          S b = englishToConceptLanguage_xyz_sub(name, s, m.m[1]);
          ret aiUsing(id) + " " +
            (iX < iY ? a + " " + b : b + " " + a);
        }
      }
    } else if (l(tok) > 1) {
      // X only
      S pat = formatXYZ(name, "*");
      if (flexMatchIC2(pat, s, m, false)) {
        assertEquals(name, 1, l(m.m));
        S a = englishToConceptLanguage_xyz_sub(name, s, m.m[0]);
        ret aiUsing(id) + " " + a;
      }
    }
  }
  null;
}

download  show line numbers  debug dex  old transpilations   

Travelled to 3 computer(s): cfunsshuasjs, mqqgnosmbjvj, tvejysmllsmz

No comments. add comment

Snippet ID: #1007424
Snippet name: englishToConceptLanguage_xyz (old)
Eternal ID of this version: #1007424/51
Text MD5: ac6046a8c073d01c9e3ebd63bde25704
Author: stefan
Category: javax / a.i.
Type: JavaX fragment (include)
Public (visible to everyone): Yes
Archived (hidden from active list): Yes
Created/modified: 2017-04-13 17:31:55
Source code size: 4684 bytes / 121 lines
Pitched / IR pitched: No / No
Views / Downloads: 420 / 330
Version history: 50 change(s)
Referenced in: [show references]