solr入门之数据源处理工具类去除字段中括号内内容,字母,数字及特殊符号

来源:互联网 发布:销售数据分析怎么写 编辑:程序博客网 时间:2024/05/20 22:29

项目中solr获取到数据源后,需要对获取到的字段再进行加工处理.

目标: 除去词语中的括号内的内容,词语中英文字符,数字,及特殊符号 仅仅保留汉字

/** * 字符处理过滤工具类 * @author songqinghu * */public class StringFilterUtils {    private static Logger logger = LoggerFactory.getLogger(StringFilterUtils.class);    /**     *      * @描述:过滤solr字段 方法      * @param word     * @return     * @return String     * @exception     * @createTime:2016年4月12日     * @author: songqinghu     */    public static String filterSolrFiled(String word){        logger.info("filterSolrFiled input word:{}",word);        word =  filterNotes(word);        word = filterAll(word);        logger.info("filterSolrFiled output word:{}",word);        return word;    }    /**     *      * @描述:过滤括号和括号里的信息  如果存在括号的话     * @param words     * @return     * @return String     * @exception     * @createTime:2016年4月12日     * @author: songqinghu     */    public static String filterNotes(String word){        //xxx        //xxxx(xxx)  去掉括号内        //xxx(xxx  去掉括号到最后        //xxxxxxx)xxx)xxx //去掉括号前        logger.info("filterNotes 接收到word{}",word);        if(word !=null && word.length()>0){            int left = word.indexOf("(");            int right = word.indexOf(")");            if(left ==-1 && right == -1){//都不存在                logger.info("filterNotes returnword{}",word);                return word;            }            if(left > -1 && right == -1){//存在左边                word = word.substring(0, left);                logger.info("filterNotes returnword{}",word);                return word;            }            if(left == -1 && right >-1){//存在右边               word = word.substring(right+1);               word = filterNotes(word);//递归               return word;            }            if(left >-1 && right>-1){//两边都存在                if(right < word.length()-1){                    String wordLeft = word.substring(0, left);                    String wordRight = word.substring(right+1);                    word = wordLeft + wordRight;                    word = filterNotes(word);                }else{//右括号在最后                    word = word.substring(0,left);                    logger.info("filterNotes returnword{}",word);                }                return word;            }        }        return null;    }    /**     *      * @描述:过滤字符中的英文字符  数字  及特殊符号     * @param word     * @return     * @return String     * @exception     * @createTime:2016年4月12日     * @author: songqinghu     */    public static String filterAll(String word){        if(word != null && word.length()>0){            char[] chars = word.toCharArray();            StringBuffer buff = new StringBuffer();            for (char c : chars) {                if(c>128){//-127 ---127 过滤                    buff.append(c);                }            }            return buff.toString();        }        return null;    }    /**     *      * @描述:过滤字符中的英文字符     * @param word     * @return     * @return String     * @exception     * @createTime:2016年4月12日     * @author: songqinghu     */    public static String filterString(String word){        if(word != null && word.length()>0){            char[] chars = word.toCharArray();            StringBuffer buff = new StringBuffer();            for (char c : chars) {                if((c>=65 && c<=90) || (c>=97 && c<=122) ){//这里是字母                    continue;                }else{                    buff.append(c);                }            }            return buff.toString();        }        return null;    }    /**     *      * @描述:过滤字符中的数字     * @param word     * @return     * @return String     * @exception     * @createTime:2016年4月12日     * @author: songqinghu     */    public static String filterNumber(String word){        if(word != null && word.length()>0){            char[] chars = word.toCharArray();            StringBuffer buff = new StringBuffer();            for (char c : chars) {                if(c>=48 && c<=57 ){//这里是数字                    continue;                }else{                    buff.append(c);                }            }            return buff.toString();        }        return null;    }    /**     *      * @描述:过滤字符中的符号     * @param word     * @return     * @return String     * @exception     * @createTime:2016年4月12日     * @author: songqinghu     */    public static String filterSymbol(String word){        if(word != null && word.length()>0){            char[] chars = word.toCharArray();            StringBuffer buff = new StringBuffer();            for (char c : chars) {                if((c>=65 && c<=90) || (c>=97 && c<=122) || (c>=48 && c<=57 )){                    buff.append(c);                }            }            return buff.toString();        }        return null;    }}





0 0