gpt4 book ai didi

java - 创建一个类的方法并在主类中调用它

转载 作者:行者123 更新时间:2023-12-01 13:17:15 25 4
gpt4 key购买 nike

RiWordnet wordnet = new RiWordnet();
String word = "apple";
String[] poss = wordnet.getPos(word);
boolean check = false;
String[] synonyms = null;

for (int j = 0; j < poss.length; j++) {
synonyms = wordnet.getAllSynonyms(word, poss[j], 50);
for (int i = 0; i < synonyms.length; i++) {
System.out.println(synonyms[i]);
}
}

for(int i= 0; i<synonyms.length;i++){
if(input.contains(synonyms[i])){
check=true;
}
} //end of synonym method

if(synonyms.check(true))
String syn ="that is the synonym of apple.";

所以我正在研究有关同义词识别的 Wordnet API。上面的代码片段工作得很好,但该方法仅适用于单词“apple”。我仍然必须对许多其他单词使用该方法,因此不必为不同的单词一遍又一遍地复制和粘贴该方法,有没有一种方法可以让我将该方法放入不同的类中并在我时调用它需要使用它吗?如果是这样,怎么办?

(p.s:我尝试将其放入类中,但我不确定如何调用该方法和“if”语句的 boolean 值。以下是我的尝试)

public class synonyms {
public static String syn(String word){
RiWordnet wordnet = new RiWordnet();
String input = null;
String[] poss = wordnet.getPos(word);
boolean check = false;
String[] synonyms = null;

for (int j = 0; j < poss.length; j++) {
synonyms = wordnet.getAllSynonyms(word, poss[j], 50);
for (int i = 0; i < synonyms.length; i++) {
System.out.println(synonyms[i]);
}
}

for(int i= 0; i<synonyms.length;i++){
if(input.contains(synonyms[i])){
check=true;
}
}
}
}

最佳答案

也许你想要的是:

public class MySynonyms {
public static boolean syn(String input, String word){
RiWordnet wordnet = new RiWordnet();
String[] poss = wordnet.getPos(word);
boolean check = false;
String[] synonyms = null;

for (int j = 0; j < poss.length; j++) {
synonyms = wordnet.getAllSynonyms(word, poss[j], 50);
for (int i = 0; i < synonyms.length; i++) {
//System.out.println(synonyms[i]);
}
}

for(int i= 0; i<synonyms.length;i++){
if(input.contains(synonyms[i])){
check=true;
}
}
return check;
}
}

以及你的主类:

if(MySynonyms.syn("apel","apple"))
{
//...do something
}
else{
//...do something
}

关于java - 创建一个类的方法并在主类中调用它,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/22367896/

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