By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
429,078 Members | 2,164 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 429,078 IT Pros & Developers. It's quick & easy.

GSYN,depth bound transitive relations

P: 1
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).
Oct 30 '08 #1
Share this Question
Share on Google+
2 Replies


numberwhun
Expert Mod 2.5K+
P: 3,503
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
Oct 30 '08 #2

KevinADC
Expert 2.5K+
P: 4,059
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
Oct 31 '08 #3

Post your reply

Sign in to post your reply or Sign up for a free account.