gpt4 book ai didi

java - 在Java中提取一个特定的单词和它每一边的一些标记

转载 作者:行者123 更新时间:2023-11-29 03:42:08 26 4
gpt4 key购买 nike

我需要找到一个很好的机制来提取特定单词(由用户提供)和单词每一侧的 7 个单词。例如,如果我们有以下文本

text = "The mean distance of the Sun from the Earth is approximately 149.6 million kilometers (1 AU), though the distance varies as the Earth moves from perihelion in January to aphelion in July"

如果用户键入“地球”一词,我应该能够提取文本的以下部分

mean distance of the Sun from the Earth is approximately 149.6 million kilometers (1 AU)

所以你可以看到“地球”这个词每边被 7 个词包围着。我如何在 Java 中执行此操作?

最佳答案

使用 ([^ ]+ ?) 匹配一个词,使用 ([^ ]+ ?){0,7} 获取关键词:

String text = "The mean distance of the Sun from the Earth is approximately 149.6 million kilometers (1 AU), though the distance varies as the Earth moves from perihelion in January to aphelion in July";
String word = "Earth";
int around=7;
String pattern="([^ ]+ ?){0,"+around+"}"+word+"( ?[^ ]+){0,"+around+"}";
if(pattern!=null){
Matcher m = Pattern.compile(pattern).matcher(text);
if(m.find()){
System.out.println(m.group());
}
}

关于java - 在Java中提取一个特定的单词和它每一边的一些标记,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/12666139/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com