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

57
LINES

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

JavaX fragment (include)

sclass QAHandler<QA> {
  QA findQAWithTypos(S s, S language) {
    Lowest<QA> qa = findClosestQA(s, language);
    if (!qa.has()) null;
    if (qa.score() > maxTypos) {
      print("Rejecting " + nTypos((int) qa.score()) + ": " + s + " / " + qa->patterns);
      null;
    }
    print("Accepting " + nTypos((int) qa.score()) + ": " + s + " / " + qa->patterns);
    ret qa!;
  }
  
  QA findMatchingQA(S s, S language, bool allowTypos) {
    try object QA qa = findMatchingQA(s, conceptsWhere(QA, +language));
    try object QA qa = findMatchingQA(s, filter(list(QA), q -> neq(q.language, language)));
    if (allowTypos)
      try object QA qa = findQAWithTypos(s, language);
    null;
  }
  
  static Lowest<QA> findClosestQA(S s, S language) {
    time "findClosestQA" {
      new Lowest<QA> qa;
      findClosestQA(s, qa, conceptsWhere(QA, +language));
      findClosestQA(s, qa, filter(list(QA), q -> neq(q.language, language)));
    }
    ret qa;
  }
  
  static L<QA> sortQAs(Cl<QA> qas) {
    Map<S, Int> categoryToIndex = fieldToFieldIndex('name, 'index, list(Category));
    ret sortedByCalculatedField(qas, q -> pair(categoryToIndex.get(q.category), q.index));
  }
  
  svoid reindexQAs() {
    for (Language lang) {
      int index = 1;
      for (QA qa : sortQAs(conceptsWhere(QA, language := lang.code)))
        cset(qa, index := index++);
    }
  }
  
  static QA findMatchingQA(S s, Cl<QA> qas) {
    for (QA qa : sortQAs(qas))
      if (mmo2_match(qa.parsedPattern(), s))
        ret qa;
    null;
  }
  
  svoid findClosestQA(S s, Lowest<QA> best, Cl<QA> qas) {
    for (QA qa : sortQAs(qas)) {
      Int score = mmo2_levenWithSwapsScore(qa.parsedPattern(), s);
      if (score != null)
        best.put(qa, score);
    }
  }
}

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: 157 / 184
Referenced in: