package org.diagbot.bigdata.work;

import org.algorithm.util.AlgorithmClassify;
import org.apache.commons.lang3.StringUtils;
import org.diagbot.bigdata.common.ApplicationCacheUtil;
import org.diagbot.bigdata.util.BigDataConstants;
import org.diagbot.common.work.SearchData;
import org.diagbot.nlp.feature.FeatureAnalyze;
import org.diagbot.nlp.feature.FeatureType;
import org.diagbot.nlp.participle.ParticipleUtil;
import org.diagbot.nlp.participle.word.Lexeme;
import org.diagbot.nlp.participle.word.LexemePath;
import org.diagbot.nlp.util.Constants;
import org.diagbot.nlp.util.NegativeEnum;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import javax.servlet.http.HttpServletRequest;
import java.util.*;
import java.util.regex.Pattern;

/**
 * @ClassName org.diagbot.bigdata.work.ParamsDataProxy
 * @Description TODO
 * @Author fyeman
 * @Date 2019/1/16/016 14:04
 * @Version 1.0
 **/
public class ParamsDataProxy {
    Logger logger = LoggerFactory.getLogger(ParamsDataProxy.class);
    //标准词只处理的词性
    public static NegativeEnum[] negativeEnums = new NegativeEnum[] { NegativeEnum.VITAL_INDEX, NegativeEnum.SYMPTOM
            , NegativeEnum.DIGITS, NegativeEnum.EVENT_TIME, NegativeEnum.UNIT, NegativeEnum.DIAG_STAND
            , NegativeEnum.OTHER};
    //标准词处理的三元组
    public static NegativeEnum[][] negativeEnumTriple = {
            { NegativeEnum.VITAL_INDEX, NegativeEnum.DIGITS, NegativeEnum.UNIT },
            { NegativeEnum.VITAL_INDEX, NegativeEnum.DIGITS, NegativeEnum.EVENT_TIME },
            { NegativeEnum.SYMPTOM, NegativeEnum.DIGITS, NegativeEnum.UNIT },
            { NegativeEnum.SYMPTOM, NegativeEnum.DIGITS, NegativeEnum.EVENT_TIME },
            { NegativeEnum.DIAG_STAND, NegativeEnum.DIGITS, NegativeEnum.UNIT },
            { NegativeEnum.DIAG_STAND, NegativeEnum.DIGITS, NegativeEnum.EVENT_TIME },
            { NegativeEnum.DIAG_STAND, NegativeEnum.DIGITS, NegativeEnum.OTHER }
    };
    //标准词处理的二元组
    public static NegativeEnum[][] negativeEnumTwoTuple = {
            { NegativeEnum.VITAL_INDEX, NegativeEnum.DIGITS },
            { NegativeEnum.SYMPTOM, NegativeEnum.DIGITS }
    };

    public void createSearchData(HttpServletRequest request, SearchData searchData) throws Exception {
        //消除空格
        if (searchData.getSymptom() != null) {
            searchData.setSymptom(searchData.getSymptom().trim());
        }
        if (searchData.getDiag() != null) {
            searchData.setDiag(searchData.getDiag().trim());
        }
        //计算年龄区间
        if (searchData.getAge() > 0) {
            searchData.setAge_start(searchData.getAge() - 5);
            searchData.setAge_end(searchData.getAge() + 5);
        }
        //修改性别代码
        if (!StringUtils.isEmpty(searchData.getSex())) {
            if ("M".equals(searchData.getSex())) {
                searchData.setSex("1");
            } else if ("F".equals(searchData.getSex())) {
                searchData.setSex("2");
            } else {
                searchData.setSex("3");
            }
        } else {
            searchData.setSex("3");
        }
        //默认查询门诊数据
        if (StringUtils.isEmpty(searchData.getResourceType())) {
            searchData.setResourceType(BigDataConstants.resource_type_o);
        }
//        //给症状末尾添加诊断依据标准词
//        String[] items = { searchData.getSymptom(), searchData.getOther(), searchData.getVital()
//                , searchData.getLis(), searchData.getPacs(), searchData.getDiag() };
//        String[] itemsType = { "symptom", "other", "vital", "lis", "pacs", "diag" };
//        for (int i = 0; i < items.length; i++) {
//            if (items[i] != null) {
//                LexemePath<Lexeme> featureData = ParticipleUtil.participle(items[i]);
//                if (featureData != null) {
//                    addStandWord(featureData, ApplicationCacheUtil.getKl_result_mapping_standword_map(), searchData, itemsType[i]);
//                }
//            }
//        }
        //所有信息参与推送
        //        searchData.setSymptom(searchData.getSymptom() + searchData.getVital()
        //                + searchData.getLis() + searchData.getPacs() + searchData.getPast() + searchData.getOther() + searchData.getIndications());
        if (StringUtils.isNotEmpty(searchData.getSymptom())) {
            searchData.setSymptom(searchData.getSymptom().trim());
        }
        //一次推送多个类别信息
        String[] featureTypes = searchData.getFeatureType().split(",");
        //featureType统一转换
        String[] convertFeatureTypes = new String[featureTypes.length];
        for (int i = 0; i < featureTypes.length; i++) {
            convertFeatureTypes[i] = convertFeatureType(searchData.getSysCode(), featureTypes[i]);
        }
        searchData.setFeatureType(StringUtils.join(convertFeatureTypes, ","));
        searchData.setFeatureTypes(convertFeatureTypes);

        //获取入参中的特征信息
        FeatureAnalyze fa = new FeatureAnalyze();
        List<Map<String, Object>> featuresList = new ArrayList<>();
        if (!StringUtils.isEmpty(searchData.getSymptom())) {
            //提取现病史
            featuresList = fa.start(searchData.getSymptom(), FeatureType.FEATURE);
            paramFeatureInit(searchData, featuresList);
            //提取时间信息
            featuresList = fa.start(searchData.getSymptom(), FeatureType.TIME);
            paramFeatureInit(searchData, featuresList);
        }
        if (!StringUtils.isEmpty(searchData.getVital())) {
            //提取体征
            featuresList = fa.start(searchData.getVital(), FeatureType.FEATURE);
            paramFeatureInit(searchData, featuresList);
        }
        if (!StringUtils.isEmpty(searchData.getPast())) {
            //提取既往史
            featuresList = fa.start(searchData.getPast(), FeatureType.FEATURE);
            paramFeatureInit(searchData, featuresList);
        }
        if (!StringUtils.isEmpty(searchData.getOther()) || !StringUtils.isEmpty(searchData.getIndications())) {
            //提取其他史等
            featuresList = fa.start((searchData.getOther() == null ? "" : searchData.getOther()) + (searchData.getIndications() == null ? "" : searchData.getIndications()), FeatureType.FEATURE);
            paramFeatureInit(searchData, featuresList);
        }
        if (!StringUtils.isEmpty(searchData.getPacs())) {
            featuresList = fa.start(searchData.getPacs(), FeatureType.PACS);
            paramFeatureInit(searchData, featuresList);
        }
        if (!StringUtils.isEmpty(searchData.getLis())) {
            featuresList = fa.start(searchData.getLis(), FeatureType.LIS);
            paramFeatureInit(searchData, featuresList);
        }
        // 清洗特征词，去除词性不匹配的词
        searchData = cleanFeature(featuresList, fa, searchData);
        if (!StringUtils.isEmpty(searchData.getOther())) {
            //如果既往史中诊断信息，需要提取这个特征
            featuresList = fa.start(searchData.getOther(), FeatureType.DIAG);
            paramFeatureInit(searchData, featuresList);
        }

        if (!StringUtils.isEmpty(searchData.getDiag()) && StringUtils.isEmpty(searchData.getSymptom())) {
            featuresList = fa.start(searchData.getDiag(), FeatureType.DIAG);
            paramFeatureInit(searchData, featuresList);
        }
    }

    /**
     * featureType转算法模型类型
     *
     * @param sysCode
     * @param featureTypes
     * @param searchData
     */
    public AlgorithmClassify[] createAlgorithmClassify(String sysCode, String[] featureTypes, SearchData searchData) {
        AlgorithmClassify[] classifies = new AlgorithmClassify[featureTypes.length];
        //下了诊断且其他信息全为空 反推标识
        boolean reverse = !StringUtils.isEmpty(searchData.getDiag()) && StringUtils.isEmpty(searchData.getSymptom());
        for (int i = 0; i < featureTypes.length; i++) {
            //            featureTypes[i] = convertFeatureType(sysCode, featureTypes[i]);
            if (featureTypes[i] != null) {
                //模型
                switch (FeatureType.parse(featureTypes[i])) {
                    case SYMPTOM:
                        if (reverse) {
                            classifies[i] = AlgorithmClassify.NEURAL_DIAG_SYMPTOM;
                        } else {
                            classifies[i] = AlgorithmClassify.NEURAL_SYMPTOM;
                        }
                        break;
                    case DIAG:
                        if (reverse) {
                            classifies[i] = null;
                        } else {
                            classifies[i] = AlgorithmClassify.NEURAL_DIAG;
                        }
                        break;
                    case VITAL:
                        if (reverse) {
                            classifies[i] = AlgorithmClassify.NEURAL_DIAG_VITAL;
                        } else {
                            classifies[i] = AlgorithmClassify.NEURAL_VITAL;
                        }
                        break;
                    case LIS:
                        if (reverse) {
                            classifies[i] = AlgorithmClassify.NEURAL_DIAG_LIS;
                        } else {
                            classifies[i] = AlgorithmClassify.NEURAL_LIS;
                        }
                        break;
                    case PACS:
                        if (reverse) {
                            classifies[i] = AlgorithmClassify.NEURAL_DIAG_PACS;
                        } else {
                            classifies[i] = AlgorithmClassify.NEURAL_PACS;
                        }
                        break;
                    case TREAT:
                        if (reverse) {
                            classifies[i] = AlgorithmClassify.NEURAL_DIAG_TREAT;
                        } else {
                            classifies[i] = AlgorithmClassify.NEURAL_TREAT;
                        }
                        break;
                    case HISTORY:
                        if (reverse) {
                            classifies[i] = AlgorithmClassify.NEURAL_DIAG_HISTORY;
                        } else {
                            classifies[i] = AlgorithmClassify.NEURAL_HISTORY;
                        }
                        break;
                }
            }
        }
        return classifies;
    }

    /**
     * 外部系统featureType需要转化为大数据定义的featureType
     *
     * @param sysCode
     * @param featureType
     * @return
     */
    private String convertFeatureType(String sysCode, String featureType) {
        if (StringUtils.isEmpty(sysCode) || sysCode.equals("1")) {
            if ("1".equals(featureType)) {
                return BigDataConstants.feature_type_symptom;
            }
            if ("7".equals(featureType)) {
                return BigDataConstants.feature_type_diag;
            }
            if ("4".equals(featureType)) {
                return BigDataConstants.feature_type_vital;
            }
            if ("5".equals(featureType)) {
                return BigDataConstants.feature_type_lis;
            }
            if ("6".equals(featureType)) {
                return BigDataConstants.feature_type_pacs;
            }
            if ("3".equals(featureType)) {
                return BigDataConstants.feature_type_history;
            }
            if ("8".equals(featureType)) {
                return BigDataConstants.feature_type_treat;
            }
            if ("22".equals(featureType)) {
                return BigDataConstants.feature_type_labelpush;
            }
            if ("11".equals(featureType)) {
                return BigDataConstants.feature_type_manju;
            }
            if ("42".equals(featureType)) {
                return BigDataConstants.feature_type_vital_index;
            }
            return null;
        }
        return featureType;
    }

    /**
     * 推送模型入参
     *
     * @param searchData
     * @throws Exception
     */
    private void paramFeatureInit(SearchData searchData, List<Map<String, Object>> featuresList) throws Exception {
        if (featuresList != null && featuresList.size() > 0) {
            Map<String, Object> featureMap = null;
            for (int i = 0; i < featuresList.size(); i++) {
                featureMap = featuresList.get(i);
                Map<String, String> map = new HashMap<>();
                for (Map.Entry<String, Object> entry : featureMap.entrySet()) {
                    map.put(entry.getKey(), String.valueOf(entry.getValue()));
                }
                map.put("featureType", String.valueOf(featureMap.get("feature_type")));
                map.put("featureName", String.valueOf(featureMap.get("feature_name")));
                map.put("property", String.valueOf(featureMap.get("property")));
                map.put("concept", String.valueOf(featureMap.get("concept")));
                if (Constants.default_negative.equals(featureMap.get("negative"))) {
                    if (map.get("featureType").equals(Constants.feature_type_time)) {
                        searchData.getInputs().put("时间", map);
                    } else {
                        if (searchData.getInputs().get(map.get("feature_name")) == null) {
                            if (i < 5) {
                                searchData.getInputs().put(map.get("feature_name"), map);
                            }
                            searchData.getGraphInputs().put(map.get("feature_name"), map);
                        }
                    }
                } else {
                    searchData.getFilters().put(map.get("feature_name"), map);
                }
            }
        }
    }

    /**
     * 给SearchData中症状末尾添加诊断依据标准词
     *
     * @param lexemes
     * @param standWords
     * @param sData
     * @return
     */
    public SearchData addStandWord(List<Lexeme> lexemes, Map<String, List<Map<String, String>>> standWords, SearchData sData, String itemType) {
        List<Lexeme> feature = new ArrayList<>();

        //收集分词结果中体征指标或体征指标值（数字）
        for (Lexeme lexeme : lexemes) {
            if (lexeme.getProperty().contains(",")) {
                ApplicationCacheUtil.setProterty(lexeme); //如果分词后词性有多个，只选一个(暂时只处理症状,体征)
            }
            NegativeEnum lexemeNegativeEnum = NegativeEnum.parseOfValue(lexeme.getProperty());
            for (int i = 0; i < negativeEnums.length; i++) {
                if (lexemeNegativeEnum == negativeEnums[i]) {
                    feature.add(lexeme);
                    break;
                }
            }
        }
        //根据收集到的分词结果把体征指标和对应体征指标值（数字）拼接
        List<String> featureType = new ArrayList<>();

        for (int i = 0; i < feature.size(); i++) {
            boolean featureTypeState = true;
            boolean featureTypeStatus = false;
            if (i < feature.size() - 2) {
                for (int j = 0; j < negativeEnumTriple.length; j++) {
                    String featureText = "";
                    for (int k = 0; k < negativeEnumTriple[j].length; k++) {
                        if (NegativeEnum.parseOfValue(feature.get(i + k).getProperty()) == negativeEnumTriple[j][k]) {
                            featureTypeStatus = true;
                            featureText += "\t" + feature.get(i + k).getText();
                        } else {
                            featureTypeStatus = false;
                            break;
                        }
                    }
                    if (featureTypeStatus) {
                        featureType.add(featureText);
                        featureTypeState = false;
                    }
                }
            }
            if (featureTypeState && i < feature.size() - 1) {
                for (int j = 0; j < negativeEnumTwoTuple.length; j++) {
                    String featureText = "";
                    for (int k = 0; k < negativeEnumTwoTuple[j].length; k++) {
                        if (NegativeEnum.parseOfValue(feature.get(i + k).getProperty()) == negativeEnumTwoTuple[j][k]) {
                            featureTypeStatus = true;
                            featureText += "\t" + feature.get(i + k).getText();
                        } else {
                            featureTypeStatus = false;
                            break;
                        }
                    }
                    if (featureTypeStatus) {
                        featureType.add(featureText);
                    }
                }
            }
        }
        //将标准词中体征指标值（数字）与分词结果中体征指标值（数字）比较
        String newStandWord = "";
        for (String f : featureType) {
            String[] features = f.trim().split("\t");
            if (standWords.containsKey(features[0])) {
                List<Map<String, String>> standWordList = standWords.get(features[0]);
                for (Map<String, String> standWordMap : standWordList) {
                    if (standWordMap.containsKey("unit") && standWordMap.containsKey("value")) {
                        if (features.length == 2) {
                            newStandWord = judgment(features, standWordMap, newStandWord, sData, itemType);
                        } else {
                            if (standWordMap.get("unit").equals(features[2].toLowerCase())) {
                                newStandWord = judgment(features, standWordMap, newStandWord, sData, itemType);
                            }
                        }
                    } else if (standWordMap.containsKey("value")) {
                        if (features.length == 2) {
                            newStandWord = judgment(features, standWordMap, newStandWord, sData, itemType);
                        }
                    }
                }
            }
        }
        //血压既满足血压≥140/90mmHg，又满足血压小于90/60mmHg时，只取前者
        String addStandWords = "";
        String smallerStandWord = "";
        boolean flag = true;
        for (String standWord : newStandWord.split(",")) {
            if (!"".equals(standWord) || standWord.length() > 0) {
                if (standWord.contains("<") || standWord.contains("<=") || standWord.contains("小于")) {
                    smallerStandWord += "," + standWord;
                } else {
                    addStandWords += "," + proxy(standWord);
                    flag = false;
                }
            }
        }
        if (flag) {
            addStandWords += smallerStandWord;
        }
        addbloodPressure(sData, itemType, addStandWords);
        return sData;
    }


    /**
     * 将标准词中体征指标值（数字）与分词结果中体征指标值（数字）比较
     * 除了血压>140/90mmHg类似标准词，其他标准词直接添加在症状后面
     *
     * @param features
     * @param standWordMap
     * @param standWord
     * @param sData
     * @return 血压>140/90mmHg或血压小于90/60mmHg或同时返回，在addStandWord()中进一步处理
     */
    private String judgment(String[] features, Map<String, String> standWordMap, String standWord, SearchData sData, String itemType) {
        if (hasDigit(features[1])) {
            try {
                if (">".equals(standWordMap.get("op"))) {
                    //单独处理  血压>140/90mmHg   类似情况
                    if (features[1].contains("/")) {
                        if (standWordMap.get("value").contains("/")) {
                            String[] feature = features[1].split("/");
                            Integer featuresSBP = Integer.valueOf(feature[0]); //分词特征收缩压
                            Integer featuresDBP = Integer.valueOf(feature[1]); //分词特征舒张压

                            String[] values = standWordMap.get("value").split("/");
                            Integer standWordSBP = Integer.valueOf(values[0]); //标准词收缩压
                            Integer standWordDBP = Integer.valueOf(values[1]); //标准词舒张压
                            if (featuresSBP > standWordSBP || featuresDBP > standWordDBP) {
                                standWord += "," + standWordMap.get("standword");
                            }
                        }
                    } else {
                        //"symptom","other","vital","lis","pacs","diag"
                        String num = getNum(standWordMap.get("value"));
                        if (Double.valueOf(getNum(features[1])) > Double.valueOf(num)) {
                            setStandword(standWordMap, sData, itemType);
                        }
                    }
                } else if ("<".equals(standWordMap.get("op"))) {
                    //单独处理  血压小于90/60mmHg   类似情况
                    if (standWordMap.get("value").contains("/")) {
                        if (features[1].contains("/")) {
                            String[] feature = features[1].split("/");
                            Integer featuresSBP = Integer.valueOf(feature[0]); //分词特征收缩压
                            Integer featuresDBP = Integer.valueOf(feature[1]); //分词特征舒张压

                            String[] values = standWordMap.get("value").split("/");
                            Integer standWordSBP = Integer.valueOf(values[0]); //标准词收缩压
                            Integer standWordDBP = Integer.valueOf(values[1]); //标准词舒张压
                            if (featuresSBP < standWordSBP || featuresDBP < standWordDBP) {
                                standWord += "," + standWordMap.get("standword");
                            }
                        }
                    } else {
                        String num = getNum(standWordMap.get("value"));
                        if (Double.valueOf(getNum(features[1])) < Double.valueOf(num)) {
                            setStandword(standWordMap, sData, itemType);
                        }
                    }
                } else if (">=".equals(standWordMap.get("op"))) {
                    //单独处理  血压大于等于140/90mmHg   类似情况
                    if (standWordMap.get("value").contains("/")) {
                        if (features[1].contains("/")) {
                            String[] feature = features[1].split("/");
                            Integer featuresSBP = Integer.valueOf(feature[0]); //分词特征收缩压
                            Integer featuresDBP = Integer.valueOf(feature[1]); //分词特征舒张压

                            String[] values = standWordMap.get("value").split("/");
                            Integer standWordSBP = Integer.valueOf(values[0]); //标准词收缩压
                            Integer standWordDBP = Integer.valueOf(values[1]); //标准词舒张压
                            if (featuresSBP >= standWordSBP || featuresDBP >= standWordDBP) {
                                standWord += "," + standWordMap.get("standword");
                            }
                        }
                    } else {
                        String num = getNum(standWordMap.get("value"));
                        if (Double.valueOf(getNum(features[1])) >= Double.valueOf(num)) {
                            setStandword(standWordMap, sData, itemType);
                        }
                    }
                } else if ("<=".equals(standWordMap.get("op"))) {
                    //单独处理  血压小于等于90/60mmHg   类似情况
                    if (standWordMap.get("value").contains("/")) {
                        if (features[1].contains("/")) {
                            String[] feature = features[1].split("/");
                            Integer featuresSBP = Integer.valueOf(feature[0]); //分词特征收缩压
                            Integer featuresDBP = Integer.valueOf(feature[1]); //分词特征舒张压

                            String[] values = standWordMap.get("value").split("/");
                            Integer standWordSBP = Integer.valueOf(values[0]); //标准词收缩压
                            Integer standWordDBP = Integer.valueOf(values[1]); //标准词舒张压
                            if (featuresSBP <= standWordSBP || featuresDBP <= standWordDBP) {
                                standWord += "," + standWordMap.get("standword");
                            }
                        }
                    } else {
                        String num = getNum(standWordMap.get("value"));
                        if (Double.valueOf(getNum(features[1])) <= Double.valueOf(num)) {
                            setStandword(standWordMap, sData, itemType);
                        }
                    }
                }
            } catch (Exception e) {
                e.printStackTrace();
            }
        }
        return standWord;
    }

    /**
     * 根据不同项目添加标准词
     *
     * @param standWordMap
     * @param sData
     * @param itemType
     */
    private void setStandword(Map<String, String> standWordMap, SearchData sData, String itemType) {
        switch (itemType) {
            case "symptom":
                if (sData.getSymptom().indexOf(proxy(standWordMap.get("standword"))) == -1) {
                    sData.setSymptom(sData.getSymptom() + "," + proxy(standWordMap.get("standword")));
                }
                break;
            case "other":
                if (sData.getOther().indexOf(proxy(standWordMap.get("standword"))) == -1) {
                    sData.setOther(sData.getOther() + "," + proxy(standWordMap.get("standword")));
                }
                break;
            case "vital":
                if (sData.getVital().indexOf(proxy(standWordMap.get("standword"))) == -1) {
                    sData.setVital(sData.getVital() + "," + proxy(standWordMap.get("standword")));
                }
                break;
            case "lis":
                if (sData.getLis().indexOf(proxy(standWordMap.get("standword"))) == -1) {
                    sData.setLis(sData.getLis() + "," + proxy(standWordMap.get("standword")));
                }
                break;
            case "pacs":
                if (sData.getPacs().indexOf(proxy(standWordMap.get("standword"))) == -1) {
                    sData.setPacs(sData.getPacs() + "," + proxy(standWordMap.get("standword")));
                }
                break;
            case "diag":
                if (sData.getDiag().indexOf(proxy(standWordMap.get("standword"))) == -1) {
                    sData.setDiag(sData.getDiag() + "," + proxy(standWordMap.get("standword")));
                }
                break;
        }
    }

    /**
     * 添加血压(血压既满足血压≥140/90mmHg，又满足血压小于90/60mmHg时，只取前者)
     *
     * @param sData
     * @param itemType
     * @param addStandWords
     */
    private void addbloodPressure(SearchData sData, String itemType, String addStandWords) {
        switch (itemType) {
            case "symptom":
                if (sData.getSymptom().indexOf(addStandWords) == -1) {
                    sData.setSymptom(sData.getSymptom() + "," + addStandWords);
                }
                break;
            case "other":
                if (sData.getOther().indexOf(addStandWords) == -1) {
                    sData.setOther(sData.getOther() + "," + addStandWords);
                }
                break;
            case "vital":
                if (sData.getVital().indexOf(addStandWords) == -1) {
                    sData.setVital(sData.getVital() + "," + addStandWords);
                }
                break;
            case "lis":
                if (sData.getLis().indexOf(addStandWords) == -1) {
                    sData.setLis(sData.getLis() + "," + addStandWords);
                }
                break;
            case "pacs":
                if (sData.getPacs().indexOf(addStandWords) == -1) {
                    sData.setPacs(sData.getPacs() + "," + addStandWords);
                }
                break;
            case "diag":
                if (sData.getDiag().indexOf(addStandWords) == -1) {
                    sData.setDiag(sData.getDiag() + "," + addStandWords);
                }
                break;
        }
    }

    /**
     * 判断分词后的特征中是否含有数字
     *
     * @param content
     * @return
     */
    private boolean hasDigit(String content) {
        boolean flag = false;
        if (Pattern.compile(".*\\d+.*").matcher(content).matches()) {
            flag = true;
        }
        return flag;
    }

    /**
     * 将字符串中的数字提取出来，针对分词结果中"90."类似情况
     *
     * @param standWord
     * @return
     */
    private String getNum(String standWord) {
        StringBuffer sb = new StringBuffer();
        for (String num : standWord.replaceAll("[^0-9]", ",").split(",")) {
            if (num.length() > 0) {
                sb.append(num);
            }
        }
        return sb.toString();
    }

    /**
     * 将血压超过标准值的标准词改为血压升高
     *
     * @param standWord
     * @return
     */
    private String proxy(String standWord) {
        if (standWord.contains("压") && (standWord.contains("≥") || standWord.contains("大于"))) {
            standWord = "血压升高";
        } else if (standWord.contains("心率") && (standWord.contains("大于") || standWord.contains("超过"))) {
            standWord = "心率快";
        }
        return standWord;
    }

    private SearchData cleanFeature(List<Map<String, Object>> featuresList, FeatureAnalyze fa,
                                    SearchData searchData) {
        // 在输入的辅检文本中，只提取辅检信息
        String[] PACS_Feature = { Constants.word_property_PACS,
                Constants.word_property_PACS_Detail, Constants.word_property_PACS_Result };
        searchData = removeFeature(searchData.getLis(), fa, searchData, PACS_Feature, FeatureType.PACS);

        // 在输入的化验文本中，只提取化验信息
        String[] LIS_Feature = { Constants.word_property_LIS,
                Constants.word_property_LIS_Detail, Constants.word_property_LIS_Result };
        searchData = removeFeature(searchData.getPacs(), fa, searchData, LIS_Feature, FeatureType.LIS);

        return searchData;
    }

    private SearchData removeFeature(String text, FeatureAnalyze fa,
                                     SearchData searchData, String[] properties, FeatureType featureType) {
        String name = "";
        Boolean related = false;

        try {
            List<Map<String, Object>> featureList = fa.start(text, featureType);
            if (featureList != null) {
                for (Map<String, Object> item : featureList) {
                    name = item.get("feature_name").toString();
                    String[] property = item.get("property").toString().split(",");
                    for (String prop : property) {
                        if (Arrays.asList(properties).contains(prop)) {
                            //                            related = true;
                            searchData.getInputs().remove(name);
                            break;
                        }
                    }

                    //                    if (!related) {
                    //                        searchData.getInputs().remove(name);
                    //                    }
                    //9
                    //                    related = false;
                }
            }

        } catch (Exception ex) {
            ex.printStackTrace();
        } finally {
            return searchData;
        }
    }
}