Java 提取包含特定单词的句子
我想得到文本文件中包含特定关键字的句子。我试了很多,但都没能得到包含关键词的正确句子……我有不止一组关键词,如果其中任何一个与段落匹配,那么就应该使用它。 例如:如果我的文本文件包含诸如抢劫、抢劫等词语,那么该句子应该被提取出来。。下面是我尝试的代码。是否有任何方法可以使用正则表达式解决此问题。任何帮助都将不胜感激Java 提取包含特定单词的句子,java,regex,Java,Regex,我想得到文本文件中包含特定关键字的句子。我试了很多,但都没能得到包含关键词的正确句子……我有不止一组关键词,如果其中任何一个与段落匹配,那么就应该使用它。 例如:如果我的文本文件包含诸如抢劫、抢劫等词语,那么该句子应该被提取出来。。下面是我尝试的代码。是否有任何方法可以使用正则表达式解决此问题。任何帮助都将不胜感激 BufferedReader br1 = new BufferedReader(new FileReader("/home/pgrms/Documents/test/one.tx
BufferedReader br1 = new BufferedReader(new FileReader("/home/pgrms/Documents/test/one.txt"));
String str="";
while(br1 .ready())
{
str+=br1 .readLine() +"\n";
}
Pattern re = Pattern.compile("[^.!?\\s][^.!?]*(?:[.!?](?!['\"]?\\s|$)[^.!?]*)*[.!?]?['\"]?(?=\\s|$)", Pattern.MULTILINE | Pattern.COMMENTS);
Matcher match = re.matcher(str);
String sentenceString="";
while (match .find())
{
sentenceString=match.group(0);
System.out.println(sentenceString);
}
通常,要检查句子是否包含
rob
或robrow
或robrobed
,您可以在字符串锚点的开头之后、正则表达式模式的其余部分之前添加lookehead:
(?=.*(?:rob|robbery|robbed))
在这种情况下,将rob
分组,然后检查潜在后缀更有效:
(?=.*(?:rob(?:ery|ed)?))
在Java代码中,我们可以(例如)如下修改循环:
while (match.find())
{
sentenceString=match.group(0);
if (sentenceString.matches("(?=.*(?:rob(?:ery|ed)?))")) {
System.out.println(sentenceString);
}
}
解释正则表达式
(?= # look ahead to see if there is:
.* # any character except \n (0 or more times
# (matching the most amount possible))
(?: # group, but do not capture:
rob # 'rob'
(?: # group, but do not capture (optional
# (matching the most amount possible)):
ery # 'ery'
| # OR
ed # 'ed'
)? # end of grouping
) # end of grouping
) # end of look-ahead
通常,要检查句子是否包含
rob
或robrow
或robrobed
,您可以在字符串锚点的开头之后、正则表达式模式的其余部分之前添加lookehead:
(?=.*(?:rob|robbery|robbed))
在这种情况下,将rob
分组,然后检查潜在后缀更有效:
(?=.*(?:rob(?:ery|ed)?))
在Java代码中,我们可以(例如)如下修改循环:
while (match.find())
{
sentenceString=match.group(0);
if (sentenceString.matches("(?=.*(?:rob(?:ery|ed)?))")) {
System.out.println(sentenceString);
}
}
解释正则表达式
(?= # look ahead to see if there is:
.* # any character except \n (0 or more times
# (matching the most amount possible))
(?: # group, but do not capture:
rob # 'rob'
(?: # group, but do not capture (optional
# (matching the most amount possible)):
ery # 'ery'
| # OR
ed # 'ed'
)? # end of grouping
) # end of grouping
) # end of look-ahead
通常,要检查句子是否包含
rob
或robrow
或robrobed
,您可以在字符串锚点的开头之后、正则表达式模式的其余部分之前添加lookehead:
(?=.*(?:rob|robbery|robbed))
在这种情况下,将rob
分组,然后检查潜在后缀更有效:
(?=.*(?:rob(?:ery|ed)?))
在Java代码中,我们可以(例如)如下修改循环:
while (match.find())
{
sentenceString=match.group(0);
if (sentenceString.matches("(?=.*(?:rob(?:ery|ed)?))")) {
System.out.println(sentenceString);
}
}
解释正则表达式
(?= # look ahead to see if there is:
.* # any character except \n (0 or more times
# (matching the most amount possible))
(?: # group, but do not capture:
rob # 'rob'
(?: # group, but do not capture (optional
# (matching the most amount possible)):
ery # 'ery'
| # OR
ed # 'ed'
)? # end of grouping
) # end of grouping
) # end of look-ahead
通常,要检查句子是否包含
rob
或robrow
或robrobed
,您可以在字符串锚点的开头之后、正则表达式模式的其余部分之前添加lookehead:
(?=.*(?:rob|robbery|robbed))
在这种情况下,将rob
分组,然后检查潜在后缀更有效:
(?=.*(?:rob(?:ery|ed)?))
在Java代码中,我们可以(例如)如下修改循环:
while (match.find())
{
sentenceString=match.group(0);
if (sentenceString.matches("(?=.*(?:rob(?:ery|ed)?))")) {
System.out.println(sentenceString);
}
}
解释正则表达式
(?= # look ahead to see if there is:
.* # any character except \n (0 or more times
# (matching the most amount possible))
(?: # group, but do not capture:
rob # 'rob'
(?: # group, but do not capture (optional
# (matching the most amount possible)):
ery # 'ery'
| # OR
ed # 'ed'
)? # end of grouping
) # end of grouping
) # end of look-ahead
以下是一个示例,用于显示预定义关键字的列表:
import java.io.BufferedReader;
import java.io.FileReader;
import java.util.regex.*;
public class Tester {
public static void main(String [] args){
try {
BufferedReader br1 = new BufferedReader(new FileReader("input"));
String[] words = {"robbery","robbed", "robbers"};
String word_re = words[0];
String str="";
for (int i = 1; i < words.length; i++)
word_re += "|" + words[i];
word_re = "[^.]*\\b(" + word_re + ")\\b[^.]*[.]";
while(br1.ready()) { str += br1.readLine(); }
Pattern re = Pattern.compile(word_re,
Pattern.MULTILINE | Pattern.COMMENTS |
Pattern.CASE_INSENSITIVE);
Matcher match = re.matcher(str);
String sentenceString="";
while (match .find()) {
sentenceString = match.group(0);
System.out.println(sentenceString);
}
} catch (Exception e) {}
}
}
以下是一个示例,用于显示预定义关键字的列表:
import java.io.BufferedReader;
import java.io.FileReader;
import java.util.regex.*;
public class Tester {
public static void main(String [] args){
try {
BufferedReader br1 = new BufferedReader(new FileReader("input"));
String[] words = {"robbery","robbed", "robbers"};
String word_re = words[0];
String str="";
for (int i = 1; i < words.length; i++)
word_re += "|" + words[i];
word_re = "[^.]*\\b(" + word_re + ")\\b[^.]*[.]";
while(br1.ready()) { str += br1.readLine(); }
Pattern re = Pattern.compile(word_re,
Pattern.MULTILINE | Pattern.COMMENTS |
Pattern.CASE_INSENSITIVE);
Matcher match = re.matcher(str);
String sentenceString="";
while (match .find()) {
sentenceString = match.group(0);
System.out.println(sentenceString);
}
} catch (Exception e) {}
}
}
以下是一个示例,用于显示预定义关键字的列表:
import java.io.BufferedReader;
import java.io.FileReader;
import java.util.regex.*;
public class Tester {
public static void main(String [] args){
try {
BufferedReader br1 = new BufferedReader(new FileReader("input"));
String[] words = {"robbery","robbed", "robbers"};
String word_re = words[0];
String str="";
for (int i = 1; i < words.length; i++)
word_re += "|" + words[i];
word_re = "[^.]*\\b(" + word_re + ")\\b[^.]*[.]";
while(br1.ready()) { str += br1.readLine(); }
Pattern re = Pattern.compile(word_re,
Pattern.MULTILINE | Pattern.COMMENTS |
Pattern.CASE_INSENSITIVE);
Matcher match = re.matcher(str);
String sentenceString="";
while (match .find()) {
sentenceString = match.group(0);
System.out.println(sentenceString);
}
} catch (Exception e) {}
}
}
以下是一个示例,用于显示预定义关键字的列表:
import java.io.BufferedReader;
import java.io.FileReader;
import java.util.regex.*;
public class Tester {
public static void main(String [] args){
try {
BufferedReader br1 = new BufferedReader(new FileReader("input"));
String[] words = {"robbery","robbed", "robbers"};
String word_re = words[0];
String str="";
for (int i = 1; i < words.length; i++)
word_re += "|" + words[i];
word_re = "[^.]*\\b(" + word_re + ")\\b[^.]*[.]";
while(br1.ready()) { str += br1.readLine(); }
Pattern re = Pattern.compile(word_re,
Pattern.MULTILINE | Pattern.COMMENTS |
Pattern.CASE_INSENSITIVE);
Matcher match = re.matcher(str);
String sentenceString="";
while (match .find()) {
sentenceString = match.group(0);
System.out.println(sentenceString);
}
} catch (Exception e) {}
}
}
请看一下和icu4j。它进行边界分析,因此它可以为您拆分句子和单词,并可以为不同的语言进行分析
对于其余部分,您可以根据模式匹配单词(如其他人所建议的),或者根据您感兴趣的一组单词进行检查。查看和icu4j。它进行边界分析,因此它可以为您拆分句子和单词,并可以为不同的语言进行分析
对于其余部分,您可以根据模式匹配单词(如其他人所建议的),或者根据您感兴趣的一组单词进行检查。查看和icu4j。它进行边界分析,因此它可以为您拆分句子和单词,并可以为不同的语言进行分析
对于其余部分,您可以根据模式匹配单词(如其他人所建议的),或者根据您感兴趣的一组单词进行检查。查看和icu4j。它进行边界分析,因此它可以为您拆分句子和单词,并可以为不同的语言进行分析
剩下的部分,你可以根据一个模式匹配单词(正如其他人所建议的),也可以根据你感兴趣的一组单词检查它。一个“句子”是如何定义的?任何结尾有换行符的
字符串是否定义为“句子”或有其他标准?@MadProgrammer str是一个包含有关抢劫的全部新闻的字符串。那么如何区分句子?@MadProgrammer我编辑了我的代码,上面的代码将段落拆分为句子Show是一个“句子”定义任何结尾有换行符的字符串是否定义为“句子”或有其他标准?@MadProgrammer str是一个包含有关抢劫的全部新闻的字符串。那么如何区分句子?@MadProgrammer我编辑了我的代码,上面的代码将段落拆分为句子Show是一个“句子”定义任何结尾有换行符的字符串是否定义为“句子”或有其他标准?@MadProgrammer str是一个包含有关抢劫的全部新闻的字符串。那么如何区分句子?@MadProgrammer我编辑了我的代码,上面的代码将段落拆分为句子Show是一个“句子”定义任何在末尾有换行符的字符串
是否定义为“句子”或者还有其他标准吗?@MadProgrammer str是一个字符串,它包含了关于抢劫的全部新闻。那么你如何区分句子呢?@MadProgrammer我已经编辑了我的代码,上面的代码将段落分割成了句子。这是一个不起作用的模式p1=Pattern.compile(“^(?=.*(:rob |抢劫|抢劫));匹配器m1=p1。匹配器(str);虽然(m1.find()){System.out.println(m1.group(0));}目前我的输出显示“sentenceString”,其中包含段落分隔的句子…我如何才能以这样的方式插入reg ex以获得所需的o/p…我对regex非常陌生…请提供帮助me@chopu刚刚在答案中添加了一些Java代码,请告诉我这是否适用于您。:)谢谢你为我花这么多时间zx81..但事实并非如此works@chopu它在哪一点坏了?您可以将输入拆分为句子,但是System.out.println(句子字符串)代码>不只是过滤抢劫
句子吗?或者你不能分开?这是