Example #1
0
 /**
  * 增加一个新词到树中
  *
  * @param newWord
  */
 public void addTerm(NewWord newWord) {
   NewWord temp = null;
   SmartForest<NewWord> smartForest = null;
   if ((smartForest = sf.getBranch(newWord.getName())) != null && smartForest.getParam() != null) {
     temp = smartForest.getParam();
     temp.update(newWord.getScore(), newWord.getNature(), newWord.getAllFreq());
   } else {
     count++;
     // 设置名字为空,节省内存空间
     synchronized (sf) {
       sf.add(newWord.getName(), newWord);
     }
   }
 }
  public void recognition() {
    if (branch == null) {
      return;
    }
    int length = terms.length - 1;

    Term term = null;
    for (int i = 0; i < length; i++) {
      if (terms[i] == null) {
        continue;
      } else {
        from = terms[i].getFrom();
        terms[i].score = 0;
        terms[i].selfScore = 0;
      }

      branch = branch.getBranch(terms[i].getName());

      if (branch == null || branch.getStatus() == 3) {
        reset();
        continue;
      }

      offe = i;

      // 循环查找添加
      term = terms[i];
      sb.append(term.getName());
      if (branch.getStatus() == 2) {
        term.selfScore = branch.getParam().getScore();
      }
      boolean flag = true;
      while (flag) {
        term = term.getTo();
        branch = branch.getBranch(term.getName());
        // 如果没有找到跳出
        if (branch == null) {
          break;
        }

        switch (branch.getStatus()) {
          case 1:
            sb.append(term.getName());
            continue;
          case 2:
            sb.append(term.getName());
            score = branch.getParam().getScore();
            tempNature = branch.getParam().getNature();
            to = term.getTo();
            makeNewTerm();
            continue;
          case 3:
            sb.append(term.getName());
            score = branch.getParam().getScore();
            tempNature = branch.getParam().getNature();
            to = term.getTo();
            makeNewTerm();
            flag = false;
            break;
          default:
            System.out.println("怎么能出现0呢?");
            break;
        }
      }
      reset();
    }
  }