If your input data is such that just finding two or more Consecutive Capitalized Words is sufficient to identify proper names, then your logic can be very "local" as well as simple -- you wouldn't need more than two lines of text in memory at any one time with Perl. But avoiding false alarms and misses takes some careful thought. Aside from the two false alarms in my first two sentences, your current method would miss the next case completely. (Mike O'Conner, whoever he is, would be missed if he's mentioned at the beginning of a quotation, as well as a parenthetical. And what about George McGovern?)

Leaving that aside, this may be one of those rare cases where a "goto" statement is worth having. Consider:

use strict; my @tokens = (); my $properName = ""; while (<>) { push @tokens, split; FINDCAP: # the "until" loop skips non-capitalized tokens until ( @tokens == 0 or $tokens[0] =~ /^\W*[A-Z][\'A-Za-z]*\b/ ) { shift @tokens; } # the "while" loop accumulates consecutive capitalized toke +ns, # if any were found that caused us to break out of the "unt +il" while ( @tokens and $tokens[0] =~ /^\W*([A-Z][\'A-Za-z]*)\b/ ) { $properName .= $1 . " "; shift @tokens; } # go into the next block if there are still tokens left # (this means we haven't reached the end of this line) if ( @tokens ) { if ( $properName =~ / [A-Z]/ ) { print $properName,$/; # print if $properName has >1 word } $properName = ""; # reset to empty string goto FINDCAP; # look through the remainder of this line } # that block was skipped if there are no tokens left # so we loop back to the outer "while" loop to get the # next line, and append its words to @tokens # (and $properName, if not empty, remains intact # for appending the next Capitalized Token, if any) }
Because it ignores punctuation, this will do the wrong thing in a case like "I saw John Brown. He was dead." Good luck! (update: enhanced the final block of commentary in the code, and made the title more relevant)

In reply to Looking for proper names in text by graff
in thread Out of memory by Anonymous Monk

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post, it's "PerlMonks-approved HTML":



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.