package com.lantone.qc.nlp.util;

import org.apache.commons.lang3.StringUtils;
import com.lantone.qc.nlp.participle.word.Lexeme;

import java.util.*;
import java.util.regex.Pattern;

/**
 * @Auther: fyeman
 * @Date: 2018/12/24/024 16:04
 * @Description:
 */
public class NlpUtil {

    private static List numtextList = new ArrayList(Arrays.asList("数", "多", "半", "一", "二", "三", "四", "五", "六", "七", "八", "九", "十"));
    private static  Map<String, String> numtextMap = new HashMap<String, String>(){{
        put("一","1");
        put("二","2");
        put("三","3");
        put("四","4");
        put("五","5");
        put("六","6");
        put("七","7");
        put("八","8");
        put("九","9");
        put("十","10");
    }};

    public static String[] extendsSymbol(String[] origin, String[] extend_symbols) {
        String[] symbols = new String[origin.length + extend_symbols.length];
        System.arraycopy(origin, 0, symbols, 0, origin.length);
        System.arraycopy(extend_symbols, 0, symbols, origin.length, extend_symbols.length);

        Arrays.sort(symbols);
        return symbols;
    }

    public static boolean isNumberString(Lexeme l) {
        if (l == null) return false;
        if (NlpUtil.isFeature(l.getProperty(), new NegativeEnum[]{NegativeEnum.NUMBER_QUANTIFIER})) {
            return true;
        }
        return Pattern.matches("^[-\\+]?\\d+(\\.\\d+)?",l.getText());
    }

    public static boolean isNumberString(String s) {
        if (StringUtils.isEmpty(s)) return false;
        return Pattern.matches("^[-\\+]?\\d+(\\.\\d+)?",s);
    }

    public static double numberText2value(Lexeme l) {
        double value = 0.0;
        if (numtextList.contains(l.getText())) {
            if ("数".equals(l.getText()) || "多".equals(l.getText())) {     //数年直接按5年处理
                value = 5;
            } else if ("半".equals(l.getText())) {
                value = 0.5;
            } else {
                value = Double.valueOf(numtextMap.get(l.getText()));
            }
        } else {
            try {
                value = Double.valueOf(l.getText());
            } catch (Exception nfe) {
                if (l.getText().indexOf("-") > -1) {
                    try {
                        value = Double.valueOf(l.getText().split("-")[0]);
                    } catch (Exception e) {
                    }
                }
            }
        }
        return value;
    }

    public static boolean isFeature(String property, NegativeEnum[] features) {
        if (property == null) {
            return false;
        }
        if (features == null || features.length == 0) {
            return true;
        }
        if (property.indexOf(",") > 0) {
            String[] properties = property.split(",");
            for (int i = 0; i < properties.length; i++) {
                for (NegativeEnum nenum : features) {
                    if (NegativeEnum.parseOfValue(properties[i]) == nenum) {
                        return true;
                    }
                }
            }
        } else {
            for (NegativeEnum nenum : features) {
                if (NegativeEnum.parseOfValue(property) == nenum) {
                    return true;
                }
            }
        }
        return false;
    }

    /**
     * 返回对应词性的标准术语
     * @param lexeme
     * @param feature
     * @return
     */
    public static String concept(Lexeme lexeme, NegativeEnum feature) {
        if (lexeme.getProperty() == null) {
            return lexeme.getText();
        }
        String[] properties = lexeme.getProperty().split(",");
        String[] concepts = lexeme.getConcept().split(",");
        for (int i = 0; i < properties.length; i++) {
            if (NegativeEnum.parseOfValue(properties[i]) == feature) {
                return concepts[i];
            }
        }
        return lexeme.getText();
    }
}
