如何在java中从一个段落中找到多组特定单词?
假设我有这样一段话:如何在java中从一个段落中找到多组特定单词?,java,arrays,string,Java,Arrays,String,假设我有这样一段话: String str = "Lorem Ipsum is simply dummy text of the printing and typesetting industry. Lorem Ipsum has been the industry's standard dummy text ever since the 1500s"; setA将包含诸如Lorem、text、dummy之类的单词。 setB将包含诸如Ipsum、printing、industry等词。 se
String str = "Lorem Ipsum is simply dummy text of the printing and typesetting industry. Lorem Ipsum has been the industry's standard dummy text ever since the 1500s";
setA
将包含诸如Lorem、text、dummy之类的单词。setB
将包含诸如Ipsum、printing、industry等词。setC
将包含诸如Lorem、text、dummy、Ipsum、printing、industry等词
然后呢
if(str.equals(set A))
{
Some logic
}
else if{
Set B logic
}
else{
Set C logic
}
如何用java编写代码 试试下面这些:
public boolean hasAny( final String txt, final Collection<String> words )
{
for ( final String word : words )
if ( txt.contains( word ) )
return true;
return false;
}
public boolean hasAll( final String txt, final Collection<String> words )
{
boolean result = true;
for ( final String word : words )
result &= txt.contains( word );
return result;
}
public boolean hasAny(最终字符串txt,最终集合词)
{
for(最后一个字符串字:words)
if(txt.contains(word))
返回true;
返回false;
}
公共布尔hasAll(最终字符串txt,最终集合字)
{
布尔结果=真;
for(最后一个字符串字:words)
结果&=txt.contains(word);
返回结果;
}
Java8流也可以做类似的事情…您希望每个集合返回一个整数,告诉您幻影的数量,还是一个布尔值,告诉您每个集合是否有多个幻影 我会计算字符串中集合中每个单词的幻影数并返回最低值,或者在每个幻影数>=2时返回布尔值,具体取决于您要执行的操作 我们首先要有一个
Map<Integer,String> setA = new HashMap<Integer,String>();// Integer is the number of apparitions of the String in the set
Map setA=newhashmap();//Integer是集合中字符串的幻影数
伪代码:
For each set{
For each word in the set{
count_of_word=str.count_number_of_apparitions_of(word)
}
for each word in the set{
if count_of_word<min_count_of_word{
min_count_of_word=count_of_word
}
return min_count_of_word
}
每组的{
对于集合中的每个单词{
单词的计数=单词的计数
}
对于集合中的每个单词{
如果count_of_word可能是一个奇怪的解决方案,但它可以帮助您在一个较长的段落中,所以通常我使用String::matches
和一些正则表达式,如:
//Only one word
(?i)(?=.*\bword\b).*
//-----------^
//Multiple words
(?i)(?=.*\bword1\b).*(?=.*\bword2\b).*
//---------^-----------------^
因此,想法很简单,为你的单词创建一个模式,然后使用匹配项验证段落是否包含所有单词:
代码示例:
class Main {
public static void main(String as[]) {
String str = "Lorem Ipsum is simply dummy text of the printing and "
+ "typesetting industry. Lorem Ipsum has been the industry's "
+ "standard dummy text ever since the 1500s";
String setA = "Lorem, text, dummy";
String setB = "Ipsum, printing, industry";
String setC = "Lorem, text, dummy,Ipsum, printing, industry";
Main m = new Main();
if (str.matches(m.getPattern(setA))) {
//Do something
} else if (str.matches(m.getPattern(setB))) {
//Do something
} else if (str.matches(m.getPattern(setC))) {
//Do something
}
}
//The important method
private String getPattern(String words) {
StringBuilder pattern = new StringBuilder();
System.out.println(Arrays.toString(words.split(",\\s*")));
Arrays.asList(words.split(",\\s*"))
.stream()
.map(t -> "(?=.*\\b" + t + "\\b).*")
.forEach(pattern::append);
return "(?i)" + pattern.toString();
}
}
方法getPattern
将获取单词列表setA
,setB
,setC
…,它可以是任何东西,然后在:
(1)把这个单词拆分为SETA,它会给你<代码> [LoRM,文本,哑] < /代码>(我认为输入是一个字符串,我用S拆开,如果你有一个集合,你可以避免使用S拆开并使用这个集合,就像它一样)
(2) 循环抛出单词列表来创建一个模式,您可以稍后使用它来匹配您的输入(我使用Java8流而不是普通的循环来简化模式的创建)
例如:对于setA
,它将返回这样一个模式(?=.*\bLorem\b)。*(?=..*\btext\b)。*(?=.*\bdummy\b)。*
,它可以匹配任何包含所有单词Lorem
和text
和dummy>的段落
检查你已经尝试过了吗?是的,我尝试过使用StringUtils。但是我刚刚开始学习java,这对我来说有点困难。