Jump to content

regular expression and abbreviation challenge


figo2476

Recommended Posts

Hi, I have been given this question and would like to get some ideas. Any thought is appreciated.

 

a)"RADIUS" is an abbreviation of "Remote Authentication Dial In User Service"

b)"TCP/IP" is an abbreviation of "Transmission Control Protocol / Internet Protocol"

c)"FedEx" is an abbreviation of "Federal Express"

d)"Bigpost is an abbreviation of "Big Post"

e)"Cisco" is an abbreviation of "San Francisco"

f)"ragnat" is a (rather silly) abbreviation of "Teragen International"

 

In the above list:

a) and b) are simple abbreviations (acronyms). The abbreviation is formed by taking the first letter of each word in the list.

c) and d) are complex abbreviations, they consist of one or more letters from the beginning of each word in the list.

e) and f) are substring abbreviations, they may take any part of any word as long as the letters appear in the original presented order.

 

 

 

I am able to match them, but it is still NOT able to tell it is simple, complex or substring abbreviations. (I wonder how)

 

Here is the code in PHP:

 

$abbr = strtolower($abbr);

$longstr = strtolower($longstr);

 

$pattern = implode('.*?', str_split($abbr));

$condi = preg_match("/$pattern/", $longstr);

if($condi == true)

{

    echo "Match!";

}

else

{

    echo "Doesn't match!";

}

Link to comment
Share on other sites

Well, by the looks of it, you want to test acronyms by comparing the string to the same string put into uppercase, if they match, it an acronym. As for complex and substring, how would you tell them apart if you didn't know which they were or what they stood for?

Link to comment
Share on other sites

i have an idea.

 

start the same:

$abbr = strtolower($abbr);
$longstr = strtolower($longstr);

 

then we change thigs:

 

1) explode $longstr around the space character (" ").

2) set the array pointer to the first element in the new $longstr array.

3) go through the current $longstr element 1 char at a time, and compare each to the first char of abbr. if you have a match, cut the first char off $abbr.

4) continue to the next char in the current $longstr element. if it matches the "new" 1st char in $abbr,  go back to step 3 again. if it doesn't, go to step 5.

5) move on the the next element in $longstr array, and go back to step 3.

 

when you run out of elements, if strlen($abbr) == 0, the $abbr can be considered an abbreviation of $longstr. otherwise, it ain't.

 

i know this is an unpleasant solution, and it ain't accurate ("if" would be an abbreviation of "spiffy" by this method), and it reminds me a bit of homework in "introduction to C" courses at uni (lol), but it's the best "all in 1" i can come up with.

 

good luck.

Link to comment
Share on other sites

Edited to remove code.  It was an interesting problem to solve (which, afaik it seems to work just fine), but it turns out I feel like if I give you my code I would be helping you cheat, so as much as I'd like to post my code for bragging rights, I can't.  I do, however, feel it would be okay for me to point you in the right direction, as far as what I did.  I'll be posting that shortly.

 

Link to comment
Share on other sites

Simple Acronym

Simple acronyms are fairly easy to figure out. The idea is to check to see if you have an equal amount of letters in the acronym as words in the meaning, and then check to see if each acronym letter corresponds to the first letter in each word.

 

There are several ways you can accomplish that. One way is for you to explode the meaning into any array, loop through each word and use substr to build a string of each of the first letters, and then compare that to the acronym.

 

Hint: Before anything else, get rid of special characters like dots and slashes. Some acronyms use them (like TCP/IP or M.A.S.H.). A Simple str_replace should (mostly) do the trick.

 

Substring Acronym

Substring Acronyms are the easiest to figure out. All you really have to do is str_split the acronym letters, then implode it back into a string, with a "match everything" regex as the glue (be sure to add the regex to the beginning and end, as well).  Then simply use preg_match to see if you've got a winner. 

 

Hint: This is the last acryonym type you will want to check for, because it will match any of the other types, as well. Technically, all Acryonyms are substring acronyms.

 

Complex Acronym

Complex Acronyms are (maybe just IMO) the hardest to figure out. At first look, it seems like it should be fairly easy. Especially if people do you a favor and capitalize the acronym in the "right" places (like FedEx). Unfortunately, that might not always be the case (like in Bigpost).

 

And you can't just loop through each letter of the acronym, check if there's a match with the current word, move on to the next word when the match stops. Why?  Take a look at FedEx, for example. If you were to do it that way, you would end up matching FedE in the first word, and then your script would then try to look for 'x' as the first letter of the next word, instead of the intended 'e.'  So what can you do? 

 

Hint: Here is some info echoed out inside of some loops.  I even used a 3 word acronym as an example for more output:

("FedExDel" => "Federal Express Delivery")

fedexdel delivery

edexdel deliver

dexdel delive

exdel deliv

xdel deli

del del

fedex expre

edex expr

dex exp

ex ex

fed fed

 

Well there you have it.  Good luck with figuring out your homework. 

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.