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

126
LINES

< > BotCompany Repo | #1008674 // Sentences to subject/verb with guessing [WORKS a bit]

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

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

1  
!759
2  
3  
sS guessProgramS = #1008669; //#1008653;
4  
sS guessProgramV = #1008680;
5  
static SingleComponentPanel scp;
6  
static int startIndex;
7  
sO guessModuleS, guessModuleV;
8  
static ReliableSingleThread relearnThread = new(f relearn);
9  
static JComboBox cbMode;
10  
11  
concept Sentence {
12  
  S text;
13  
  S action; // subject
14  
  S verb;
15  
}
16  
17  
p-substance {
18  
  unswing {
19  
    useDBOf(#1008607);
20  
    db(); 
21  
    indexConceptField(Sentence, "text");
22  
    L<S> sentences = toLinesFullTrim(loadSnippet(#1008606));
23  
    sentences = notStartingWith("\"", sentences);
24  
    time { makeUniqueConcepts(Sentence, "text", sentences); }
25  
    pcall { guessModuleS = runDependent(guessProgramS); }
26  
    pcall { guessModuleV = runDependent(guessProgramV); }
27  
  }
28  
  showFrame(centerAndSouth(scp = new SingleComponentPanel,
29  
    withTopMargin(20, jcenteredline(
30  
      jbutton("Prev", r { makePanel(max(startIndex-10, 0)) }),
31  
      jbutton("Next", f nextPage),
32  
      jbutton("Random", r { makePanel(random(countConcepts(Sentence))/10*10) }),
33  
      jbutton("All OK", r { clickButtons(findButtons(scp, "OK")); nextPage(); }),
34  
      jbutton("Re-learn", r { relearnThread.trigger() })))));
35  
  setFrameWidth(scp, 1000);
36  
  centerFrame(scp);
37  
  makePanel(0);
38  
}
39  
40  
svoid makePanel(int startIndex) {
41  
  main.startIndex = startIndex;
42  
  makePanel();
43  
}
44  
45  
svoid makePanel() {
46  
  JPanel panel = gridLayoutPanel_trackWidth_evenlySpacedCols(2);
47  
  panel.add(jlabel("Sentence"));
48  
  panel.add(jline(cbMode = jcombobox_copySelection(cbMode, "Subject", "Verb")));
49  
  onChange(cbMode, f makePanel);
50  
  for (final Sentence s : takeFirst(10, dropFirst(startIndex, list(Sentence)))) {
51  
    final JTextField tfText = jtextfield_caretLeft(s.text);
52  
    tfText.setCaret(new DefaultCaret {
53  
      @Override
54  
      public void setSelectionVisible(boolean visible) {
55  
        super.setSelectionVisible(true);
56  
      }
57  
    });
58  
    panel.add(tfText);
59  
    IntRange r = ai_parseSubjectAction(s.action);
60  
    if (r != null)
61  
      tfText.select(r.start, r.end);
62  
63  
    bool hasAction = nempty(verbMode() ? s.verb : s.action);
64  
    final JTextField tf = jtextfield(hasAction ?
65  
      (verbMode() ? s.verb : s.action) : guessAction(s));
66  
    final new Var<JButton> reviseBtn, okBtn;
67  
    okBtn.set(jbutton("OK", r {
68  
      cset(s, verbMode() ? "verb" : "action", getTextTrim(tf));
69  
      disableTextField(tf);
70  
      replaceComponent(okBtn!, reviseBtn!);
71  
      relearnThread.trigger();
72  
    }));
73  
    reviseBtn.set(jbutton("Revise", r {
74  
      cset(s, verbMode() ? "verb" : "action", null);
75  
      enableTextField(tf);
76  
      replaceComponent(reviseBtn!, okBtn!);
77  
    }));
78  
    if (hasAction) disableTextField(tf);
79  
    panel.add(centerAndEast(tf, hasAction ? reviseBtn! : okBtn!));
80  
    
81  
    onCaret(tfText, r {
82  
      if ((verbMode() ? s.verb : s.action) == null) {
83  
        S a;
84  
        IntRange r = IntRange(tfText.getSelectionStart(), tfText.getSelectionEnd());
85  
        if (verbMode())
86  
          a = ai_makeVerbAction(s.text, ll(r));
87  
        else
88  
          a = ai_makeSubjectAction(s.text, r);
89  
        if (a != null) tf.setText(a);
90  
      }
91  
    });
92  
  }
93  
  scp.setComponent(jscroll(panel));
94  
}
95  
96  
static IntRange guessSubject(S sentence) {
97  
  if (guessModuleS == null) null;
98  
  ret (IntRange) quickImport(call(guessModuleS, "callGuesser", get(guessModuleS, "best"), sentence));
99  
}
100  
101  
static L<IntRange> guessVerb(S sentence, IntRange subject) {
102  
  if (guessModuleV == null) null;
103  
  ret (L<IntRange>) callWithImportExport(guessModuleV, "callGuesser", get(guessModuleV, "best"), sentence, subject);
104  
}
105  
106  
static S guessAction(Sentence s) {
107  
  if (verbMode()) {
108  
    IntRange subject = ai_parseSubjectAction(s.action);
109  
    ret or2(ai_makeVerbAction(s.text, guessVerb(s.text, subject)), "?");
110  
  } else
111  
    ret or2(ai_makeSubjectAction(s.text, guessSubject(s.text)), "?");
112  
}
113  
114  
svoid relearn {
115  
  callMain(guessModuleS);
116  
  callMain(guessModuleV);
117  
  swing { makePanel(); }
118  
}
119  
120  
svoid nextPage {
121  
  if (startIndex+10 < countConcepts(Sentence)) makePanel(startIndex+10);
122  
}
123  
124  
sbool verbMode() {
125  
  ret cbMode.getSelectedIndex() > 0;
126  
}

Author comment

Began life as a copy of #1008632

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: #1008674
Snippet name: Sentences to subject/verb with guessing [WORKS a bit]
Eternal ID of this version: #1008674/24
Text MD5: 5a152c68cab3063ddbd184765df94eb4
Transpilation MD5: 2062b7c3e296803307861e23e41ed315
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 20:08:11
Source code size: 4097 bytes / 126 lines
Pitched / IR pitched: No / No
Views / Downloads: 549 / 1214
Version history: 23 change(s)
Referenced in: [show references]