r/dailyprogrammer • • Dec 19 '14
[2014-12-19] Challenge #193 [Easy] Acronym Expander
Description
During online gaming (or any video game that requires teamwork) , there is often times that you need to speak to your teammates. Given the nature of the game, it may be inconvenient to say full sentences and it's for this reason that a lot of games have acronyms in place of sentences that are regularly said.
Example
gg : expands to 'Good Game'
brb : expands to 'be right back'and so on...
This is even evident on IRC's and other chat systems.
However, all this abbreviated text can be confusing and intimidating for someone new to a game. They're not going to instantly know what 'gl hf all'(good luck have fun all) means. It is with this problem that you come in.
You are tasked with converting an abbreviated sentence into its full version.
Inputs & Outputs
Input
On console input you will be given a string that represents the abbreviated chat message.
Output
Output should consist of the expanded sentence
Wordlist
Below is a short list of acronyms paired with their meaning to use for this challenge.
- lol - laugh out loud
- dw - don't worry
- hf - have fun
- gg - good game
- brb - be right back
- g2g - got to go
- wtf - what the fuck
- wp - well played
- gl - good luck
- imo - in my opinion
Sample cases
input
wtf that was unfairoutput
'what the fuck that was unfair'input
gl all hfoutput
'good luck all have fun'Test case
input
imo that was wp. Anyway I've g2goutput
????
16
u/adrian17 1 4 Dec 19 '14 edited Dec 20 '14
Python 3:
Simple one-liner (not counting dictionary of acronyms) (doesn't work for test case, doesn't recognize "wp.". I wanted to avoid using regex substitution for as long as possible) (edit: there is an improved version of this suggested in this comment):
Kinda one-liner:
Normal solution where regex does 99% of work:
8
u/G33kDude 1 1 Dec 19 '14
My solution in AutoHotkey
7
u/adrian17 1 4 Dec 19 '14 edited Dec 20 '14
Also a C++11 solution with regex, analogue to my Python solution.
5
u/Stenwulf Dec 19 '14
Java
5
u/Azzu Dec 20 '14
Java
I'm using a regex pattern that matches whole words. I'm only replacing words if they are in the abbreviation list, everything else (including whitespace, punctuation, etc.) of the original String is retained.
4
u/bluelite Dec 19 '14
Quick n' dirty Perl
4
u/marchelzo Dec 20 '14
Here's a solution in Haskell. It doesn't behave perfectly, but making it do so would mean losing a lot of elegance.
The problem is that it will turn things like:
into
because of the way
wordsandunwordsfrom the Prelude work.4
u/rHermes Dec 20 '14
First try at a C implementation, with some obvious limitations.
8
u/NewbornMuse Dec 19 '14
The "quick and dirty and works like 95% of the time and breaks on wrong input" way that I love for little pet programs like this is pretty simple. Python even has a function that does pretty much this, reducing it to a three-liner (you could probably even do one really long one).
However, if you like some extra challenge, think about corner cases, think about how your program should handle them, and make it handle them that way. You can go pretty much as deep as you want. Examples:
2
u/tt0nic Dec 19 '14
First time posting here and I'd love to give this one a try in Ruby!
Test case output:
2
u/Elite6809 1 1 Dec 19 '14
Learning
bashscripting; decided to be purposely obtuse. Never had to quadruple-escape something before.2
u/Davipb Dec 19 '14 edited Dec 19 '14
Did it in C#, using Regular Expressions.
It preserves all punctuation, recognizes acronyms even if they have a punctuation after them (as in the test case, "wp.") and doesn't replace acronyms inside words (egg doesn't turn into egood game, for example)
2
u/Godspiral 3 3 Dec 19 '14
in J, with dict built from clipboard
dict =: dltb each '-' cut &> cutLF wdclippaste ''
in my opinion that was well played . Anyway I've got to go
2
u/[deleted] Dec 20 '14 edited Dec 22 '18
deleted 0.1127 What is ^^^this?
2
u/darthjoey91 Dec 20 '14
Sed script: acronymexpander.sed
Execution:
2
u/jnazario 2 0 Dec 20 '14 edited Dec 20 '14
F#. took a bit of tweaking to preserve punctuation in the challenge input.
2
u/dunnowins Dec 20 '14
A rather boring Ruby solution.
2
u/asymtex Dec 20 '14
Python 3!
Output
2
u/TopLOL Dec 20 '14
C++ I used tokens and a map to implement my solution. Only thing wrong with it is that the output omits any punctuation. After looking at some of the other solutions I think i need to use Regex.
Sample Input: imo that was wp. Anyway I've g2g
Sample Output:
2
u/MysteryForumGuy 0 0 Dec 21 '14 edited Dec 21 '14
Java
I'd really like some constructive criticism on this. Whether it's good or bad. I'm not really sure myself, so it would be good to know.
My first solution worked, but got stuck on "wp." because of the punctuation. My new solutions works with punctuation and with pretty much anything I throw at it.
Pretty short for Java in my opinion. It's only 8 lines of the actual logic that does the work.
Code
Input: "imo that was wp. Anyway I've g2g"
Output: "in my opinion that was well played. Anyway I've got to go"
2
u/gregsaw Dec 21 '14
Java time
2
u/hassanchug Dec 21 '14
JavaScript—wouldn't mind any feedback.
2
u/sh33pUK Dec 21 '14
Python
I'm still new to programming but I've been messing with web scraping stuff recently so I thought I'd take a different approach. Instead of using the dictionary provided I used a web scraper to get definitions for the abbreviations. The script starts off by checking every word in the phrase against a wordlist, then it gets urbandictionary pages in JSON format, then searches the description for phrases like "acronym for". If it finds one it strips it from the front of the description, if it doesn't it just assumes the description starts with the acronym. Then, whether or not the beginning's been stripped it looks for the number of spaces needed to make the nmber of words in the acronym and removes the rest of the string.
It works ok, but it has problems when it gets the wrong definition or the definition's written in an odd way.
The test cases come out quite poorly unfortunately.
Input: wtf that was unfair
Output: the world taekwond that was unfair
Input: gl all hf
Output: good luck all have fun
Input:imo that was wp. Anyway I've g2g
Output: in my opinion that was well played anyway is as beautifu got to go
2
u/Manic0892 Dec 22 '14
C
2
u/Spaaze Dec 26 '14
Javascript: Did something else than the other Javascripters around here, probaly not the prettiest solution but hey it works!
2
u/Coneicus Jan 04 '15
Python
First submission! Had to google around to help with the regular expression and found a solution. Can pretty much understand everything except this one bit:
Can anyone help explain the above? General feedback would be nice as well.
2
u/vivalaBuffy Jan 06 '15
Java:
My Java solution:
3
u/jetRink Dec 19 '14 edited Dec 19 '14
Clojure. Preserves punctuation
Output
3
u/rockybaboon Dec 20 '14
Did it in C the complicated way: wihout external functions besides i/o
However it requires one whitespace around acronyms, which means "wp." for example doesn't work.
I used the "a string is an array of chars" approach to the logic of this
1
u/RelevantJesse Dec 19 '14
C#
1
u/slwz Dec 19 '14
in ruby
1
u/[deleted] Dec 20 '14 edited Dec 20 '14
Java, preserving punctuation use.
1
u/diclus4 Dec 20 '14
Ruby
1
u/Eli485 Dec 20 '14 edited Dec 20 '14
Incredibly new to programming here with a poorly and probably made much harder than necessary (also long) Scala solution:
Problems with this code include:
Output of test:
1
u/thestoicattack Dec 20 '14
bash:
1
u/binaryblade Dec 20 '14 edited Dec 20 '14
golang ,this took me way longer than I should have but here we go. I would appreciate commentary as I'm using these challenges to learn go.
EDIT:Fixed a subtle bug that omitted things that weren't gamer speak
1
u/mpatraw Dec 20 '14
Here's mine in C. Doesn't use regex and handles the "imo that was wp. Anyway I've g2g" case: https://gist.github.com/mpatraw/c3fe0bd2440662023db9
1
u/PalestraRattus Dec 20 '14 edited Dec 20 '14
C# Just simple swaps using the native String.Replace function. It would be limited by the string char max, but that was accounted for in the form code. If you needed a more robust solution there'd be better ways.
*Update, for giggles compressed the replace functions into 1 line
Form wrapper (http://i.imgur.com/LpydL1f.png)
1
u/[deleted] Dec 20 '14
Python
I should definitely learn how into regex sometime.
1
u/aintso Dec 20 '14
heyyyy
here it is in Haskell. Criticism and suggestions welcome.
1
u/jameis_harden Dec 20 '14
Use a dictionary (ie "gg" is key and "good game" is value). Searches the string for any instances of the keys (if key in string) and replaces it with the dictionary value.
1
u/spfy Dec 20 '14 edited Dec 20 '14
This was fun! Here's some long Python 3. I think the way I handled punctuation will work most of the time.
Here's how it outputs:
1
u/frozensunshine 1 0 Dec 20 '14 edited Dec 20 '14
C. Learning, feedback requested.
1
u/seanshoots Dec 20 '14
C#
Usage:
AcronymExpander.ExpandString("imo that was wp. Anyway I've g2g");
Output:
in my opinion that was well played. Anyway I've g2g
Originally I used String.Split but this didn't work if the word ended with punctuation, so I switched to regex (please don't hate on the beautiful pattern above, all patterns are beautiful).
Really though, how could I improve the pattern? ^^Thanks
1
u/quickreply100 Dec 20 '14
Submission in ruby. As always any tips or advice are appreciated.
1
u/[deleted] Dec 20 '14
Strictly conformant C99. Compiles silently with -std=c99 -Weverything in clang. Handles both leading and trailing punctuation.
1
u/quickreply100 Dec 20 '14 edited Dec 20 '14
A second submission, this time in Lua. As always, any feedback is appreciated.
edit: I used lrexlib's bindings for PCRE since Lua's pattern matching doesn't include boundary matches (and PCRE does).
1
u/AshtonNight Dec 20 '14
Done in Python3. I created a acronyms.cfg to hold the acronyms and their translation. This way it's easy to update with new acronyms.
acrnym.cfg:
acronymExtender.py:
Ouput:
1
u/[deleted] Dec 20 '14
In Python27. Feedback would be greatly appreciated.
1
u/crashRevoke Dec 21 '14
python 2.7
1
u/blakemerryman Dec 21 '14
I know I'm a little late to the party but I wanted to post my solution. I've been experimenting with Swift's shell scripting capabilities and thought this would be a good first run.
https://gist.github.com/blakemerryman/a74a5a982186fe209649
1
u/[deleted] Dec 21 '14
this in in js;
I don't know how to automate it yet without having to force a prompt, was thinking of a text box that auto swaps.
1
u/atcrd Dec 21 '14
Im a bit late;just trying to get better and work on something during the break written in Java
1
u/chronoslynx Dec 21 '14
Using C this expands on word breaks (I've always had trouble with POSIX regex, so I arbitrarily defined a few characters). Reads from STDIN
1
u/[deleted] Dec 21 '14
My solution in Javascript :)
1
u/kur0saki Dec 21 '14
Go. The engine:
And the test which provides some acronyms:
1
u/Pretentious_Username Dec 21 '14
Python. I decided to add a few extra features to the challenge, my code:
Capitalises the expanded text based on the case of the original acronym, i.e. gg = good game, GG = Good Game and gG = good Game.
and the dict.txt file is here:
1
u/Yopu Dec 21 '14 edited Dec 21 '14
Groovy
1
u/juanchi35 Dec 21 '14
My C++ try, I think I could have used maps but I dont know how to use them :P
1
u/dhmmjoph Dec 22 '14
Python 2:
1
u/HumbleAlchemy Dec 22 '14
My solution to the problem without using regex, even though I liked the solution with it better from other users. https://gist.github.com/HumbleAlchemy/8d4dea36a71ab91d9120
1
u/5900 Dec 22 '14
perl
1
u/[deleted] Dec 22 '14 edited Dec 22 '14
C# :)
1
u/pshatmsft 0 1 Dec 22 '14 edited Dec 22 '14
PowerShell
Here is the code...
Output
1
u/covamalia Dec 22 '14
My first attempt at one of these. Done in VB.Net:
Doesnt fully take into account for punctuation though (commas and other punctuation are completely missed, "." is replaced by a space) but does get most other things. Sample output:
I could test for double space before writing the code back with a simple Replace function, but that would still leave me short if I went on to implement other punctuation so will leave it as that for now
1
u/[deleted] Dec 22 '14
Dirt simple C#:
1
u/Alexir563 Dec 22 '14
Java! I saw other people using fancy dictionaries and maps but I don't fully understand that... so I used an inefficient switch statement. Any help on keeping punctuation at the end?
}
1
u/cbk486 Dec 22 '14 edited Dec 27 '14
EDIT: I fixed a lot of things. I think my code is actually correct now... Java
First-timer here and, I'd love some feedback!
1
u/retupmoca Dec 23 '14
Perl 6:
1
u/hellectric Dec 23 '14
Groovy:
1
u/gma992 Dec 23 '14
PHP
This is my first /r/dailyprogrammer, just started with PHP a while ago without prior programming knowledge so I would love some advice.
1
u/procrastinatorDaz Dec 23 '14
JAVA
1
u/lhamil64 Dec 24 '14
In Python (note, I'm using these to learn Python, so it may not be the best solution):
1
u/datgohan Dec 24 '14
My attempt in python, passes the test case and can handle some edge cases like "this is a test gg.wp" where some punctuation is involved. Comments welcome as I'm still very new to python.
1
u/[deleted] Dec 24 '14
This is a very very rudimentary solution in Python. Works for the test cases except for the last one. Is there an easy way to handle periods and commas? Python nooby here.
1
u/staffinator Dec 24 '14 edited Dec 24 '14
Java:
1
u/deathmacro Dec 24 '14
1
u/[deleted] Dec 24 '14
Language: Python
Feedback would be appreciated, thank you.
1
u/[deleted] Dec 25 '14 edited Dec 25 '14
1
u/[deleted] Dec 25 '14
Hope I'm not too late to the party. Anyway, my first attempt at C#:
1
u/MarkAndRemember Dec 25 '14 edited Dec 26 '14
c#:
First submission here. Updated code to handle punctuation using a version of the current word string which has been cleaned up to remove all punctuation (non-alpha characters) with Regex.Replace.
If a match is found on the key in the alpha-only string, String.Replace handles the rest.
Edited to simplify. No need to go to the trouble of creating string with "{0}" for String.Format in this case.
1
u/AnhNyan Dec 25 '14
Ceylon
Noticed that we don't have any regex engines, so I had to use the Java one. Results in having to constantly convert between Java::String and Ceylon::String, because Pattern#matcher expects CharSequence, which apparently Ceylon::String does not derive (every other interop works fine).
:/
1
u/ICanCountTo0b1010 Dec 25 '14
Python 3:
key file:
1
u/[deleted] Dec 27 '14
Windows Powershell solution:
1
u/Aegior Dec 27 '14
Hey guys. I'm somewhat new at programming, but here's my Java solution. Let me know what I could have done differently.
1
u/Steve132 0 1 Dec 28 '14
C++11
1
u/lewisj489 0 1 Dec 31 '14
First post here :) C# [Spoiler](/s " using System; using System.Collections.Generic; using System.Linq;
namespace AcronymExpander { internal class Program { private static void Main() { var acronymnDefinitions = AcronymDictionary();
}
")
1
u/[deleted] Jan 01 '15
here's my solution in javascript, it is amalgamated from 2 previous solutions by hassanchug & spikebogdan:
1
u/Burnz69 Jan 01 '15
Java. Haven't added caps functionality yet.
1
u/[deleted] Jan 02 '15
More PHP cuz' everyone loves PHP!:
1
u/paktorfabi Jan 03 '15
JavaScript
1
u/[deleted] Jan 03 '15
Here is my python one-liner that works with all the cases, without regex.
Apart from this, there is a file called dict.txt that contains the list of acronyms exactly as it appears in the OP.
1
u/apentlander 0 1 Jan 04 '15
Rust. Feedback appreciated as it's my first time using rust
1
u/katysdayout Jan 04 '15
My first submission! In Python. Is there an easier way to do this than to manually type 4 spaces before every line?
1
u/officeSpaceCat Jan 05 '15
1
u/apentlander 0 1 Jan 06 '15
chicken scheme. Very very short if you don't include the alist definition haha. This doesn't match the whole word though (gloat -> good luckoat).
1
u/tomosjohnrees Jan 08 '15 edited Jan 08 '15
I'm new to programming and and am after any feedback I can get. My solution is written in Python 2.7. Sorry in advance if the formatting is wrong on this post, it's my first time posting to reddit. Cheers.
1
u/der_berliner Jan 10 '15
In Java, feedback would be much appreciated. Here's my gist.
1
u/montanabong Jan 14 '15 edited Jan 14 '15
Edit: Python 2.7 Not my prettiest code, but should work for 99.9% of inputs
1
u/voiceoverr Jan 15 '15
Python
A bit late to the party but wanted to see if anyone had feedback on my solution. Thanks!
1
u/[deleted] Jan 17 '15
Java:
1
u/zebulan_ Jan 26 '15
Python 3:
1
u/gemao_o Feb 01 '15
Took me a while but I came up with this::
1
u/Hanse00 Feb 15 '15
Python 2.4 using the built in string functions, did short work of the challenge (no regex included).
1
u/luminarus Feb 21 '15
So okay a few disclaimers:
Anyway, here goes with Python 2.7. Works on test cases.
1
u/russianwintersoldier Dec 20 '14
My first C++ program ever, please don't be gentle. Rip it constructively apart ;)
0
u/mobiusstripsearch Dec 21 '14
C++
First submission. Took way too much time; never worked with map before. I know I formatted the map inefficiently but once I got it to work I didn't care. Please and thank you for any feedback.
Might upload another version to make a dictionary off a file. That seems better than leaving it in the main program.
0
u/TheRealHamburgler Dec 30 '14 edited Dec 30 '14
PHP 5 (because why not?): Execute: php challenge193.php "lol that was fun. gl hf."
Edit: lawlers, forgot the word boundaries. >_< Marking as won't fix is appropriate here? :P
Edit: Fixed it anyway.
0
u/[deleted] Dec 20 '14
[deleted] — view deleted comment
0
u/[deleted] Dec 20 '14
[deleted] — view deleted comment
0
u/[deleted] Dec 26 '14
[deleted] — view deleted comment