我有这个类,但我需要以一种构造函数(public StemmingAndLemmatization(TurkishMorphParser parser))没有参数(public StemmingAndLemmatization())的方式来编写它!我怎么转换它??任何想法?
public class StemmingAndLemmatization {
TurkishMorphParser parser;
public StemmingAndLemmatization(TurkishMorphParser parser) {
this.parser = parser;
}
public void parse(String word) {
System.out.println("Word = " + word);
System.out.println("Parses: ");
List<MorphParse> parses = parser.parse(word);
for (MorphParse parse : parses) {
System.out.println(parse.formatLong());
System.out.println("tStems = " + parse.getStems());
System.out.println("tLemmas = " + parse.getLemmas());
}
}
public static void main(String[] args) throws IOException {
TurkishMorphParser parser = TurkishMorphParser.createWithDefaults();
new StemmingAndLemmatization(parser).parse("kitabımızsa");
}
}
您需要将构造函数的功能替换为将接受解析器的方法,或者您可以让构造函数创建自己的解析器。
public class StemmingAndLemmatization {
TurkishMorphParser parser;
public void setParser(TurkishMorphParser parser) {
this.parser = parser;
}
或
public StemmingAndLemmatization(TurkishMorphParser) {
this.parser = TurkishMorphParser.createWithDefaults();
}