2013-04-08 150 views
0

我有一个类检查id短语包含在一条消息中,我试图用MatcherPatternString.contains()来做,但返回的结果很奇怪。检查一条消息是否包含一个字符串

这里是类:

public class MotsClesFilter implements EmailFilter { 

    final String NAME = "Filtrage par mots cles"; 
    /*private Pattern chaineSpam; 
    private Matcher chaineCourriel;*/ 
    private int nbOccMotSpam; 
    private byte confidenceLevel; 
    @Override 
    public String getFilterName() { 
     return this.NAME; 

    } 

    @Override 
    public byte checkSpam(MimeMessage message) { 
     analyze(message); 

     if(this.nbOccMotSpam==0) 
      this.confidenceLevel = 1; 
     else if (this.nbOccMotSpam>0 && this.nbOccMotSpam<2) 
      this.confidenceLevel = CANT_SAY; 
     else if (this.nbOccMotSpam>1 && this.nbOccMotSpam<3) 
      this.confidenceLevel = 50; 
     else if (this.nbOccMotSpam>3 && this.nbOccMotSpam<4) 
      this.confidenceLevel = 65; 
     else if (this.nbOccMotSpam>4 && this.nbOccMotSpam<5) 
      this.confidenceLevel = 85; 
     else this.confidenceLevel = 90; 
     return (getConfidenceLevel()); 
    } 


    public void analyze(MimeMessage message){ 
     try { 
      List<String> listeChaines = new ArrayList<String>(); 
      BufferedReader bis = new BufferedReader(new InputStreamReader(new FileInputStream(new File("SpamWords.txt")))); 
      while(bis.ready()){ 
       String ligne = bis.readLine(); 
       listeChaines.add(ligne); 
      } 

      String mail = ((String.valueOf(message.getContent()))); 
      //System.out.println(mail); 


      for (int j =0; j<listeChaines.size();j++){ 
       //System.out.println(listeChaines.get(j)); 
       Pattern chaineSpam = Pattern.compile(listeChaines.get(j),Pattern.CASE_INSENSITIVE); 
       Matcher chaineCourriel = chaineSpam.matcher(mail); 
       if (chaineCourriel.matches()) 
        this.nbOccMotSpam++; 

      } 

     } catch (FileNotFoundException e) { 
      // TODO Auto-generated catch block 
      e.printStackTrace(); 
     } catch (IOException e) { 
      // TODO Auto-generated catch block 
      e.printStackTrace(); 
     } catch (MessagingException e) { 
      // TODO Auto-generated catch block 
      e.printStackTrace(); 
     } 
    } 
    @Override 
    public byte getConfidenceLevel() { 
     // TODO Auto-generated method stub 
     return this.confidenceLevel; 
    } 

    @Override 
    public boolean enabled() { 
     // TODO Auto-generated method stub 
     return true; 
    } 
} 

通过checkSpam返回的结果总是1场,如果使用比赛和90,如果我使用发现,它也返回90,当我使用mail.contains(listeChaines.get(j))

+0

这两种方法都是有效的,你必须尽可能小心Pattern和Matcher 'String'中有非正则表达式兼容的字符。 “Contains”是一个更好的选择。也许添加一些日志记录并使用调试器来查看发生了什么。也用于从'BufferedReader'阅读常规成语是'如果((行= reader.readLine())!= null'。 – 2013-04-08 21:27:28

+0

我推荐使用'之开关语句,而不是那些'if'语句。此外,还有有两个'if'语句永远不会为真。 – 2013-04-08 21:29:12

+0

这如果是从来没有真的吗?我想包含但它就像发现,它不是给我正确的结果。 – user2133558 2013-04-08 21:36:35

回答

0

这意味着,该消息不匹配任何文件中的字符串,但是,有至少5个串,可以发现内部消息的文件中。

matches()检查整个字符串是否匹配模式。如果某个子字符串匹配,则不行。

+0

是的,但找到并String.contains检查沃勒pharase是消息isn'it里面? – user2133558 2013-04-08 21:31:08

+0

'包含()'检查。find()检查子字符串是否匹配正则表达式模式。什么是问题?输入是什么(消息和listeChaines)?你期望什么而不是你得到什么?正如注释所示,你你的if语句有错误,这是否是问题M& – 2013-04-08 21:34:36

+0

对于listeChaines这里是thta ocntains通常垃圾邮件表情列表,消息包含电子邮件的内容。 – user2133558 2013-04-08 21:37:53

0

的问题是,你不要指望所有事件。你只需检查Matcher是否找到了一些东西。您的代码:

if (chaineCourriel.matches()) 
    this.nbOccMotSpam++; 

它不应该是这样的:

while (chaineCourriel.find()) 
    this.nbOccMotSpam++; 

请看到我的小例子:

import java.util.regex.Matcher; 
import java.util.regex.Pattern; 

public class SourceCodeProgram { 

    public static void main(String argv[]) throws Exception { 
     String once = "I contains one SPAM"; 
     String twice = "I contains two: SPAM and SPAM"; 
     String thrice = "I contains 3: SPAM and SPAM and ... again SPAM"; 

     System.out.println(countWordOccurrences(once, "SPAM")); 
     System.out.println(countWordOccurrences(twice, "SPAM")); 
     System.out.println(countWordOccurrences(thrice, "SPAM")); 
    } 

    private static int countWordOccurrences(String text, String word) { 
     Matcher matcher = Pattern.compile(word).matcher(text); 
     int count = 0; 
     while (matcher.find()) { 
      count++; 
     } 
     return count; 
    } 
} 

输出:

1 
2 
3 
相关问题