Connecting Tech Pros Worldwide Help | Site Map

GSYN,depth bound transitive relations

Newbie
 
Join Date: Oct 2008
Posts: 1
#1: Oct 30 '08
Expand|Select|Wrap|Line Numbers
  1. #!/usr/bin/env perl
  2. ################################################################
  3. ### gsyn2
  4. ### Plamena Dragieva
  5. ### Oct 30, 2008
  6. ################################################################
  7.  
  8.  
  9.  
  10. use strict;
  11. use warnings;
  12. use Carp;
  13. use English '-no_match_vars';
  14. use utf8;
  15. binmode(STDOUT, ":utf8");
  16.  
  17. use LWP;
  18. use WWW::Mechanize 0.48;
  19. use Encode;
  20.  
  21. my $word = decode_utf8($ARGV[0]);
  22. my $url = "http://www.google.com/search?hl=en&q=%7E$word&btnG=Search";
  23. my $mech = WWW::Mechanize->new(autocheck => 1);
  24. $mech->get($url);
  25. my $page = $mech->content;
  26.  
  27. # Look for some bit of text occuring just before the real results.
  28. # Throw away all text before this.
  29. $page =~ s/.*?Save time//;
  30.  
  31. my %found;
  32.  
  33.  
  34. # This matches anything in bold. Uses non-greedy matching.
  35. while ($page =~ m/<b>(.*?)<\/b>/g) {
  36.     $found{$1}++;
  37. }
  38.  
  39. # Get one more page from Google.
  40. $page = $mech->follow_link( text => "Next", n => 1 )->content;
  41. $page =~ s/.*?Results//;
  42. while ($page =~ m/<b>(.*?)<\/b>/g) {
  43.     $found{$1}++;
  44. }
  45.  
  46. foreach (keys %found) {
  47.     my $key = $_;
  48.     if ($key !~ m/\.\./ && $key !~ m/~$word/) {
  49.         print "$_\n";
  50.     }
  51. }
  52.  
How can I modify this code so that I get a second command line argument?
Here is the task.
Try running the gsyn program (located in /afs/sfs/lehre/dg/perl):

prompt> gsyn beer
11
budweiser
Beer
Ale
ale
20
beer
0.22
beers
724,000,000

You see that this program returns a number of words that are
related or synonymous with 'beer' (or whatever word you choose).
Try some other possibilities. Then try 'gsyn ale' and you get the
word "brewery". Similarly 'gsyn brewery' returns the word
"restaurant." So by transitivity, the word "beer" is related to the
word "restaurant." Modify the gsyn program so that it returns these
transitive relations (to some depth bound).
numberwhun's Avatar
Site Moderator
 
Join Date: May 2007
Location: New Hampshire
Posts: 2,565
#2: Oct 30 '08

re: GSYN,depth bound transitive relations


If you provide a second command line option, then you can reference it with $ARGV[1], the next element in the array you referenced in your code.

Regards,

Jeff
KevinADC's Avatar
Expert
 
Join Date: Jan 2007
Location: Southern California USA
Posts: 4,091
#3: Oct 31 '08

re: GSYN,depth bound transitive relations


Whatever, I just love that descriptive bit of gobbeldy-guk:

depth bound transitive relations

I'm going to try and use that in a sentence one of these days
Reply