Skip to content

Instantly share code, notes, and snippets.

@blezek
Created October 26, 2012 15:46
Show Gist options
  • Select an option

  • Save blezek/3959527 to your computer and use it in GitHub Desktop.

Select an option

Save blezek/3959527 to your computer and use it in GitHub Desktop.

Revisions

  1. blezek revised this gist Oct 27, 2012. 1 changed file with 8 additions and 1 deletion.
    9 changes: 8 additions & 1 deletion letterpress.groovy
    Original file line number Diff line number Diff line change
    @@ -1,4 +1,6 @@
    #!/usr/bin/env groovy
    import groovy.util.Node;
    import java.util.zip.GZIPInputStream;

    def root = new Node ( null, "root", [ path : "", leaf : false, value : "" ] );
    Words = []
    @@ -48,7 +50,12 @@ def Letters = this.args[1];

    def StartTime = System.currentTimeMillis()
    def count = 0;
    new File(DictionaryFile).eachLine {
    def input = new FileInputStream ( DictionaryFile );
    if ( DictionaryFile.endsWith ( '.gz' ) ) {
    input = new GZIPInputStream ( input )
    }

    input.eachLine {
    stuffWord ( it.toLowerCase(), root )
    count++
    }
  2. blezek revised this gist Oct 26, 2012. 1 changed file with 19 additions and 2 deletions.
    21 changes: 19 additions & 2 deletions letterpress.groovy
    Original file line number Diff line number Diff line change
    @@ -1,6 +1,7 @@
    import groovy.util.Node;

    def root = new Node ( null, "root", [ path : "", leaf : false, value : "" ] );
    Words = []

    // Pull off the first letter, see if the node has that.
    // If at end of word, make as leaf node
    @@ -27,7 +28,9 @@ def findWords ( letters, node ) {
    if ( index >= 0 ) {
    // we have a match, remove the letter
    remainingLetters = letters.substring(0,index) + letters.substring(index+1,letters.length() )
    println ( child.attributes()["path"] )
    if ( child.attributes()["leaf"] ) {
    Words.add ( child.attributes()["path"] )
    }
    findWords ( remainingLetters, child )
    }
    }
    @@ -36,7 +39,7 @@ def findWords ( letters, node ) {

    if ( this.args.size() != 2 ) {
    println ( "usage: letterpress <dictionary> <letters>" )
    println ( "sample: \ngroovy letterpress.groovy sowpods.txt xmwzldqgtnpuwwanuemwcpisv | awk '{ print length($0),$0 | "sort -n -r"}'> zac2.txt" )
    println ( 'sample: \ngroovy letterpress.groovy sowpods.txt xmwzldqgtnpuwwanuemwcpisv | awk \'{ print length($0),$0 | "sort -n -r"}\'> zac2.txt' )
    System.exit ( 1 )
    }

    @@ -57,3 +60,17 @@ StartTime = System.currentTimeMillis()
    findWords ( Letters, root )
    EndTime = System.currentTimeMillis()
    println ( "# Found words in ${ (EndTime-StartTime)/1000.0 } seconds" )

    // Sort longest to shortest, then alphabetically
    Words = Words.sort { a, b ->
    eq = a.length() <=> b.length()
    if ( eq == 0 ) {
    eq = a <=> b
    } else {
    eq *= -1
    }
    eq
    }
    Words.each {
    println ( it )
    }
  3. blezek created this gist Oct 26, 2012.
    59 changes: 59 additions & 0 deletions letterpress.groovy
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,59 @@
    import groovy.util.Node;

    def root = new Node ( null, "root", [ path : "", leaf : false, value : "" ] );

    // Pull off the first letter, see if the node has that.
    // If at end of word, make as leaf node
    def stuffWord ( word, node ) {
    if ( word.length() == 0 ) {
    node.attributes()["leaf"] = true
    return
    }
    letter = new String ( word[0] )
    child = node.children().find { it.attributes()["value"] == letter }

    if ( ! child ) {
    child = new Node ( node, letter, [ path : node.attributes()["path"] + letter, leaf : false, value : letter ] );
    }
    stuffWord ( word.substring(1), child )
    }


    // See if we match anything
    def findWords ( letters, node ) {
    // Do a depth first search
    node.children().each { child ->
    index = letters.findIndexOf { it == child.name() }
    if ( index >= 0 ) {
    // we have a match, remove the letter
    remainingLetters = letters.substring(0,index) + letters.substring(index+1,letters.length() )
    println ( child.attributes()["path"] )
    findWords ( remainingLetters, child )
    }
    }
    }


    if ( this.args.size() != 2 ) {
    println ( "usage: letterpress <dictionary> <letters>" )
    println ( "sample: \ngroovy letterpress.groovy sowpods.txt xmwzldqgtnpuwwanuemwcpisv | awk '{ print length($0),$0 | "sort -n -r"}'> zac2.txt" )
    System.exit ( 1 )
    }

    def DictionaryFile = this.args[0];
    def Letters = this.args[1];

    def StartTime = System.currentTimeMillis()
    def count = 0;
    new File(DictionaryFile).eachLine {
    stuffWord ( it.toLowerCase(), root )
    count++
    }
    def EndTime = System.currentTimeMillis()
    println ( "# Searching ${DictionaryFile} for words made up of ${Letters}" )
    println ( "# Finished building tree in ${ (EndTime-StartTime)/1000.0 } seconds for ${count} entries" )

    StartTime = System.currentTimeMillis()
    findWords ( Letters, root )
    EndTime = System.currentTimeMillis()
    println ( "# Found words in ${ (EndTime-StartTime)/1000.0 } seconds" )