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

57
LINES

< > BotCompany Repo | #1029825 // QAHandler [dev.]

JavaX fragment (include)

1  
sclass QAHandler<QA> {
2  
  QA findQAWithTypos(S s, S language) {
3  
    Lowest<QA> qa = findClosestQA(s, language);
4  
    if (!qa.has()) null;
5  
    if (qa.score() > maxTypos) {
6  
      print("Rejecting " + nTypos((int) qa.score()) + ": " + s + " / " + qa->patterns);
7  
      null;
8  
    }
9  
    print("Accepting " + nTypos((int) qa.score()) + ": " + s + " / " + qa->patterns);
10  
    ret qa!;
11  
  }
12  
  
13  
  QA findMatchingQA(S s, S language, bool allowTypos) {
14  
    try object QA qa = findMatchingQA(s, conceptsWhere(QA, +language));
15  
    try object QA qa = findMatchingQA(s, filter(list(QA), q -> neq(q.language, language)));
16  
    if (allowTypos)
17  
      try object QA qa = findQAWithTypos(s, language);
18  
    null;
19  
  }
20  
  
21  
  static Lowest<QA> findClosestQA(S s, S language) {
22  
    time "findClosestQA" {
23  
      new Lowest<QA> qa;
24  
      findClosestQA(s, qa, conceptsWhere(QA, +language));
25  
      findClosestQA(s, qa, filter(list(QA), q -> neq(q.language, language)));
26  
    }
27  
    ret qa;
28  
  }
29  
  
30  
  static L<QA> sortQAs(Cl<QA> qas) {
31  
    Map<S, Int> categoryToIndex = fieldToFieldIndex('name, 'index, list(Category));
32  
    ret sortedByCalculatedField(qas, q -> pair(categoryToIndex.get(q.category), q.index));
33  
  }
34  
  
35  
  svoid reindexQAs() {
36  
    for (Language lang) {
37  
      int index = 1;
38  
      for (QA qa : sortQAs(conceptsWhere(QA, language := lang.code)))
39  
        cset(qa, index := index++);
40  
    }
41  
  }
42  
  
43  
  static QA findMatchingQA(S s, Cl<QA> qas) {
44  
    for (QA qa : sortQAs(qas))
45  
      if (mmo2_match(qa.parsedPattern(), s))
46  
        ret qa;
47  
    null;
48  
  }
49  
  
50  
  svoid findClosestQA(S s, Lowest<QA> best, Cl<QA> qas) {
51  
    for (QA qa : sortQAs(qas)) {
52  
      Int score = mmo2_levenWithSwapsScore(qa.parsedPattern(), s);
53  
      if (score != null)
54  
        best.put(qa, score);
55  
    }
56  
  }
57  
}

download  show line numbers  debug dex  old transpilations   

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

No comments. add comment

Snippet ID: #1029825
Snippet name: QAHandler [dev.]
Eternal ID of this version: #1029825/1
Text MD5: 36b195ea16a8bc949f72e46b66bb0b6c
Author: stefan
Category: javax / chat bots
Type: JavaX fragment (include)
Public (visible to everyone): Yes
Archived (hidden from active list): No
Created/modified: 2020-09-21 09:00:54
Source code size: 1801 bytes / 57 lines
Pitched / IR pitched: No / No
Views / Downloads: 158 / 185
Referenced in: [show references]