Created
December 21, 2018 18:23
-
-
Save bmcminn/24a2f32427371301025db2a823415a66 to your computer and use it in GitHub Desktop.
A naive acronym generator
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| <?php | |
| $things = [ | |
| // WORKING TEST CASES | |
| 'Aangenaam Door Vermaak En Nuttig Door Ontspanning' => 'ADVENDO', // "Pleasant by entertainment and useful by relaxation")[28][29] | |
| 'abdominal aortic aneurysm' => 'AAA', | |
| 'Amateur Athletic Association' => 'AAA', | |
| 'American Automobile Association' => 'AAA', | |
| 'American Standard Code for Information Interchange' => 'ASCII', | |
| 'anti-aircraft artillery' => 'AAA', | |
| 'AOL Instant Messenger' => 'AIM', | |
| 'Asistencia, Asesoría y Administración' => 'AAA', | |
| 'automated teller machine' => 'ATM', | |
| 'British Broadcasting Company' => 'BBC', | |
| 'compact disc' => 'CD', | |
| 'digital video disc' => 'DVD', | |
| 'frequently asked questions' => 'FAQ', | |
| 'GNU Image Manipulation Program' => 'GIMP', | |
| 'Graphics Interchange Format' => 'GIF', | |
| 'GTK+ AOL Instant Messenger' => 'GAIM', | |
| 'human immunodeficiency virus' => 'HIV', | |
| 'individual retirement account' => 'IRA', | |
| 'Institute of Electrical and Electronics Engineers' => 'IEEE', | |
| 'International Union of Pure and Applied Chemistry' => 'IUPAC', | |
| 'Joint Photographic Experts Group' => 'JPEG', | |
| 'light amplification by stimulated emission of radiation' => 'Laser', | |
| 'liquid-crystal display' => 'LCD', | |
| 'National Aeronautics and Space Administration' => 'NASA', | |
| 'National Association for the Advancement of Colored People' => 'NAACP', | |
| 'National Collegiate Athletic Association' => 'NCAA', | |
| 'NOAD ADVENDO Combinatie' => 'NAC', | |
| 'Nooit Opgeven Altijd Doorgaan' => 'NOAD', // "Never give up, always persevere") | |
| 'North Atlantic Treaty Organization' => 'NATO', | |
| 'original equipment manufacturer' => 'OEM', | |
| 'personal identification number' => 'PIN', | |
| 'self-contained underwater breathing apparatus' => 'Scuba', | |
| 'Structured Query Language' => 'SQL', | |
| 'United States of America' => 'USA', | |
| 'Very High Speed Integrated Circuit' => 'VHSIC', | |
| 'VHSIC hardware description language' => 'VHDL', | |
| 'Hypertext Preprocessor' => 'PHP', | |
| 'Hypertext Markup Language' => 'HTML', | |
| 'Cascading Stylesheets' => 'CSS', | |
| 'Javascript' => 'JS', | |
| // KNOWN FAILURE CASES DUE TO SOPHISTICATED RULES | |
| 'alpha-methyl-phenethylamine' => 'Amphetamine', | |
| 'Command, Control, Communications, Computers, Intelligence, Surveillance, Target Acquisition, and Reconnaissance' => 'C4ISTAR', // (C-four Istar) | |
| 'compact disc read-only memory' => 'CD-ROM', | |
| 'Geheime Staatspolizei' => 'Gestapo', // ('Secret State Police') | |
| 'International Information Systems Security Certification Consortium' => '(ISC)²', // (ISC squared) | |
| 'Minnesota Mining and Manufacturing Company' => '3M', // (three M) originally | |
| 'Museum of Modern Art' => 'MOMA', | |
| 'oculocutaneous albinism' => 'OCA', | |
| 'paracoccidioidomycosis' => 'PCM', | |
| 'polymorphonuclear leukocytes' => 'PMN', | |
| 'radio detection and ranging' => 'Radar', | |
| 'San Francisco Museum of Modern Art' => 'SFMOMA', | |
| 'World Wide Web Consortium' => 'w3C', // (W-three C) | |
| ]; | |
| /** | |
| * Naive acronym generator | |
| * @param string $str The string we wish to build an acronym from | |
| * @return string The resulting acronym without explicit case formatting | |
| */ | |
| function acronyze(string $str) { | |
| $str = preg_replace('/[-,\.]/', ' ', $str); | |
| $str = preg_replace('/\s+/', ' ', $str); | |
| $parts = explode(' ', $str); | |
| $res = ''; | |
| foreach ($parts as $part) { | |
| $part = trim(strtolower($part)); | |
| $continue = false; | |
| switch($part) { | |
| case 'a': | |
| case 'and': | |
| case 'by': | |
| case 'for': | |
| case 'of': | |
| case 'the': | |
| case 'y': | |
| $continue = true; | |
| break; | |
| } | |
| if ($continue) { | |
| continue; | |
| } | |
| $res .= substr($part, 0, 1); | |
| } | |
| return strtoupper($res); | |
| } | |
| foreach ($things as $label => $proof) { | |
| $result = acronyze($label); | |
| $bool = strtoupper($result) === strtoupper($proof) ? 'true ' : 'false'; | |
| echo "{$bool} :: {$proof} - {$result} - {$label}". PHP_EOL; | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment