Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/359.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/design-patterns/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java--Patterns--Matcher.group()?_Java_Design Patterns_Matcher - Fatal编程技术网

Java--Patterns--Matcher.group()?

Java--Patterns--Matcher.group()?,java,design-patterns,matcher,Java,Design Patterns,Matcher,所以我有一个文档和一个指定的n-gram目标字符串。我正在尝试查找目标字符串的所有匹配项的索引 final Pattern WORD_PATTERN = Pattern.compile("\\w+"); Matcher matcher = WORD_PATTERN.matcher("the lazy dog, jumps, the lazy dog."); 所以字符串是“懒惰的狗,跳跃,懒惰的狗。” 假设我的目标n-gram是“懒惰者”。我基本上按照如下所示对整个字符串进行“迭代”,将“n”个

所以我有一个文档和一个指定的n-gram目标字符串。我正在尝试查找目标字符串的所有匹配项的索引

final Pattern WORD_PATTERN = Pattern.compile("\\w+");
Matcher matcher = WORD_PATTERN.matcher("the lazy dog, jumps, the lazy dog.");
所以字符串是“懒惰的狗,跳跃,懒惰的狗。”

假设我的目标n-gram是“懒惰者”。我基本上按照如下所示对整个字符串进行“迭代”,将“n”个单词添加到一个链表currentNGram中。如果currentNGram中的所有单词都与目标n-gram匹配,则保存索引。否则,我将删除链表的第一个元素,并在输入字符串的下一个单词上追加(例如,检查文档中的下一个连续n-gram)

while(matcher.find()){
而(currentNGram.size()
这很好,但我的下一个问题是,我必须再次“迭代”文档,找到每个n-gram到最近目标n-gram的距离。所以我采取了完全相同的方法。除了这次,当我重新初始化matcher并按如下方式运行循环时

 while (matcher.find()) {
        while (currentGram.size() < lengthOfTargetNTuple) {
            currentGram.add(matcher.group().toLowerCase());
                    System.out.println(currentGram.printLast()) // Psuedocode
        }
while(matcher.find()){
while(currentGram.size()
它7次打印单词“the”,而不是打印“lazy”“dog”“jumps”等。然而

while (matcher.find()) {
        while (currentGram.size() < lengthOfTargetNTuple) {
            currentGram.add(matcher.group().toLowerCase());
        }
        System.out.println(matcher.group()); // Prints words in order, correctly
}
while(matcher.find()){
while(currentGram.size()
为什么会这样?为什么matcher.group()方法会在第一个问题中按正确的顺序调用打印出来的单词,而在第二个问题中却不调用?如果您能提供任何指导,我们将不胜感激;我知道这是一篇很长的文章,对不起


谢谢!

首先,一些基本知识。让我们看看什么是

尝试查找与模式匹配的输入序列的下一个子序列。 此方法从该匹配器区域的开始处开始,或者,如果先前成功调用了该方法并且此后未重置匹配器,则从第一个未与先前匹配匹配的字符开始

下一步,让我们看一下

返回与上一个匹配匹配的输入子序列


现在我们了解了它们是如何工作的,让我们看看下面的循环是如何工作的

while (matcher.find()) {
    while (currentGram.size() < lengthOfTargetNTuple) {
        currentGram.add(matcher.group().toLowerCase());
                System.out.println(currentGram.printLast()) // Psuedocode
    }
}

但是,在这里,您只打印一次
matcher.group
每个
匹配。find
invoke。这意味着您只打印一次每个匹配的子序列,而不是将
lengthoftargetn打印两次。

首先,一些基本知识。让我们看看有什么作用

尝试查找与模式匹配的输入序列的下一个子序列。 此方法从该匹配器区域的开始处开始,或者,如果先前成功调用了该方法并且此后未重置匹配器,则从第一个未与先前匹配匹配的字符开始

下一步,让我们看一下

返回与上一个匹配匹配的输入子序列


现在我们了解了它们是如何工作的,让我们看看下面的循环是如何工作的

while (matcher.find()) {
    while (currentGram.size() < lengthOfTargetNTuple) {
        currentGram.add(matcher.group().toLowerCase());
                System.out.println(currentGram.printLast()) // Psuedocode
    }
}
但是,在这里,每个
匹配只打印一次
matcher.group

while (matcher.find()) {
    while (currentGram.size() < lengthOfTargetNTuple) {
        currentGram.add(matcher.group().toLowerCase());
    }
    System.out.println(matcher.group()); // Prints words in order, correctly
}