package com.wlkj.test; /** * 判断一段文本中出现比例是否命中 * @author 黄宝康 * */ public class WordUtil { public static int appearNumber(String srcText, String findText) { int count = 0; int index = 0; while ((index = srcText.indexOf(findText, index)) != -1) { index = index + findText.length(); count++; } return count; } // 判断评论是否是非法评论 public static boolean isBadComment(String comment,String badword,double baseratio){ System.out.println("调用"+badword); int time = appearNumber(comment, badword); double ratio = badword.length()*time*1.0/comment.length();// 过滤词长度*出现次数/评论的长度 //System.out.println("比例="+ratio); if(ratio>baseratio){ return true; }else{ return false; } } public static void main(String[] args) { String test_str = "赞赞赞,好"; System.out.println(isBadComment(test_str, "赞", 0.5)); //比例=0.6 //true } }