3

I can't seem to find any concise and precise tutorial for WordNet's C API. Has anyone of you used it and can guide?

Prashant Kumar
  • 20,069
  • 14
  • 47
  • 63
user1343318
  • 2,093
  • 6
  • 32
  • 59
  • what exactly you want to achieve? im working on a project requries synset navigation and found this page is very useful. http://wordnet.princeton.edu/man/wnsearch.3WN.html. especially the navigation section – Xin Sep 17 '13 at 09:08
  • I just want to find examples so that I can know to call functions. There is none there. Do you have a github repo, code samples? – user1343318 Sep 17 '13 at 09:24
  • have you tried googling "wordnet [function_name]" to see if you come across bits of code? – arturomp Sep 17 '13 at 15:04
  • @user1343318 copied some code for you below – Xin Oct 10 '13 at 10:07
  • @user1343318 If you have found a tutorial, could you share the link or could you paste a sample program? – Rudra Murthy Oct 31 '13 at 20:19
  • @RudraMurthy I only found a workable system with Python. – user1343318 Nov 02 '13 at 10:16
  • Here's a simple example in C++ using the C API: https://gist.github.com/benman1/3d268be9fdfde0e20d2adb772df233d6 – ben26941 Mar 27 '20 at 14:36
  • Here's the API documentation: https://wordnet.princeton.edu/documentation – ben26941 Mar 27 '20 at 14:37

2 Answers2

2

Have you tried http://code.google.com/p/wordnet-blast/ ?

From the google code site:

DEPENDENCIES:

boost 1.46+

INSTALL:

cmake CMakeLists.txt
make

USAGE:

#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);
}
alvas
  • 115,346
  • 109
  • 446
  • 738
1

this example might help in case of synset navigation. Its good enough to demo the idea although the code is not quite completed yet

copied from 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);
                }
            }
        }
    }
}
Xin
  • 575
  • 4
  • 20