Creating your own Suggestion Analyzer

You can create your own Suggestion Analyzer.

Conditions:

  • The new Suggestion Analyzer class must implement the "Suggestion" interface.
  • The new Suggestion Analyzer class must be declared under the package "com.openkm.plugin.form.suggestion".
  • The new Suggestion Analyzer class must be annotated with "@PluginImplementation".
  • The new Suggestion Analyzer class must extend of "BasePlugin".

Suggestion Analyzer interface:

package com.openkm.plugin.form.suggestion;

import com.openkm.bean.form.Select;
import com.openkm.core.DatabaseException;
import com.openkm.core.PathNotFoundException;
import net.xeoh.plugins.base.Plugin;

import java.util.List;

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

The new class must be loaded into the package com.openkm.plugin.form.suggestion because application plugins system will try to load from there.

Do not miss the tag @PluginImplementation otherwise the application plugin system will not be able to retrieve the new class.

More information at Register a new plugin.

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.plugin.form.suggestion;

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.db.bean.NodeDocumentVersion;
import com.openkm.db.service.NodeDocumentSrv;
import com.openkm.db.service.NodeDocumentUtilsSrv;
import com.openkm.db.service.NodeDocumentVersionSrv;
import com.openkm.extractor.TextExtractorWork;
import com.openkm.plugin.BasePlugin;
import com.openkm.principal.PrincipalUtils;
import com.openkm.search.lucene.LuceneUtils;
import net.xeoh.plugins.base.annotations.PluginImplementation;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;

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

@PluginImplementation
public class DocumentTermsSuggestion extends BasePlugin implements Suggestion {
    private static final Logger log = LoggerFactory.getLogger(DocumentTermsSuggestion.class);

    @Autowired
    private NodeDocumentVersionSrv nodeDocumentVersionSrv;

    @Autowired
    private NodeDocumentUtilsSrv nodeDocumentUtilsSrv;

    @Autowired
    private NodeDocumentSrv nodeDocumentSrv;

    @Autowired
    private LuceneUtils luceneUtils;

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

        try {
            if (nodeDocumentSrv.itemExists(nodeUuid)) {
                // Get text extraction
                if (!nodeDocumentUtilsSrv.isTextExtracted(nodeUuid)) {
                    // Force text extraction
                    NodeDocumentVersion nDocVer = nodeDocumentVersionSrv.findCurrentVersion(nodeUuid);
                    TextExtractorWork tew = new TextExtractorWork();
                    tew.setDocUuid(nodeUuid);
                    tew.setDocPath(nodePath);
                    tew.setDocVerUuid(nDocVer.getUuid());
                    tew.setTenant(PrincipalUtils.getTenantId());

                    // Execute extractor
                    nodeDocumentUtilsSrv.textExtractorHelper(tew);
                }

                for (String term : luceneUtils.getTerms(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(e.getMessage(), e);
        }

        return list;
    }
}