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

103
LINES

< > BotCompany Repo | #1008632 // Sentences to subject with guessing [OK]

JavaX source code (desktop) [tags: use-pretranspiled] - run with: x30.jar

Download Jar. Uses 3874K of libraries. Click here for Pure Java version (9202L/63K).

!759

sS guessProgram = #1008669; //#1008653;
static SingleComponentPanel scp;
static int startIndex;
sO guessModule;
static ReliableSingleThread relearnThread = new(f relearn);

concept Sentence {
  S text;
  S action;
}

p-substance {
  unswing {
    useDBOf(#1008607);
    db(); 
    indexConceptField(Sentence, "text");
    L<S> sentences = toLinesFullTrim(loadSnippet(#1008606));
    sentences = notStartingWith("\"", sentences);
    time { makeUniqueConcepts(Sentence, "text", sentences); }
    pcall { guessModule = runDependent(guessProgram); }
  }
  showFrame(centerAndSouth(scp = new SingleComponentPanel,
    withTopMargin(20, jcenteredline(
      jbutton("Prev", r { makePanel(max(startIndex-10, 0)) }),
      jbutton("Next", f nextPage),
      jbutton("Random", r { makePanel(random(countConcepts(Sentence))/10*10) }),
      jbutton("All OK", r { clickButtons(findButtons(scp, "OK")); nextPage(); }),
      jbutton("Re-learn", r { relearnThread.trigger() })))));
  setFrameWidth(scp, 1000);
  centerFrame(scp);
  makePanel(0);
}

svoid makePanel(int startIndex) {
  main.startIndex = startIndex;
  JPanel panel = gridLayoutPanel_trackWidth_evenlySpacedCols(2);
  panel.add(jlabel("Sentence"));
  panel.add(jlabel("Action"));
  for (final Sentence s : takeFirst(10, dropFirst(startIndex, list(Sentence)))) {
    final JTextField tfText = jtextfield_caretLeft(s.text);
    panel.add(tfText);
    IntRange r = ai_parseSubjectAction(s.action);
    if (r != null)
      tfText.select(r.start, r.end);

    bool hasAction = nempty(s.action);
    final JTextField tf = jtextfield(hasAction ? s.action : guessAction(s.text));
    final new Var<JButton> reviseBtn, okBtn;
    okBtn.set(jbutton("OK", r {
      cset(s, action := getTextTrim(tf));
      disableTextField(tf);
      replaceComponent(okBtn!, reviseBtn!);
      relearnThread.trigger();
    }));
    reviseBtn.set(jbutton("Revise", r {
      cset(s, action := null);
      enableTextField(tf);
      replaceComponent(reviseBtn!, okBtn!);
    }));
    if (hasAction) disableTextField(tf);
    panel.add(centerAndEast(tf, hasAction ? reviseBtn! : okBtn!));
    
    onCaret(tfText, r {
      if (s.action == null) {
        S a = ai_makeSubjectAction(s.text, new IntRange(tfText.getSelectionStart(), tfText.getSelectionEnd()));
        if (a != null) tf.setText(a);
      }
    });
  }
  scp.setComponent(jscroll(panel));
}

static IntRange guess(S sentence) {
  if (guessModule == null) null;
  ret (IntRange) quickImport(call(guessModule, "callGuesser", get(guessModule, "best"), sentence));
}

static S guessAction(S sentence) {
  IntRange r = guess(sentence);
  ret or2(ai_makeSubjectAction(sentence, r), "?");
}

static L<Pair<S, IntRange>> learningMaterial() {
  L<Pair<S, IntRange>> out = new L;
  for (Sentence s) {
    if (s.action == null) continue;
    IntRange r = ai_parseSubjectAction(s.action);
    if (r != null)
      out.add(pair(s.text, r));
  }
  ret out;
}

svoid relearn {
  callMain(guessModule);
  swing { makePanel(startIndex); }
}

svoid nextPage {
  if (startIndex+10 < countConcepts(Sentence)) makePanel(startIndex+10);
}

Author comment

Began life as a copy of #1008607

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: #1008632
Snippet name: Sentences to subject with guessing [OK]
Eternal ID of this version: #1008632/29
Text MD5: 0df6a4a338d889071e0406090caf53b1
Transpilation MD5: 5ae4f699e98890921ab7ef99c55bc45b
Author: stefan
Category: javax / a.i.
Type: JavaX source code (desktop)
Public (visible to everyone): Yes
Archived (hidden from active list): No
Created/modified: 2017-05-28 00:40:33
Source code size: 3215 bytes / 103 lines
Pitched / IR pitched: No / No
Views / Downloads: 446 / 636
Version history: 28 change(s)
Referenced in: [show references]