Loading ...
Sorry, an error occurred while loading the content.

Re: [PBML] Re: recursion help

Expand Messages
  • Paul Archer
    Oh, I forgot to mention: if anyone wants to do a quick code review and tell me if there s an easier/better way of doing this task, I d really appreciate it. I
    Message 1 of 3 , Jan 27, 2005
    • 0 Attachment
      Oh, I forgot to mention: if anyone wants to do a quick code review and tell
      me if there's an easier/better way of doing this task, I'd really appreciate
      it. I know that just because I solved the problem doesn't mean I solved it
      the best possible way.

      3:11pm, Paul Archer wrote:

      >
      > FWIW, I think I solved my problem by 'undef'ing the %seen_keywords hash
      > after every iteration of my foreach loop. That way I start fresh on each
      > iteration of the loop, but still avoid infinite loops each time I start the
      > recursion.
      >
      > Paul
      >
      > 1:34pm, Paul Archer wrote:
      >
      >> I described this the other day, and got some help. I've now got code, but
      >> it's clunky and not working right.
      >> Here's the situation: I have a hash whose keys are keywords, and whose values
      >> are anon lists of more keywords that I want the first keyword to expand into.
      >> For example:
      >> $syns{winston}=[ qw/cat pet/ ];
      >> $syns{cat} =[ qw/animal/ ];
      >>
      >> I want to expand the keyword of Winston out to 'winston', 'cat', 'pet', and
      >> 'animal'.
      >>
      >> Here's what I've got so far:
      >>
      >> foreach (sort keys %syns) {
      >> recurse_synonyms($_,\%syns);
      >> }
      >>
      >> our $count;
      >> my %seen_keywords;
      >> sub recurse_synonyms {
      >> my $cur_keyword=shift;
      >> my %syns=%{$_[0]};
      >> foreach my $expansion_keyword (@{$syns{$cur_keyword}}) {
      >> print "we've seen keyword: $expansion_keyword\n" if defined
      >> $seen_keywords{$expansion_keyword};
      >> next if defined $seen_keywords{$expansion_keyword};
      >> if (defined $syns{$expansion_keyword}) {
      >> # print "keyword $expansion_keyword is recursive\n";
      >> $seen_keywords{$expansion_keyword}++;
      >> # print "\$seen_keywords{$expansion_keyword} is now
      >> $seen_keywords{$expansion_keyword} for expansion keyword of
      >> $expansion_keyword\n";
      >> push @{$syns{$cur_keyword}},
      >> recurse_synonyms($expansion_keyword, \%syns);
      >> # print "count is ", ++$count, " for expansion keyword
      >> of $expansion_keyword\n";
      >> }
      >> }
      >> return @{$syns{$cur_keyword}};
      >> }
      >>
      >>
      >> The problems I'm having are that if I don't use the %seen_keywords hash to
      >> limit what I expand, I get repeated (posibilly) infinite loops if a keyword
      >> expands to two keywords that each have expansions. (See alphabet data in
      >> example.)
      >>
      >> If I *do* use the %seen_keywords hash, then I have problems because I'll only
      >> expand a keyword once. So if it shows up twice, the second keyword is out of
      >> luck. (See monkey/primate data in example.)
      >>
      >> %syns = {
      >> 'A' => [
      >> 'B',
      >> 'C'
      >> ],
      >>
      >> 'B' => [
      >> 'D',
      >> 'E'
      >> ],
      >>
      >> 'C' => [
      >> 'F',
      >> 'G'
      >> ],
      >>
      >> 'D' => [
      >> 'H',
      >> 'I'
      >> ],
      >>
      >> 'Z' => [
      >> 'A'
      >> ],
      >>
      >> 'monkey' => [
      >> 'primate'
      >> ],
      >>
      >> 'primate' => [
      >> 'animal'
      >> ],
      >>
      >> 'patas' => [
      >> 'monkey',
      >> 'zoo'
      >> ],
      >>
      >> 'chimpanzee' => [
      >> 'primate',
      >> 'animal',
      >> 'zoo'
      >> ],
      >>
      >> };
      >>
      >>
      >>
      >
      > ---------------------------------------------
      > There are two major products that come out of
      > Berkeley: LSD and UNIX. We don't believe this
      > to be a coincidence. - Jeremy S. Anderson
      > ---------------------------------------------
      >
      >
      > Unsubscribing info is here: http://help.yahoo.com/help/us/groups/groups-32.html
      > Yahoo! Groups Links
      >
      >
      >
      >
      >
      >
      >

      -------------------------------------------------------------------
      Education in the U.S. has improved greatly under the George W. Bush
      administration Now, 60% of 6th graders read at a higher level
      than the President. Under President Clinton, few if any achieved
      these levels of literacy.
      -------------------------------------------------------------------
    Your message has been successfully submitted and would be delivered to recipients shortly.