returning calculation results in perl scripts

Discussion in 'Perl Misc' started by spanish26@gmail.com, Jan 21, 2009.

  1. Guest

    Hello,

    I am running a program using perl scripts to calculate the proximity
    between two search terms when they are entered in the form box, i can
    successfully view the form boxes, but when i click on the calculate
    button after entering the term, i do not get any result, the values do
    not show any result

    E.g lets say i enter two terms "egg" term 1 and "fish" term 2 and i
    click on the calculate button, i am supposed to see the following
    results:
    NGD(x,y) = some value

    Term1: +:"egg"
    f(x) = some value
    log f(x) = some value

    Term 2: +"fish"
    f(y) = some value
    log f (y) = some value

    Intersection: +"egg" + "fish"
    f(x,y) = some value

    i dont get any result when click the calculate.

    The source code is below :

    #!"C:\Program Files\xampp\perl\bin\perl.exe"
    # ngd-calculator.cgi
    #
    # wjt
    # http://history.uwo.ca/faculty/turkel/
    #
    # 5 aug 2006

    use SOAP::Lite;
    use CGI;
    use POSIX qw(log10);
    use List::Util qw(max min);

    # Google API developer's key
    my $google_key = '<Insert Key Here>';

    # Google WSDL
    my $google_wsdl = "http://api.google.com/GoogleSearch.wsdl";
    # my $google_wsdl = "./GoogleSearch.wsdl";

    # start, maxResults, filter, restrict, safeSearch, lr, ie, oe
    my @params = (0, 10, 0, '', 0, '', 'utf-8', 'utf-8');

    # Do Google search and return count
    sub do_search {
    unshift (@params, ($google_key, $_[0]));
    my $result =
    SOAP::Lite
    -> service($google_wsdl)
    -> doGoogleSearch(@params);
    shift @params;
    shift @params;
    return $result->{estimatedTotalResultsCount};
    }

    # Create the search page
    $query = new CGI;
    print $query->header;
    print $query->start_html('NGD Calculator');
    print "<H1>Normalized Google Distance (NGD) Calculator</H1>";

    print '<p>';
    print 'For information about NGD see Rudi Cilibrasi and Paul Vitanyi,
    "';
    print '<a href="http://www.arxiv.org/PS_cache/cs/pdf/
    0412/0412098.pdf">';
    print 'Automatic Meaning Discovery Using Google</a>."';
    print '</p>';

    # Print the search box form
    print $query->startform;
    print '<strong>Enter term 1</strong> ',$query->textfield('term1');
    print '<br />';
    print '<strong>Enter term 2</strong> ',$query->textfield('term2');
    print '<br />';
    print $query->submit('form_1','Calculate');
    print $query->endform;
    print '<br />';

    $x = ''; $y = ''; $xy = '';
    $x = '+"' . $query->param('term1') . '"';
    $y = '+"' . $query->param('term2') . '"';
    $xy = $x . " " . $y;

    $fx = 1; $fy = 1; $fxy = 1;
    $logfx = 0; $logfy = 0; $logfxy = 0; $logm = 0;
    $maxlogfxy = 0; $minlogfxy = 0;
    $ngd = 0;

    # Best guess as of Jan 2006
    $m = 11828505634;

    if ($x && $y) {

    # Determine frequencies
    $fx = do_search( $x );
    $fy = do_search( $y );
    $fxy = do_search( $xy );

    # Determine logarithms
    $logm = log10( $m );
    $logfx = log10( $fx );
    $logfy = log10( $fy );
    $logfxy = log10( $fxy );

    # Determine max and min
    @fxy = ($logfx, $logfy);
    $maxlogfxy = max @fxy;
    $minlogfxy = min @fxy;

    # Calculate NGD
    $ngd = ($maxlogfxy - $logfxy) / ($logm - $minlogfxy);

    print 'NGD(x,y) = ' . $ngd . '<br /><br />';

    print 'Term 1: ' . $x . '<br />';
    print 'f(x) = ' . $fx . '<br />';
    print 'log f(x) = ' . $logfx . '<br /><br />';

    print 'Term 2: ' . $y . '<br />';
    print 'f(y) = ' . $fy . '<br />';
    print 'log f(y) = ' . $logfy . '<br /><br />';

    # print 'max(log f(x),log f(y)) = ' . $maxlogfxy . '<br />';
    # print 'min(log f(x),log f(y)) = ' . $minlogfxy . '<br /><br />';

    print 'Intersection: ' . $xy . '<br />';
    print 'f(x,y) = ' . $fxy . '<br />';
    print 'log f(x,y) = ' . $logfxy . '<br /><br />';

    print 'M: ' . $m . '<br />';
    print 'log M: ' . $logm . '<br />';
    }

    print qq{<P><A HREF="http://history.uwo.ca/faculty/turkel">Digital
    History at Western</A>};
    print $query->end_html;

    Thank you
     
    , Jan 21, 2009
    #1
    1. Advertising

  2. wrote in news::

    > I am running a program using perl scripts to calculate the proximity
    > between two search terms when they are entered in the form box, i can
    > successfully view the form boxes, but when i click on the calculate
    > button after entering the term, i do not get any result, the values do
    > not show any result


    I do not have a Google API key so I can't try out your code. AFAIK,
    Google no longer hands out keys for that service:
    http://google-code-updates.blogspot.com/2006/12/beyond-soap-search-api.html

    I will point out that you need to divide the problem into two separate
    questions:

    1) Are you getting back results?

    2) Are those results being output properly?

    As I said, I cannot look into (1) above. (2) is a stealth CGI question,
    not a Perl question, but let's look at the code below.

    > #!"C:\Program Files\xampp\perl\bin\perl.exe"
    > # ngd-calculator.cgi
    > #
    > # wjt
    > # http://history.uwo.ca/faculty/turkel/
    > #
    > # 5 aug 2006


    Here, you are missing:

    use strict;
    use warnings;

    Please see the posting guidelines for why you should include
    those pragmas so that you can help yourself and help others help
    you more effectively.

    > use SOAP::Lite;
    > use CGI;
    > use POSIX qw(log10);
    > use List::Util qw(max min);


    <snip>

    Code accessing Google Search service. You should test this part
    separately from the command line.

    </snip>

    > # Create the search page
    > $query = new CGI;


    my $query = CGI->new;

    Avoid indirect object notation.

    > print $query->header;
    > print $query->start_html('NGD Calculator');
    > print "<H1>Normalized Google Distance (NGD) Calculator</H1>";


    Here, you are mixing CGI.pm's output generation routines with plain HTML.
    I would recommend using HTML::Template or some other similar solution
    to separate presentation from logic.

    Also, by default, CGI.pm outputs XHTML in which upper case tags
    are not valid AFAIK.

    In any case, you should try the script from the command line to see
    what happens there. See
    http://search.cpan.org/~lds/CGI.pm-3.42/CGI.pm#DEBUGGING

    Please provide a small, self-contained example others can run from
    the command line that still exhibits the problem.

    Sinan
    --
    A. Sinan Unur <>
    (remove .invalid and reverse each component for email address)

    comp.lang.perl.misc guidelines on the WWW:
    http://www.rehabitation.com/clpmisc/
     
    A. Sinan Unur, Jan 21, 2009
    #2
    1. Advertising

  3. Guest

    Thank you, i signed for the Google API developers key on Monday and i
    was issued the key.
    I ran the program on the command prompt, but got error. when
    debugging.


    On Jan 21, 9:11 pm, "A. Sinan Unur" <> wrote:
    > wrote innews::
    >
    > > I am running a program using perl scripts to calculate the proximity
    > > between two search terms when they are entered in the form box, i can
    > > successfully view the form boxes, but when i click on the calculate
    > > button after entering the term, i do not get any result, the values do
    > > not show any result

    >
    > I do not have a Google API key so I can't try out your code. AFAIK,
    > Google no longer hands out keys for that service:http://google-code-updates.blogspot.com/2006/12/beyond-soap-search-ap...
    >
    > I will point out that you need to divide the problem into two separate
    > questions:
    >
    > 1) Are you getting back results?
    >
    > 2) Are those results being output properly?
    >
    > As I said, I cannot look into (1) above. (2) is a stealth CGI question,
    > not a Perl question, but let's look at the code below.
    >
    > > #!"C:\Program Files\xampp\perl\bin\perl.exe"
    > > # ngd-calculator.cgi
    > > #
    > > # wjt
    > > #http://history.uwo.ca/faculty/turkel/
    > > #
    > > # 5 aug 2006

    >
    > Here, you are missing:
    >
    > use strict;
    > use warnings;
    >
    > Please see the posting guidelines for why you should include
    > those pragmas so that you can help yourself and help others help
    > you more effectively.
    >
    > > use SOAP::Lite;
    > > use CGI;
    > > use POSIX qw(log10);
    > > use List::Util qw(max min);

    >
    > <snip>
    >
    > Code accessing Google Search service. You should test this part
    > separately from the command line.
    >
    > </snip>
    >
    > > # Create the search page
    > > $query = new CGI;

    >
    > my $query = CGI->new;
    >
    > Avoid indirect object notation.
    >
    > > print $query->header;
    > > print $query->start_html('NGD Calculator');
    > > print "<H1>Normalized Google Distance (NGD) Calculator</H1>";

    >
    > Here, you are mixing CGI.pm's output generation routines with plain HTML.
    > I would recommend using HTML::Template or some other similar solution
    > to separate presentation from logic.
    >
    > Also, by default, CGI.pm outputs XHTML in which upper case tags
    > are not valid AFAIK.
    >
    > In any case, you should try the script from the command line to see
    > what happens there. Seehttp://search.cpan.org/~lds/CGI.pm-3.42/CGI.pm#DEBUGGING
    >
    > Please provide a small, self-contained example others can run from
    > the command line that still exhibits the problem.
    >
    > Sinan
    > --
    > A. Sinan Unur <>
    > (remove .invalid and reverse each component for email address)
    >
    > comp.lang.perl.misc guidelines on the WWW:http://www.rehabitation.com/clpmisc/
     
    , Jan 21, 2009
    #3
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Jeremy
    Replies:
    1
    Views:
    365
    Jim White
    Feb 18, 2004
  2. Badass Scotsman
    Replies:
    1
    Views:
    368
    S. Justin Gengo
    Apr 24, 2006
  3. Vedran

    Presenting calculation results

    Vedran, Sep 10, 2008, in forum: Python
    Replies:
    1
    Views:
    296
    Marco Bizzarri
    Sep 10, 2008
  4. KsAdmin

    Formatting the results of a calculation

    KsAdmin, Jul 6, 2004, in forum: Javascript
    Replies:
    22
    Views:
    243
    Dr John Stockton
    Jul 7, 2004
  5. Replies:
    13
    Views:
    535
    Anno Siegel
    Sep 10, 2007
Loading...

Share This Page