Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations strongm on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Complete a word

Status
Not open for further replies.

CJason

Programmer
Oct 13, 2004
223
US
Anyone know of a good/simple/quick way to complete a word in a sentence? For example, I want to replace: "DEL" or "DELE" or "DELET" with: "DELETE".

Thanks in advance!
 
Code:
$foo = 'I want to DELETE DELET DEL DELE DELET';
$foo =~ s/\b(DELET|DELE|DEL)\b/DELETE/g;
print $foo;




------------------------------------------
- Kevin, perl coder unexceptional! [wiggle]
 
Thanks, Kevin. That's kind of where I was going as well...I just wanted to make sure I wasn't missing something simple. Thanks again!
 
I'll propose a more general, though very simple, method to do that. It requires of course a dictionary (a database) of the allowed substitutions/completions, as with natural languages you can't have a general programmatic rule for that: of course partial words that are also full words (e.g. be,before) cannot be handled correctly. Another example of what can't be done is if you have delete and deleted in the dictionary.
Code:
%completions=('del','delete','dele','delete','delet','delete');
for my$sentence(@sentences){
  my@words=split/\s/,$sentence;
  for my$word(@words){
    my$lcw=lc$word;
    if(exists$completions{$lcw}){
      if(ord$word<91){
        $word=ucfirst$completions{$lcw};
      }else{
        $word=$completions{$lcw};
      }
    }
  }
  $sentence=join(' ',@words);
}

Franco
: Online engineering calculations
: Magnetic brakes for fun rides
: Air bearing pads
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top