我试图从我的C ++应用程序调用WordNet C API,但它可以工作,但不是预期的。这是我的代码:
int search (char* term)
{
results.clear();
SynsetPtr synsets = findtheinfo_ds(term, NOUN, HYPERPTR, ALLSENSES);
SynsetPtr currentSynset = synsets;
// Loop all senses
while (currentSynset != nullptr)
{
SynsetPtr next = currentSynset;
// Iterate up hierarchy for each sense.
while (next != nullptr)
{
String words;
for (int i = 0; i != next->wcount; ++i)
{
String nextWord = next->words[i];
nextWord = nextWord.replaceCharacter('_', ' ');
words += String(nextWord);
if (i != (next->wcount - 1)) words += ", ";
}
results.add (words + " - " + String(next->defn));
next = next->ptrlist;
}
currentSynset = currentSynset->nextss;
}
free_syns(synsets);
return results.size();
}
我的程序正确地输出每个感官的定义,但是对于每个感觉,它只在层次结构中的搜索词的正上方输出ONE上位词,它不会一直向上移动到& #39;实体&#39 ;.换句话说,第二个SynsetPtr-> ptrlist总是为NULL,即使我可以从WordNet CLI看到有很多级别。
我错过了什么吗?我是否错误地调用了findtheinfo_ds()?
答案 0 :(得分:2)
findtheinfo_ds()
仅返回一个节点。要通过树,您必须为它找到的每个连接调用findtheinfo_ds()
。我发现this page显示了返回数据结构的gdb交互式会话,我认为你会发现它很有用。
另请查看traceptrs_ds()
功能,听起来它可能是针对您要执行的操作而设计的。
答案 1 :(得分:2)
感谢@Darren Cook指出我正确的方向。
正确的解决方案是调用findtheinfo_ds()
来获取起始节点。然后遍历每个感官,获得每个感知的synset->nextss
用于下一个感觉的头部节点。
对于每个感知头节点,首先打印syn->words
或syn->defn
作为定义。然后调用traceptrs_ds()
传递头节点并且深度为1(这意味着我们正在请求递归搜索以使所有节点达到“实体”)。然后从节点traceptrs_ds()
迭代返回以获得该感觉的所有(在我的情况下)上位词。这是我更新的代码:
int setSearch (char* term)
{
results.clear();
SynsetPtr synsets = findtheinfo_ds(term, NOUN, HYPERPTR, ALLSENSES);
SynsetPtr currentSynset = synsets;
// Loop all senses
while (currentSynset != NULL)
{
// Function that prints the synset->defn i.e. the definition of this sense.
printSynsetDef(currentSynset);
// Call this to get the hypernyms for this sense, with next->ptrlist linked as expected.
SynsetPtr next = traceptrs_ds(currentSynset, *currentSynset->ptrtyp, *currentSynset->ppos, 1);
// Iterate up/down tree for each sense.
while (next != NULL)
{
printSynsetDef(next);
next = next->ptrlist;
}
// Don't forget to free the list. Hairy C!
free_syns(next);
currentSynset = currentSynset->nextss;
}
// And free the original list.
free_syns(synsets);
return results.size();
}