All Downloads are FREE. Search and download functionalities are using the official Maven repository.

artoria.action.hanlp.support.HankcsSegmentHandler Maven / Gradle / Ivy

The newest version!
package artoria.action.hanlp.support;

import artoria.action.handler.AbstractClassicActionHandler;
import artoria.action.hanlp.SegmentResult;
import artoria.action.hanlp.TextSegment;
import artoria.action.hanlp.Word;
import artoria.util.Assert;
import artoria.util.CollectionUtils;
import com.hankcs.hanlp.HanLP;
import com.hankcs.hanlp.seg.common.Term;

import java.util.ArrayList;
import java.util.List;

import static artoria.util.ObjectUtils.cast;

/**
 * HanLP Segment.
 * @see HanLP
 * @see HanLP
 * @author Kahle
 */
public class HankcsSegmentHandler extends AbstractClassicActionHandler {

    @Override
    public  T execute(Object input, Class clazz) {
        isSupport(new Class[]{SegmentResult.class}, clazz);
        Assert.notNull(input, "Parameter \"input\" must not null. ");
        TextSegment textSegment = (TextSegment) input;
        String algorithm = textSegment.getAlgorithm();
        String text = textSegment.getText();
        Assert.notBlank(text, "Parameter \"TextSegment.text\" must not blank. ");
        List termList = HanLP.segment(text);
        if (CollectionUtils.isEmpty(termList)) { return null; }
        List list = new ArrayList();
        for (Term term : termList) {
            if (term == null) { continue; }
            list.add(new Word(term.word, String.valueOf(term.nature)));
        }
        return cast(new SegmentResult(list, algorithm));
    }

}




© 2015 - 2025 Weber Informatics LLC | Privacy Policy