Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    text
    copied!<p>I dare suggest a brute force solution that is not very elegant but still useful in case</p> <ul> <li>you have a large number of items (building a regexp can be a pain)</li> <li>the string to analyse is not limited to two components</li> <li>you want to get all splittings of a string</li> <li>you want only complete analyses of a string, that span from ^ to $.</li> </ul> <p>Because of my poor English, I could not figure out a long personal name that can be split in more than one way, so let's analyse a phrase:</p> <pre><code>word = "godisnowhere" </code></pre> <p>The dictionary:</p> <pre><code>@dict = [ "god", "is", "now", "here", "nowhere", "no", "where" ] @lengths = @dict.collect {|w| w.length }.uniq.sort </code></pre> <p>The array <code>@lengths</code> adds a slight optimization to the algorithm, we will use it to prune subwords of lengths that don't exist in the dictionary without actually performing dictionary lookup. The array is sorted, this is another optimization.</p> <p>The main part of the solution is a recursive function that finds the initial subword in a given word and restarts for the tail subword.</p> <pre><code>def find_head_substring(word) # boundary condition: # remaining subword is shorter than the shortest word in @dict return [] if word.length &lt; @lengths[0] splittings = [] @lengths.each do |len| break if len &gt; word.length head = word[0,len] if @dict.include?(head) tail = word[len..-1] if tail.length == 0 splittings &lt;&lt; head else tails = find_head_substring(tail) unless tails.empty? tails.collect!{|tail| "#{head} #{tail}" } splittings.concat tails end end end end return splittings end </code></pre> <p>Now see how it works</p> <pre><code>find_head_substring(word) =&gt;["god is no where", "god is now here", "god is nowhere"] </code></pre> <p>I have not tested it extensively, so I apologize in advance :)</p>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload