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

199
LINES

< > BotCompany Repo | #1026126 // Rules + Inputs Web Bot

JavaX module (desktop) [tags: butter use-pretranspiled] - homepage

Download Jar. Libraryless. Click here for Pure Java version (17940L/127K).

!7

concept Rule > ConceptWithGlobalID {
  S input, output, variables /* comma-separated */, flags, comment;
  sS _fieldOrder = "globalID input output variables flags comment";
  
  Set<S> vars() { ret asCISet(tok_splitAtComma(variables)); }
  Set<S> flags() { ret asCISet(tok_splitAtComma(flags)); }
}

concept PossibleExample > ConceptWithGlobalID {
  Rule rule;
  S input, output;
  SS mapping;
  Bool good;
  sS _fieldOrder = "globalID rule good input output mapping";
}

concept Input > ConceptWithGlobalID {
  S input;
}

p {
  db();
  indexConceptFields(PossibleExample, 'rule);
  indexConceptFieldsCI(Input, 'input, Rule, 'input, PossibleExample, 'input);
  indexConceptFieldsOrdered(Rule, 'globalID);
}

static HCRUD rulesCRUD() {
  ret new HCRUD(rawLink("admin"), new HCRUD_Concepts<Rule>(Rule) {
    void massageItemMapForList(Rule r, MapSO map) {
      Cl<PossibleExample> examples = conceptsWhere(PossibleExample, rule := r);
      PossibleExample goodExample = random(objectsWhere(examples, good := true));
      map.put("Example Input" := goodExample != null ? goodExample.input : null);
      map.put("Example Output" := goodExample != null ? goodExample.output : null);
      map.put("Possible Examples" := l(examples));
      replaceKey(map, "input", "Pattern");
      replaceMap(map, putKeysFirst(map, "globalID", "Example Input", "Example Output", "Pattern"));
    }
    
    Cl<Rule> listConcepts() {
      ret conceptsSortedByFieldCI(Rule, 'input);
    }
  } .fieldHelp(variables := "comma-separated")
    .fieldHelp(flags := "comma-separated")
  ) {
    S renderValue(S field, O value) {
      S html = super.renderValue(field, value);
      if (eq(field, 'globalID))
        ret ahref(rawLink(str(value)), html);
      if (eqOneOf(field, 'input, "Pattern"))
        ret b(html);
      ret html;
    }
  };
}

static HCRUD inputsCRUD() {
  ret new HCRUD(new HCRUD_Concepts<Input>(Input.class) {
    void massageItemMapForList(Input i, MapSO map) {
      map.put("Possible Examples" := l(conceptsWhereCI PossibleExample(input := i.input)));
      map.put("Cmds", HTML(ahref(
        appendQueryToURL("admin", cmd := "processInput", inputID := i.id),
        "process")));
    }
  });
}

static HCRUD possibleExamplesCRUD(S uri) {
  ret new HCRUD(new HCRUD_Concepts<PossibleExample>(PossibleExample.class) {
    void massageItemMapForList(PossibleExample e, MapSO map) {
      if (e.rule != null)
        map.put(rule := HTML(ahref(rawLink(e.rule.globalIDStr()), e.rule.globalID())));
      map.put(Cmds := HTML(
        ahref(appendQueryToURL(rawLink(uri), cmd := "markGood", exampleID := e.id), "good") + " " +
        ahref(appendQueryToURL(rawLink(uri), cmd := "markBad", exampleID := e.id), "bad")));
    }
  });
}

set flag NoNanoHTTPD. html {
  long startTime = sysNow();
  bool authed = webAuthed(params);

  if (empty(uri = dropSlashPrefix(uri)))
    ret htitle_h1("Rules")
      + (webAuthed(params) ? p(ahref(rawLink("admin"), "Admin")) : "")
      + ul_noEncode(map(list(Rule), r -> ahref(rawLink(r.globalIDStr()), htmlEncode2(r.input + " => " + r.output))));
    
  if (eqic(uri, "wordClassExamples"))
    ret serveText(mapPairsToLines(multiSetMapToPairs(wordClassExamples()), (k, v) -> quote(v) + " is in word class " + quote(k)));
    
  // serve rule
  if (possibleGlobalID(uri)) {
    Rule r = conceptWhere(Rule, globalID := GlobalID(uri));
    if (r == null) ret subBot_serve404("Rule not found");
    S html = htitle_h1("Rule " + r.globalID)
      + htmlTable2(mapToTwoElementMaps("Field", "Value", litorderedmap("Input" := r.input, "Output" := r.output, "Variables" := r.variables, "Comment" := r.comment)));

    HCRUD peCRUD = possibleExamplesCRUD(uri);
    ((HCRUD_Concepts) peCRUD.data).addFilter(rule := r);
    html += h3("Possible Examples")
      + peCRUD.renderTable(false);

    //Cl<PossibleExample> examples = conceptsWhere PossibleExample(rule := r);
    //ret possibleExamplesCRUD().render(false, )
    ret html;  
  }
  
  new Matches m;
  if (swic_slash(uri, "admin", m))
 {
    S _uri = uri;
    HMultiCRUD multi = new(rawLink("admin"), Rule, Input, PossibleExample) {
      HCRUD makeCRUD(S className) {
        if (eq(className, "Rule")) ret rulesCRUD();
        if (eq(className, "Input")) ret inputsCRUD();
        if (eq(className, "PossibleExample")) ret possibleExamplesCRUD(_uri);
        null;
      }

      LS naviComponents() {
        LS l = super.naviComponents();
        if (mutationRights)
          l.add(ahref(appendQueryToURL(baseLink, cmd := "processInputs"), "process inputs"));
        l.add(ahref(rawLink("wordClassExamples"), "word class examples"));
        ret l;
      }
    } .mainTitle("Rules + Inputs")
      .mutationRights(authed);
    multi.downloadRights = authed;
    HAbstractRenderable.MakeFrame mf = multi.makeFrame;
    multi.makeFrame = (title, contents) -> mf.makeFrame(title, contents) + p(small(sysNow()-startTime + " ms"), align := "right");

    S cmd = params.get('cmd);
    if (nempty(cmd)) {
      try object checkWebAuthed(params);
      if (eqOneOf(cmd, "markGood", "markBad")) {
        PossibleExample e = getConcept(PossibleExample, parseLong(params.get('exampleID)));
        cset(e, good := eq(cmd, "markGood"));
      }

      if (eq(cmd, "processInput")) {
        Input i = getConcept(Input, toLong(params.get('inputID)));
        ret serveText(hijackPrint_tee_pcall(() -> processInput(i.input)));
      }

      if (eq(cmd, "processInputs")) {
        time {
          for (Input i)
            processInput(i.input);
        }
        ret multi.refreshWithMsgs("Processed " + nInputs(countConcepts(Input)) + " in " + lastTiming() + " ms");
      }
    }

    ret multi.renderPage(m.rest(), params);
  }
  
  ret subBot_serve404();
}

static void processInput(S input) {
  for (Rule r) {
    new Set<PossibleExample> found;
    print("Combining input " + quote(input) + " with rule " + quote(r.input));
    ITokenizer tokenizer = r.flags().contains("keep punctuation")
      ? lambda1 javaTokWithBrackets_cached
      : lambda1 javaTokNPunctuationWithBrackets_plusAsterisk_cached;
    //for (SS mapping : arbitraryVarsFlexMatchIC_iterator(r.vars(), r.input, input)) {
    for (SS mapping : arbitraryVarsFlexMatchIC_tok_iterator(r.vars(), Tok(tokenizer, r.input), Tok(tokenizer, input))) {
      print("  Have mapping: " + mapping);
      PossibleExample e = uniq PossibleExample(rule := r, +input, +mapping);
      found.add(e);
      S output = replaceVars_optRound2(r.output, mapping);
      output = applyTransformers(output);
      cset(found, +output);
    }
    deleteConcepts(listMinusSet(conceptsWhere(PossibleExample, rule := r, +input), found));
  }
}

static ByName<ITransform<S>>transformers(){ret mapGet_if1(litmap(
    nominativ := (IF1<S>) lambda1 german_toNominativ,
    switcheroo := (IF1<S>) lambda1 switcheroo
));}

sS applyTransformers(S s) {
  ret pcallOrKeep(s, () -> tok_applyCurlyTransformers(transformers(), s));
}

static MultiSetMap<S> wordClassExamples() {
  MultiSetMap<S> mm = ciMultiCISetMap();
  for (PossibleExample e : conceptsWhere PossibleExample(good := true))
    for (S key, value : unnull(e.mapping))
      mm.put(key, value);
  ret mm;
}

Author comment

Began life as a copy of #1026102

download  show line numbers  debug dex  old transpilations   

Travelled to 6 computer(s): bhatertpkbcr, mqqgnosmbjvj, pyentgdyhuwx, pzhvpgtvlbxg, tvejysmllsmz, vouqrxazstgt

No comments. add comment

Snippet ID: #1026126
Snippet name: Rules + Inputs Web Bot
Eternal ID of this version: #1026126/107
Text MD5: 8536ce6a3c2c3ffa6af396a74c85fb98
Transpilation MD5: b1978f946a00b6f1173e365b48a08293
Author: stefan
Category: javax / a.i.
Type: JavaX module (desktop)
Public (visible to everyone): Yes
Archived (hidden from active list): No
Created/modified: 2019-11-29 23:13:51
Source code size: 7305 bytes / 199 lines
Pitched / IR pitched: No / No
Views / Downloads: 269 / 1926
Version history: 106 change(s)
Referenced in: [show references]