3

我似乎找不到任何关于 WordNet 的 C API 的简明准确的教程。有没有人用过并且可以指导?

4

2 回答 2

1

您是否尝试过http://code.google.com/p/wordnet-blast/

从谷歌代码网站:

依赖项:

boost 1.46+

安装:

cmake CMakeLists.txt
make

用法:

#include <wnb/core/wordnet.hh>
using namespace std;
using namespace wnb;
int main()
{
        wordnet wn(PATH_TO_WORDNET);
        vector<synset> synsets1 = wn.get_synsets("cat");
        vector<synset> synsets2 = wn.get_synsets("dog");
        nltk_similarity similarity(wn);
        float d = similarity(synsets1[0], synsets2[0], 6);
}
于 2013-11-03T10:29:55.403 回答
1

这个例子在同义词集导航的情况下可能会有所帮助。尽管代码还没有完全完成,但它足以演示这个想法

从https://github.com/xxinl/semantic-similarity复制

void WordnetExtended::build_synset_adjacency_list(const std::vector<std::string> & words, UndirectedGraph &adj_list)
{   
    if(!OpenDB)
    {
        // wninit return 0 as no error
        if(wninit())
        {           
            std::cout << "Failed to open wordnet files" << std::endl;
            throw;
        }
    }

    // http://stackoverflow.com/questions/8274451/well-how-does-the-custom-deleter-of-stdunique-ptr-work
    //auto delSynset = [](Synset * p) { free_synset(p); };

    for(std::vector<std::string>::const_iterator it = words.begin(); it != words.end(); ++it)
    {
        std::string word = *it;

        // get all avalible poses e
        int all_poses = in_wn(&word[0], ALL_POS);
        //loop 4 poses available in wordnet
        for(int i_pos = 2; i_pos <= 8; i_pos = i_pos << 1)
        {
            if(all_poses & i_pos)
            {   
                SynsetPtr synset_sense = findtheinfo_ds2(&word[0], std::sqrt(i_pos), HYPERPTR, ALLSENSES, 1);   
                // c++ notes: http://stackoverflow.com/questions/106508/what-is-a-smart-pointer-and-when-should-i-use-one
                // TODO consider custom deleter here - auto delSynset = [](Synset * p) { free_synset(p); };
                SynsetPtrS synset_ptr(synset_sense);

                // loop all senses
                while(synset_ptr != nullptr)
                {
                    SynsetPtrS synset_hyper_ptr = synset_ptr;

                    vertex_t pre_vertex = -1;
                    // loop all hyper for each sense
                    while(synset_hyper_ptr != nullptr)
                    {                           
                        vertex_t v = find_vertex(adj_list, synset_hyper_ptr->hereiam, synset_hyper_ptr->pos);
                        if(v == -1)
                        {
                            //http://www.boost.org/doc/libs/1_54_0/libs/graph/example/undirected_adjacency_list.cpp
                            v = boost::add_vertex(adj_list);
                            adj_list[v] = synset_hyper_ptr;
                        }

                        if(pre_vertex != -1 && !boost::edge(pre_vertex, v, adj_list).second)
                        {                           
                            edge_t e; bool b;
                            boost::tie(e, b) = boost::add_edge(pre_vertex, v, 1, adj_list);
                        }

                        pre_vertex = v;

                        // TODO handle the case when more than one HYPERPTR synset. see http://wordnet.princeton.edu/man/wnsearch.3WN.html#sect3 
                        // move to next hyper sense
                        synset_hyper_ptr = SynsetPtrS(synset_hyper_ptr->ptrlist);
                    }

                    // move to next sibling sense
                    synset_ptr = SynsetPtrS(synset_ptr->nextss);
                }
            }
        }
    }
}
于 2013-09-18T08:04:02.283 回答