Running a Perl module in Qt



  • Hello,
    I am trying to use a perl module called WordNet : : Similarity module that implements a variety of semantic similarity and relatedness measures based on information found in the lexical database.
    I have managed to install this module in Ubuntu,Linux. However, my Perl knowledge is limited. I would like to use this module in my C++ application. I have searched for the way of doing it and I found that QProcess or perlembed can be used for my purpose. Perlembed seemed rather complicated. I believe in Qt and I am hoping that there is an easier way to use the module with C++. Could you give me a small code snippet which achieves what I want.

    The basic use of the module is below.

    @ use WordNet::QueryData; use WordNet::Similarity::path;

    my $wn = WordNet::QueryData->new;

    my $measure = WordNet::Similarity::path->new ($wn);

    my $value = $measure->getRelatedness("car#n#1", "bus#n#2");

    my ($error, $errorString) = $measure->getError();

    die $errorString if $error;
    print "car (sense 1) <-> bus (sense 2) = $value\n";
    @

    I just need to invoke getRelatedness function and use the output in my application.
    Here is the "link ":http://search.cpan.org/dist/WordNet-Similarity/for WordNet : : Similarity module.

    Please help me because I have already started tearing my hair off.



  • You can adapt the sample code on "Call an Applescript from Qt":/wiki/Call_an_AppleScript_from_Qt wiki page for your needs.

    If you have the script on your file system, the easiest way is to call [[Doc:QProcess]]. The docs have an example that should be easy to adapt too.

    The command line to use should be something like

    @
    perl /path/to/your/script.pl
    @

    which would result in calling QProcess like this:

    @
    QProcess perl;
    perl.start("perl", QStringList() << "/path/to/your/script.pl");
    @

    Then connect to the signals or use the synchronous API.

    The other option is to embed a perl interpreter into your application. I don't have experience with that.



  • Hello,
    thank for the response, I have followed the steps you suggested. However, I have some problems.

    As far as I understood, in the code below.

    @ QProcess perl;

        perl.start("perl /Desktop/WordNet-Similarity-2.05/samples/sample.pl vehicle#n#1 car#n#1");
       perl.waitForFinished(-1);
    
       QString p_stdout = perl.readAllStandardOutput();
       QString p_stderr = perl.readAllStandardError();
    
    
       qDebug()  << p_stderr;@
    

    @ perl.start("perl /Desktop/WordNet-Similarity-2.05/samples/sample.pl vehicle#n#1 car#n#1");@

    is supposed to do the same thing when you run the command below in the terminal .

    @perl /Desktop/WordNet-Similarity-2.05/samples/sample.pl vehicle#n#1 car#n#1@

    I will try to explain the problemI got thoroughly.

    Pederson's Wordnet : : Similarity module requires min Wordnet 3.0 and Perl 5 installed. When I run the command in terminal, it works properly and it can locate perl libraries.

    However, when I run exactly the same code in QProcess : : start(), it can locate the libraries.
    I have no idea why I get this problem.

    Could you elaborate what QProcess: : start() does with the given arguments ?

    Thanks



  • Maybe some missing environment variables?



  • That might be the reason but since I am kinda unfamiliar with Perl, I am not exactly sure which libraries should I add.
    I only added the followings.
    DEPENDPATH += /usr/local/lib/perl5
    INCLUDEPATH += /usr/local/lib/perl5

    I thought it's enough.
    Do you have any suggestions?



  • Remove that stuff from the .pro file since it's not relevant at all to your problem. Does the "perl" executable starts if you run it from QProcess? Do you really have the "/Desktop/WordNet-Similarity-2.05/samples/sample.pl" file? (Sounds a very strange path to me)



  • No that's not the absolute path.

    Here is the code:

    @#include <QtCore/QCoreApplication>
    #include <QProcess>

    #include <QDebug>

    int main(int argc, char **argv)

    {

       QProcess perl;
    
        perl.start("perl /home/kirbac/Desktop/WordNet-Similarity-2.05/samples/sample.pl vehicle#n#1 car#n#1");
    
       perl.waitForFinished(-1);
    
       QString p_stdout = perl.readAllStandardOutput();
       QString p_stderr = perl.readAllStandardError();
    
    
       qDebug()  << p_stderr;
    
       return 0;
    

    }@

    And the error I get is the following:

    @Can't locate Wordnet/QueryData.pm in @INC (@INC contains: /etc/perl /usr/local/lib/perl/5.10.1 /usr/local/share/perl/5.10.1 /usr/lib/perl5 /usr/share/perl5 /usr/lib/perl/5.10 ) at /home/kirbac/Desktop/WordNet-Similarity-2.05/samples/sample.pl line 11
    BEGIN failed-- compilation aborted at /home/kirbac/Desktop/WordNet-Similarity-2.05/samples/sample.pl line 111@



  • And "WordNet-Similarity-2.05" doesn't look like the required version 3.0.



  • [quote author="Volker" date="1326116617"]And "WordNet-Similarity-2.05" doesn't look like the required version 3.0.[/quote]

    WordNet-Similarity-2.05 is a module written in Perl. However, WordNet is a large lexical database of English. WordNet-Similarity-2.05 uses WordNet database to measure semantic relatedness between words.



  • I would say you need to actually install the perl module. calling

    @
    make install
    @

    usually is sufficient.

    I'm pretty sure, your program does not work if you call if from the command line outside its build directory as it will be unable to finde the module then too.

    You can try to add a perl library path to the command line:

    @
    perl -I /home/path/lib script.pl
    @



  • bq. I would say you need to actually install the perl module. calling

    Actually I have already installed the module and am able to run it on terminal
    Here are the command I used when I ran it on terminal and outcome.

    @kirbac@ubuntu:~/Desktop/WordNet-Similarity-2.05$ perl samples/sample.pl vehicle#n#1 car#n#1 Loading WordNet... done.
    Creating jcn object... done.
    Creating res object... done.
    Creating lin object... done.
    Creating wup object... done.
    Creating lch object... done.
    Creating hso object... done.
    Creating path object... done.
    Creating random object... done.
    Creating lesk object... done.
    Creating vector object... done.
    Creating vector_pairs object... done.
    JCN Similarity = 0.679238561464479
    RES Similarity = 5.5313491229262
    LIN Similarity = 0.882549308265133
    WUP Similarity = 0.818181818181818
    LCH Similarity = 2.07944154167984
    HSO Similarity = 4
    @

    I think I am calling it from the command line outside its build directory. I am kinda puzzled here.



  • You run it inside the build directory. Read the whole sentence! Because it runs in a specific directory does not mean it runs everywhere.

    cd to /tmp and try to run your script again...



  • @kirbac@ubuntu:~/Desktop/Alternative$ perl /home/kirbac/Desktop/Dene-build-desktop-Desktop_Qt_4_7_4_for_GCC__Qt_SDK__Release/sample.pl banana#n#1 apple#n#1
    Loading WordNet... done.
    Creating jcn object... done.
    Creating res object... done.
    Creating lin object... done.
    Creating wup object... done.
    Creating lch object... done.
    Creating hso object... done.
    Creating path object... done.
    Creating random object... done.
    Creating lesk object... done.
    Creating vector object... done.
    Creating vector_pairs object... done.
    JCN Similarity = 0.0480946368308077
    RES Similarity = 1.36959027657875
    LIN Similarity = 0.1164047451416
    WUP Similarity = 0.434782608695652
    LCH Similarity = 1.04982212449868
    HSO Similarity = 2

    @

    I created an alternative folder and I can run it again.

    I am curious about the exact command the below generates.

    @
    QProcess perl;
    perl.start("perl /home/kirbac/Desktop/WordNet-Similarity-2.05/samples/sample.pl vehicle#n#1 car#n#1");@



  • Doesn't look like a Qt problem - something with calling the perl script is screwed up. Most probably some environment variables or the wrong perl interpreter running the script. Try to use the full path to a perl interpreter. Or check the version of "perl -v" of the QProcess.



  • I agree with you not being a Qt problem. I believe what Qt is capable of and that's why I am trying to find a solution. It's so awkward. Everything seems to be normal.
    I also suspect that I am missing qmake variables.
    Here is the last question before I fall into despair and give up.

    Which variables would you use in the project file?



  • [quote author="magpielover" date="1326119505"
    Which variables would you use in the project file?
    [/quote]

    NONE! Your project builds and runs just fine! The only problem you have is how to setup the command line for the perl interpreter. There's nothing in a .pro file that can you help here.

    The state of affairs is quite clear:
    Perl shows you the include paths, and it cannot find the requested module in there. Go to that directories and check manually. Check that the version of perl called by QProcess is the same as on the command line (depending on the PATH env variable this can differ!). Add the installation directory using -I switch. Ask some Perl gurus...

    As a last resort, use the WordNet libraries directly, it seems to be standard C or C++.



  • I appreciate your effort. I'd better get familiar with perlembed.

    Thanks



  • Despite using perlembed (which introduces another library) you should try to use the WordNet library directly, without using perl at all.



  • [quote author="Volker" date="1326120811"]Despite using perlembed (which introduces another library) you should try to use the WordNet library directly, without using perl at all. [/quote]

    English lexical database WordNet written in C has nothing to do with similarity measurement algorithms. In WordNet database Nouns, verbs, adjectives and adverbs are grouped into sets of cognitive synonyms (synsets), each expressing a distinct concept. Synsets are interlinked by means of conceptual-semantic and lexical relations.

    However, there exist some algorithms which use WordNet tools to measure the semantic relatedness. The module I have been struggling with is a Perl module that implements a variety of semantic similarity and relatedness measures based on information found in the lexical database WordNet. In particular, it supports the measures of Resnik, Lin, Jiang-Conrath, Leacock-Chodorow, Hirst-St.Onge, Wu-Palmer, Banerjee-Pedersen, and Patwardhan-Pedersen.

    Therefore, I cannot use WordNet C code directly for my purpose. I need the implementation of the algorithms I mentioned above.
    I will try to implement the algorithms myself in C++ and release the code for people's good.
    Thanks



  • I would simply avoid embedding the Perl interpreter. It's much more complicated than simply running the perl interpreter.

    Just double check that the working directory is the same, the env variables are the same, etcetera. You need to gain some knowledge about how Perl finds its modules. Read perldoc perlmod, perldoc -f require, perldoc -f use.



  • I've used QProcess to access external applications on my linux box multiple times without flaw. I also had issues like you are having right now being that I could run the application on the command line with no errors, and then I tried using QProcess and had issues all around.

    My solution was following the exact example on the QProcess page using the argument string lists, checking for errors, and all that jazz.

    For example (right from the QProcess documentation)
    @
    QObject *parent;
    ...
    QString program = "./path/to/Qt/examples/widgets/analogclock";
    QStringList arguments;
    arguments << "-style" << "motif";

    QProcess *myProcess = new QProcess(parent);
    myProcess->start(program, arguments);
    @

    Just a thought though. Here's the docs if you haven't already been there. http://developer.qt.nokia.com/doc/qt-4.8/qprocess.html

    I think when I was having issues, the QProcess line recognized some characters differently then what the command line does. So an argument string list was absolutely required for my case. Also, put ' ' around your arguments as well.

    Best of luck.


Log in to reply
 

Looks like your connection to Qt Forum was lost, please wait while we try to reconnect.