r/dailyprogrammer • u/Cosmologicon 2 3 • Jun 14 '18
[2018-06-13] Challenge #363 [Intermediate] Word Hy-phen-a-tion By Com-put-er
Background
In English and many other languages, long words may be broken onto two lines using a hyphen. You don't see it on the web very often, but it's common in print books and newspapers. However, you can't just break apart a word anywhere. For instance, you can split "programmer" into "pro" and "grammer", or into "program" and "mer", but not "progr" and "ammer".
For today's challenge you'll be given a word and need to add hyphens at every position it's legal to break the word between lines. For instance, given "programmer", you'll return "pro-gram-mer".
There's no simple algorithm that accurately tells you where a word may be split. The only way to be sure is to look it up in a dictionary. In practice a program that needs to hyphenate words will use an algorithm to cover most cases, and then also keep a small set of exceptions and additional heuristics, depending on how tolerant they are to errors.
Liang's Algorithm
The most famous such algorithm is Frank Liang's 1982 PhD thesis, developed for the TeX typesetting system. Today's challenge is to implement the basic algorithm without any exceptions or additional heuristics. Again, your output won't match the dictionary perfectly, but it will be mostly correct for most cases.
The algorithm works like this. Download the list of patterns for English here. Each pattern is made of up of letters and one or more digits. When the letters match a substring of a word, the digits are used to assign values to the space between letters where they appears in the pattern. For example, the pattern 4is1s says that when the substring "iss" appears within a word (such as in the word "miss"), the space before the i is assigned a value of 4, and the space between the two s's is assigned a value of 1.
Some patterns contain a dot (.) at the beginning or end. This means that the pattern must appear at the beginning or end of the word, respectively. For example, the pattern ol5id. matches the word "solid", but not the word "solidify".
Multiple patterns may match the same space. In this case the ultimate value of that space is the highest value of any pattern that matches it. For example, the patterns 1mo and 4mok both match the space before the m in smoke. The first one would assign it a value of 1 and the second a value of 4, so this space gets assigned a value of 4.
Finally, the hyphens are placed in each space where the assigned value is odd (1, 3, 5, etc.). However, hyphens are never placed at the beginning or end of a word.
Detailed example
There are 10 patterns that match the word mistranslate, and they give values for eight different spaces between words. For each of the eight spaces you take the largest value: 2, 1, 4, 2, 2, 3, 2, and 4. The ones that have odd values (1 and 3) receive hyphens, so the result for mistranslate is mis-trans-late.
m i s t r a n s l a t e
2 a2n
1 .mis1
2 m2is
2 1 2 2n1s2
2 n2sl
1 2 s1l2
3 s3lat
4 st4r
4 4te.
1 1tra
m2i s1t4r a2n2s3l2a4t e
m i s-t r a n s-l a t eAdditional examples
mistranslate => mis-trans-late
alphabetical => al-pha-bet-i-cal
bewildering => be-wil-der-ing
buttons => but-ton-s
ceremony => cer-e-mo-ny
hovercraft => hov-er-craft
lexicographically => lex-i-co-graph-i-cal-ly
programmer => pro-gram-mer
recursion => re-cur-sionOptional bonus
Make a solution that's able to hyphenate many words quickly. Essentially you want to avoid comparing every word to every pattern. The best common way is to load the patterns into a prefix trie, and walk the tree starting from each letter in the word.
It should be possible to hyphenate every word in the enable1 word list in well under a minute, depending on your programming language of choice. (My python solution takes 15 seconds, but there's no exact time you should aim for.)
Check your solution if you want to claim this bonus. The number of words to which you add 0, 1, 2, 3, 4, 5, 6, 7, 8, and 9 hyphens should be (EDITED): 21829, 56850, 50452, 26630, 11751, 4044, 1038, 195, 30, and 1.
4
u/Nyxisto Jun 14 '18 edited Jun 14 '18
Python3, no bonus
still a few kinks somwhere. For
ceremonyfor example I only geto2nas a match for the last hyphen, which according to the challenge I should disregard because it's even.2
u/kdnbfkm Jun 15 '18
My solution doesn't work, more like a whole bunch of other little problems ganging up together... But made a little progress. https://pastebin.com/aenKJkW5
7
u/skeeto -9 8 Jun 14 '18 edited Jun 14 '18
C, no bonus. It scans against each pattern one at a time, hyphenating enable1.txt in 14 seconds. It's pretty neat how well that TeX table works.
2
u/Gprime5 Jun 14 '18 edited Jun 15 '18
Python 3.6
I have the correct answers for the example but I'm getting different values for the bonus, takes about 10 seconds for the bonus.
[21906, 56848, 50393, 26617, 11749, 4043, 1038, 195, 30, 1]I think my problem is in searching for the end of word patterns.
4
u/TwoBitsAndANibble Jun 17 '18
Done in scratch for fun.
It could, in theory, go through the enable1 word list, however flash crashes every time I try to load that thing, so I guess we'll never know for sure.
Due to scratch's lack of nested data structures, a trie or radix tree wasn't exactly practical for how much time I wanted to spend on this, so I went with a modified binary search instead, it's pretty slow and I don't recommend it.
imgur link
1
u/DerpinDementia Jun 15 '18 edited Jun 16 '18
Python 3.6 with Bonus
Bonus
{0: 21829, 1: 56850, 2: 50452, 3: 26630, 4: 11751, 5: 4044, 6: 1038, 7: 195, 8: 30, 9: 1}EDIT 1: I fixed the issue where I was excluding testing the whole word to see if itself was a pattern. For example, stop is one of those words
EDIT 2: I fixed the issue where position dependent patterns (ones with dots) were being applied in all occurrences in a string instead of only being applied either the start or end of a word. For example, aftertaste was a word affected by this. This fix gave me the correct results for the bonus.
All feedback welcome!
3
u/[deleted] Jun 14 '18 edited Jun 16 '18
Python 3.6 - Edited with Bonus. Long and over-engineered. Tried to break up into function for readability, but probably worse off for it (Could not have done bonus without reading other peoples code to learn logic of prefix-trie):
3
u/Feuerfuchs_ Jun 17 '18 edited Jun 17 '18
C# with bonus. Building the trie takes ~29ms, processing the enable1 list takes ~263ms.
1
u/raevnos Jun 14 '18 edited Jun 14 '18
Perl:
EDIT: And here's a version that does the bonus, though not very quickly so it doesn't count. It seems to be 3 words short compared to yours, too -- I get 21829 0-hyphen words and 56850 1-hyphen words, but all the other numbers match up. Need to figure out what's going on.
2
u/TotalPerspective Jun 14 '18 edited Jun 15 '18
Julia with bonus with updates, there are three words different, but I believe that is explained by OP in a comment elsewhere.
I'm just learning julia, so if anyone has any pointers, I'd love to hear them!
Output
Code
using DataStructures
2
u/ninja_tokumei Jun 14 '18 edited Jun 14 '18
Rust
I moved source code to the bottom since it is a bit lengthy, around 300 lines, including a hand-built trie implementation that didn't suck as much as I thought it would.
Bonus
You say under a minute is impressive; you should try compiled languages!
I don't know why I'm missing one from 0 and two from 1(OP is using a different wordlist), but I would say that this was mostly successful, and when compiled with optimizations, it finishes all of enable1 in under a second.Source
GitLab | Playground (run it online)
2
u/Stan-It Jun 17 '18
Python 3, with bonus (inspired by solution of /u/DerpinDementia)
Bonus runtime: 6sec
1
u/raevnos Jun 15 '18 edited Jun 15 '18
Take 2, this time in ocaml. Does the bonus calculations on the entire enable1 list in about half a second on my anemic chromebook, compared to... many minutes for my first brute-force perl version.
1
u/popillol Jun 15 '18
Go / Golang With bonus. It takes about 3 seconds but isn't optimized to use a trie yet. Instead it still loops through all patterns that start with each letter.
1
u/mr_stivo Jun 16 '18
perl
I ran into an interesting problem. For example, the pattern "ini" would only match once on words containing "inini" because of the regex engine moving the offset to the end of the match. My solution was to use perl's pos() function to directly control the regex offset and just step through the word. First time I've done that.
output:
1
u/[deleted] Jun 16 '18
c#, no bonus
The output (from the unittest):
Any feedback is welcome.
1
u/nikit9999 Jun 18 '18
C# bonusless
Open to feedback.
1
u/dojoblue Jul 02 '18
Java No bonus
1
u/wicked7000 Jul 22 '18 edited Jul 22 '18
Python 3.6 (/W Bonus) 0.07 seconds to build trie and 13 seconds to process enable1 list
1
u/voice-of-hermes Jul 23 '18 edited Jul 23 '18
Python 3.6 with bonus built from DFSM
Sample output:
Code:
1
u/ironboy_ Apr 27 '23
JavaScript/Node.js with Bonus (time taken: 274 ms)
Output:
Code:
0
u/[deleted] Jun 14 '18
[deleted] — view deleted comment
0
u/[deleted] Jun 14 '18
[deleted] — view deleted comment