我有一个语料库,其中包含一个简短的故事,其中有的“谁”、“什么”、“什么时候”、“哪里”、“为什么”问。我用斯坦福NLP API把故事分成句子,然后我得到句子中每个单词的引理,给出了基本词。我也是这么回答问题的。我已经将故事和句子保存在单独的文件中,从这些文件中我可以使用WS4J来帮助我确定故事中的哪个句子可以回答每个问题。
我使用这种方法,它接受两个字符串(问题和可能),并将它们相互比较,并返回一个值,它可能回答问题或不回答问题。
public int compSen(double prob, String sen1, String sen2) {
int cant = 0;
// String sen2c = remStopWords(sen2);
String[] sent1 = getWords(sen1);
String[] sent2 = getWords(sen2);
for (int s = 0; s < sent2.length - 1; s++) {
for (int m = s + 1; m < sent2.length; m++) {
if (sent2[s] != "" && sent2[s].equals(sent2[m])) {
sent2[m] = "";
}
}
}
for (int i = 0; i < sent1.length; i++) {
for (int j = 0; j < sent2.length; j++) {
if (sent2[j] != "") {
double res = compWord(sent1[i].trim(), sent2[j].trim());
if (res >= prob) {
// System.out.println(sent1[i] + " " + sent2[j]);
// System.out.println(res);
cant++;
}
}
}
}
return cant;
}比较单词的另一种方法是::
public double compWord(String word1, String word2) {
ILexicalDatabase db = new NictWordNet();
WS4JConfiguration.getInstance().setMFS(true);
RelatednessCalculator rc = new Path(db);
// String word1 = "gender";
// String word2 = "sex";
List<POS[]> posPairs = rc.getPOSPairs();
double maxScore = -1D;
for (POS[] posPair : posPairs) {
List<Concept> synsets1 = (List<Concept>) db.getAllConcepts(word1, posPair[0].toString());
List<Concept> synsets2 = (List<Concept>) db.getAllConcepts(word2, posPair[1].toString());
for (Concept synset1 : synsets1) {
for (Concept synset2 : synsets2) {
Relatedness relatedness = rc.calcRelatednessOfSynset(synset1, synset2);
double score = relatedness.getScore();
if (score > maxScore) {
maxScore = score;
}
}
}
}
if (maxScore == -1D) {
maxScore = 0.0;
}
// System.out.println(word1);
// System.out.println(word2);
//
// System.out.println(maxScore);
// System.out.println("sim('" + word1 + "', '" + word2 + "') = " + maxScore);
return maxScore;
}我想知道是否还有更好的方法来回答来自一个故事的语料库中的问题,因为我的方法是非常基本的,我成功地回答了20个问题中的1到3个问题。对我来说,这真的很好。任何帮助,想法都是值得感激的。
发布于 2015-02-10 21:46:21
您正在以错误的方式测试空字符串。例如
if (sent2[j] != "") { ...除非您使用保证来规范它返回的字符串,否则这是不可靠的。Java并不保证所有空字符串都是与""相同的对象。下面是测试字符串是否为空的可靠方法:
if ("".equal(sent2[j])) { ... // works even for a null !!!
if (sent2[j].equals("") { ...
if (sent2[j].length() == 0) { ...
if (sent2[j].isEmpty()) { ... // Java 6 onwards这可能不是导致程序失败的原因,但它很可能是一个错误。
https://stackoverflow.com/questions/28441949
复制相似问题