package com.ruoyi.biemo.nlp;

import com.hankcs.hanlp.HanLP;
import com.hankcs.hanlp.corpus.dependency.CoNll.CoNLLSentence;
import com.hankcs.hanlp.corpus.dependency.CoNll.CoNLLWord;
import com.hankcs.hanlp.corpus.document.sentence.Sentence;
import com.hankcs.hanlp.corpus.document.sentence.word.IWord;
import com.hankcs.hanlp.corpus.document.sentence.word.Word;
import com.hankcs.hanlp.model.perceptron.PerceptronLexicalAnalyzer;
import com.hankcs.hanlp.tokenizer.NLPTokenizer;
import com.ruoyi.biemo.utils.ParserUtil;
import com.ruoyi.biemo.utils.RelationUtil;

import java.io.IOException;
import java.util.*;

/**
 * 句法分析
 */
public class DependencyParserUtils {

    public static Set<String>  analysis(String text){
        List<CoNLLWord> parser = ParserUtil.parser(text);
        List<Map<String, List<CoNLLWord>>> dict = ParserUtil.dict(text);
        Set<String> result = RelationUtil.relation(parser, dict);
        return  result;
    }

    public static Sentence nerAnalysis(String text) throws IOException {
                    PerceptronLexicalAnalyzer analyzer = new PerceptronLexicalAnalyzer(HanLP.Config.PerceptronCWSModelPath, HanLP.Config.PerceptronPOSModelPath, HanLP.Config.PerceptronNERModelPath);
        //return NLPTokenizer.analyze(text);
        return analyzer.analyze(text);
    }

    public static Sentence nerAnalysis(String text,PerceptronLexicalAnalyzer analyzer) throws IOException {
        return analyzer.analyze(text);
    }

    //上传词性
    public static Boolean learn(Sentence sentence) throws IOException {
        PerceptronLexicalAnalyzer analyzer = new PerceptronLexicalAnalyzer(HanLP.Config.PerceptronCWSModelPath,
                HanLP.Config.PerceptronPOSModelPath,
                HanLP.Config.PerceptronNERModelPath);
        Boolean result = analyzer.learn(sentence);
        if(result){
            analyzer.getPerceptronSegmenter().getModel().save(HanLP.Config.PerceptronCWSModelPath);
            analyzer.getPerceptronPOSTagger().getModel().save(HanLP.Config.PerceptronPOSModelPath);
            analyzer.getPerceptionNERecognizer().getModel().save(HanLP.Config.PerceptronNERModelPath);
        }
        return result;
    }

    public static void main(String[] args) throws Exception{
        String text = "里根号核动力航空母舰驶入南海,里根号他的船很大，极大的像一只傻逼。我爱北京天安门,中国万岁";
        System.out.println(nerAnalysis(text).translateLabels());
        //analyzer.getPerceptronPOSTagger().getModel().save(HanLP.Config.PerceptronPOSModelPath);
        //analyzer.learn("与/c 特朗普/nr 通/v 电话/n 讨论/v");

    }

    public static Map<String,Set<String>> getMyNERTagSet(String[] strArr) throws Exception{
        Map<String,Set<String>> result = new HashMap<>();
        if(strArr!=null&&strArr.length>0){
            PerceptronLexicalAnalyzer analyzer = new PerceptronLexicalAnalyzer(HanLP.Config.PerceptronCWSModelPath, HanLP.Config.PerceptronPOSModelPath, HanLP.Config.PerceptronNERModelPath);
            for(int i=0;i<strArr.length;i++){
                Sentence sentence = nerAnalysis(strArr[i],analyzer);
                sentence = sentence.translateLabels();
                List<IWord> words = sentence.wordList;
                words.forEach(word->{
                    System.out.println(word.getLabel());
                    System.out.println(word.getValue());
                    if(!result.containsKey(word.getLabel())){
                        result.put(word.getLabel(),new HashSet<String>(){{add(word.getValue());}});
                    }else{
                        Set<String> values = result.get(word.getLabel());
                        values.add(word.getValue());
                        result.put(word.getLabel(),values);
                    }
                });
            }
        }
        return result;
    }

}
