Creating your own Suggestion Analyzer

To create your own Suggestion Analyzer must create a new class that implements Suggestion interface:

package com.openkm.form.suggestion;

import java.util.List;

import com.openkm.bean.form.Select;
import com.openkm.core.DatabaseException;
import com.openkm.core.PathNotFoundException;

public interface Suggestion {
  List<String> getSuggestions(String nodeUuid, String nodePath, Select sel) throws PathNotFoundException, SuggestionException, DatabaseException;
}

We suggest create the new class into the package com.openkm.form.suggestion.

Methods description

MethodTypeDescription

getSuggestions(String nodeUuid, String nodePath, Select sel)

List<String>

Return a list of suggested words to tag the document.

The method need the document uuid ( nodeUuid ) and the document path ( nodePath ).

Select can be used as a collection of possible values ( enclosed dictionary ).

 Example of Suggestion Analyzer

package com.openkm.form.suggestion;

import java.io.IOException;
import java.util.ArrayList;
import java.util.List;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import com.openkm.bean.form.Option;
import com.openkm.bean.form.Select;
import com.openkm.core.DatabaseException;
import com.openkm.core.PathNotFoundException;
import com.openkm.dao.NodeDocumentDAO;
import com.openkm.dao.NodeDocumentVersionDAO;
import com.openkm.dao.SearchDAO;
import com.openkm.dao.bean.NodeDocument;
import com.openkm.dao.bean.NodeDocumentVersion;
import com.openkm.extractor.TextExtractorWork;

public class DocumentTermsSuggestion implements Suggestion {
  private static Logger log = LoggerFactory.getLogger(DocumentTermsSuggestion.class);

  @Override
  public List<String> getSuggestions(String nodeUuid, String nodePath, Select sel) throws PathNotFoundException, SuggestionException,
      DatabaseException {
    log.debug("getSuggestions({}, {}, {})", new Object[] { nodeUuid, nodePath, sel });
    List<String> list = new ArrayList<String>();

    try {
      if (NodeDocumentDAO.getInstance().isValid(nodeUuid)) {
        // Get text extraction
        if (!NodeDocumentDAO.getInstance().isTextExtracted(nodeUuid)) {
          // Force text extraction
          NodeDocumentVersion nDocVer = NodeDocumentVersionDAO.getInstance().findCurrentVersion(nodeUuid);
          TextExtractorWork tew = new TextExtractorWork();
          tew.setDocUuid(nodeUuid);
          tew.setDocPath(nodePath);
          tew.setDocVerUuid(nDocVer.getUuid());

          // Execute extractor
          NodeDocumentDAO.getInstance().textExtractorHelper(tew);
        }

        for (String term : SearchDAO.getInstance().getTerms(NodeDocument.class, nodeUuid)) {
          for (Option opt : sel.getOptions()) {
            if (term.equals(opt.getLabel().toLowerCase())) {
              if (!list.contains(opt.getValue())) {
                list.add(opt.getValue());
                break;
              }
            }
          }
        }
      }
    } catch (IOException e) {
      throw new SuggestionException("IOException: " + e.getMessage(), e);
    }

    return list;
  }
}