识别给出一组句子的模式

时间:2013-12-30 08:55:18

标签: regex string data-structures pattern-matching

我有一个包含大量句子的文本文件。这些句子可以以模式出现。我如何识别这些模式?

例如:


我早上醒来

我去了学校

我踢足球

我回到了家里

我早上醒来

我去了学校

我打篮球

此时我希望程序说“我踢足球”应该出现了。

1 个答案:

答案 0 :(得分:1)

这个任务似乎有点复杂,但你可以尝试这个简单的代码来理解,或者如果发现它有用,你可以进一步实现它::

     //the sentences/input input String
    String sampleString1="xyz";
    String[] sampleString2=sampleString1.split(".");
    for(int i=1;i<=sampleString2.length;i++){
        //The pattern which you can specify to match with the sentence
        if(sampleString2[i].substring(0, 14).equals(sampleString2[0].substring(0,16))){
            //code to execute the matched sentence.
            System.out.println("Sentence matching with pattern ::" + sampleString2[i]);
        }
    }

如果要匹配的模式是序列的第一行,则尝试此代码。