java简单实现季节,性别分词处理
淘宝里面,每个宝贝都有一个标题,根据标题来分词,区分出季节和性别,分别写了两个方法,供大家参考。
public int season(String str) { String dest = ""; Pattern p = Pattern.compile("\\s*|\t|\r|\n"); Matcher m = p.matcher(str); dest = m.replaceAll(""); System.out.println(dest); String ok=null; for(int i=0;i<dest.length();i++) { //System.out.println("i="+i); if(i>=dest.length()-2) ok=dest.substring(i); else { ok=dest.substring(i,i+2); } //System.out.println(ok); switch(ok) { case "冬日": flag= 4; break; case "夏日": flag= 2; break; case "春季": if(i>=1&&dest.charAt(i-1)=='秋') flag=5; else if(i>=1&&dest.charAt(i-1)=='冬') flag=9; else if(i>=1&&dest.charAt(i-1)=='夏') flag=6; else flag= 1; break; case "夏季": if(i>=1&&dest.charAt(i-1)=='春') flag=6; else if(i>=1&&dest.charAt(i-1)=='秋') flag=11; else if(i>=1&&dest.charAt(i-1)=='冬') flag=8; else flag= 2; break; case "秋季": if(i>=1&&dest.charAt(i-1)=='春') flag=5; else if(i>=1&&dest.charAt(i-1)=='夏') flag=10; else if(i>=1&&dest.charAt(i-1)=='冬') flag=7; else flag= 3; break; case "冬季": if(i>=1&&dest.charAt(i-1)=='春') flag=9; else if(i>=1&&dest.charAt(i-1)=='秋') flag=7; else if(i>=1&&dest.charAt(i-1)=='夏') flag=8; else flag= 4; break; case "春款": if(i>=1&&dest.charAt(i-1)=='秋') flag=5; else if(i>=1&&dest.charAt(i-1)=='冬') flag=9; else if(i>=1&&dest.charAt(i-1)=='夏') flag=6; else flag= 1; break; case "夏款": if(i>=1&&dest.charAt(i-1)=='春') flag=6; else if(i>=1&&dest.charAt(i-1)=='秋') flag=11; else if(i>=1&&dest.charAt(i-1)=='冬') flag=8; else flag= 2; break; case "秋款": if(i>=1&&dest.charAt(i-1)=='春') flag=5; else if(i>=1&&dest.charAt(i-1)=='夏') flag=10; else if(i>=1&&dest.charAt(i-1)=='冬') flag=7; else flag= 3; break; case "冬款": if(i>=1&&dest.charAt(i-1)=='春') flag=9; else if(i>=1&&dest.charAt(i-1)=='秋') flag=7; else if(i>=1&&dest.charAt(i-1)=='夏') flag=8; else flag= 4; case "春秋": if(i<=dest.length()-3&&dest.charAt(i+2)=='冬') { flag=9; return 9; } else flag= 5; break; case "秋春": flag= 5; break; case "春夏": if(i<=dest.length()-3&&dest.charAt(i+2)=='秋') { flag=11; return 11; } else flag= 6; break; case "夏春": flag= 6; break; case "秋冬": flag= 7; break; case "冬秋": flag= 7; break; case "冬夏": flag= 8; break; case "夏冬": flag= 8; break; case "春冬": flag= 9; break; case "冬春": flag= 9; break; case "夏秋": flag= 10; break; case "秋夏": flag= 10; break; case "春天": if(i>=1&&dest.charAt(i-1)=='秋') flag=5; else if(i>=1&&dest.charAt(i-1)=='冬') flag=9; else if(i>=1&&dest.charAt(i-1)=='夏') flag=6; else flag= 1; break; case "夏天": if(i>=1&&dest.charAt(i-1)=='春') flag=6; else if(i>=1&&dest.charAt(i-1)=='秋') flag=11; else if(i>=1&&dest.charAt(i-1)=='冬') flag=8; else flag= 2; break; case "秋天": if(i>=1&&dest.charAt(i-1)=='春') flag=5; else if(i>=1&&dest.charAt(i-1)=='夏') flag=10; else if(i>=1&&dest.charAt(i-1)=='冬') flag=7; else flag= 3; break; case "冬天": if(i>=1&&dest.charAt(i-1)=='春') flag=9; else if(i>=1&&dest.charAt(i-1)=='秋') flag=7; else if(i>=1&&dest.charAt(i-1)=='夏') flag=8; else flag= 4; break; case "春装": if(i>=1&&dest.charAt(i-1)=='秋') flag=5; else if(i>=1&&dest.charAt(i-1)=='冬') flag=9; else if(i>=1&&dest.charAt(i-1)=='夏') flag=6; else flag= 1; break; case "夏装": if(i>=1&&dest.charAt(i-1)=='春') flag=6; else if(i>=1&&dest.charAt(i-1)=='秋') flag=11; else if(i>=1&&dest.charAt(i-1)=='冬') flag=8; else flag= 2; break; case "秋装": if(i>=1&&dest.charAt(i-1)=='春') flag=5; else if(i>=1&&dest.charAt(i-1)=='夏') flag=10; else if(i>=1&&dest.charAt(i-1)=='冬') flag=7; else flag= 3; break; case "冬装": if(i>=1&&dest.charAt(i-1)=='春') flag=9; else if(i>=1&&dest.charAt(i-1)=='秋') flag=7; else if(i>=1&&dest.charAt(i-1)=='夏') flag=8; else flag= 4; break; default: break; } } System.out.println("flag="+flag); if(flag==1) return 1; else if(flag==2) return 2; else if(flag==3) return 3; else if(flag==4) return 4; else if(flag==5) return 5; else if(flag==6) return 6; else if(flag==7) return 7; else if(flag==8) return 8; else if(flag==9) return 9; else if(flag==10) return 10; else return 99; } public int sex(String str) { String dest = ""; Pattern p = Pattern.compile("\\s*|\t|\r|\n"); Matcher m = p.matcher(str); dest = m.replaceAll(""); String ok=null; boolean b=false; System.out.println(dest); for(int i=0;i<dest.length();i++) { if(i==dest.length()-1) ok=dest.substring(i); else ok=dest.substring(i,i+1); switch(ok) { case "男": for(int j=0;j<dest.length();j++) { char c=dest.charAt(j); if(c=='女') b=true; } if(i<=dest.length()-2&&b) return 3; else flag=1; break; case "女": for(int j=0;j<dest.length();j++) { char c=dest.charAt(j); if(c=='男') b=true; } if(i<=dest.length()-2&&b) return 3; else flag=2; break; case "高": if(i<=dest.length()-3&&dest.charAt(i+1)=='富'&&dest.charAt(i+2)=='帅') flag=1; if(i<=dest.length()-3&&dest.charAt(i=1)=='帅'&&dest.charAt(i+2)=='富') flag=1; break; case "白": if(i<=dest.length()-3&&dest.charAt(i+1)=='富'&&dest.charAt(i+2)=='美') flag=2; if(i<=dest.length()-3&&dest.charAt(i+1)=='美'&&dest.charAt(i+2)=='富') flag=2; break; case "萝": if(i<=dest.length()-2&&dest.charAt(i+1)=='莉') flag=2; break; case "罗": if(i<=dest.length()-2&&dest.charAt(i+1)=='莉') flag=2; break; case "靓": if(i<=dest.length()-2&&dest.charAt(i+1)=='妹') flag=2; break; case "辣": if(i<=dest.length()-2&&dest.charAt(i+1)=='妹') flag=2; break; case "正": if(i<=dest.length()-2&&dest.charAt(i+1)=='太') flag=1; break; case "屌": if(i<=dest.length()-2&&dest.charAt(i+1)=='丝') flag=1; break; case "闺": if(i<=dest.length()-2&&dest.charAt(i+1)=='蜜') flag=2; break; case "贵": if(i<=dest.length()-2&&dest.charAt(i+1)=='妇') flag=2; break; case "美": if(i<=dest.length()-2&&dest.charAt(i+1)=='妞') flag=2; break; case "帅": if(i<=dest.length()-2&&dest.charAt(i+1)=='哥') flag=1; break; case "长": if(i<=dest.length()-2&&dest.charAt(i+1)=='裙') flag=2; break; case "短": if(i<=dest.length()-2&&dest.charAt(i+1)=='裙') flag=2; break; case "胸": if(i<=dest.length()-2&&dest.charAt(i+1)=='罩') flag=2; break; case "文": if(i<=dest.length()-2&&dest.charAt(i+1)=='胸') flag=2; break; case "少": if(i<=dest.length()-2&&dest.charAt(i+1)=='妇') flag=2; case "御": if(i<=dest.length()-2&&dest.charAt(i+1)=='姐') flag=2; break; case "连": if(i<=dest.length()-3&&dest.charAt(i+1)=='衣'&&dest.charAt(i+2)=='裙') flag=1; break; } } if(flag==1) return 1; else if(flag==2) return 2; else return 3; }
纯体力活。。觉得有用的顶一下,谢谢~