Java 在字符串中搜索停止词
我试图在我的字符串“zach”中搜索停止词the”、“BE”、“to”、“OF”、“AND”、“A”、“IN”、“THAT”、“I”、“IT”、“ON”、“IN”、“BUT”、“IS”、“WITH” 我不确定字符串搜索方法是否有效,或者是否有更好的方法Java 在字符串中搜索停止词,java,string,eclipse,Java,String,Eclipse,我试图在我的字符串“zach”中搜索停止词the”、“BE”、“to”、“OF”、“AND”、“A”、“IN”、“THAT”、“I”、“IT”、“ON”、“IN”、“BUT”、“IS”、“WITH” 我不确定字符串搜索方法是否有效,或者是否有更好的方法 package zk; public class Class { public boolean isNonStopWord(int[] nums, int value) { } public String
package zk;
public class Class
{
public boolean isNonStopWord(int[] nums, int value)
{
}
public String search( String [] Strings , String july) {
String [] skoal = {"THE", "BE", "TO", "OF", "AND", "A", "IN",
"THAT", "I", "IT", "ON", "IN", "BUT", "IS", "WITH"};
for ( String i = 0, ) {
return false;
}
return true;
}
public static void main(String [] args) {
String zach = ("Amazon offered up more answers Thursday about what"
+ " caused a bunch of websites to fail two days ago. According "
+ "to a postmortem by the company's cloud services business, "
+ "around 9:37 a.m. PT Tuesday an Amazon worker incorrectly"
+ " punched in a command while trying to debug an issue. "
+ "That command shut down a large set of servers at Amazon Web "
+ "Services' Northern Virginia site, causing a domino effect of"
+ " problems. Other services that relied on those S3 cloud"
+ " storage servers were disrupted. Also, removing so much "
+ "server capacity required a full system restart, which then "
+ "took longer than expected, AWS said. The sites affected "
+ "included Quora, Imgur, IFTTT, Giphy and Slack. Amazon was "
+ "able to fix the issue by about 2 p.m. PT.");
zach = zach.replace(",","");
zach = zach.replace(".","");
zach = zach.toUpperCase();
String [] strings = zach.split(" ");
for (String s1: strings)
{
System.out.println(s1);
}
}
}
在这里使用会不会有什么问题:
假设您使用的是Java8+,您可以使用
并且,
\\s+
匹配正则表达式中的一个(或多个)空格。您想如何处理“停止字”“?@ElliottFrisch我想在它们旁边返回false,在其他所有内容旁边返回true。我需要is nonStopWord方法来查找单词并返回true或false,然后我需要在main中调用该方法。我不理解isNonStopWord()
的签名。整数与搜索文本有什么关系?签名是错误的我不知道如何写布尔值correctly@elliott是的,该函数在我的main中工作,但我需要在IsNonStopWord中使用它,并在main中调用它,我不知道如何做到这一点
public boolean hasWord(String input, String word) {
return input.matches(".*\\b" + word + "\\b.*"));
}
// now call the above method from somewhere
public static void main (String[] args) {
String [] skoal = {"THE", "BE", "TO", "OF", "AND", "A", "IN",
"THAT", "I", "IT", "ON", "IN", "BUT", "IS", "WITH"};
String zach = "..."; // your original content
zach = zach.replace(",", ""); // remove punctuation
zach = zach.replace(".", "");
zach = zach.toUpperCase(); // uppercase
for (String stop : skoal) {
if (hasWord(stop)) {
System.out.println(word + " true");
}
else {
System.out.println(word + " false");
}
}
}
String[] strings = zach.split("\\s+");
for (String s1 : strings) {
System.out.println(s1 + ": "
+ Stream.of(skoal).noneMatch(s -> s.equals(s1)));
}