Created
October 26, 2012 15:46
-
-
Save blezek/3959527 to your computer and use it in GitHub Desktop.
Revisions
-
blezek revised this gist
Oct 27, 2012 . 1 changed file with 8 additions and 1 deletion.There are no files selected for viewing
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 charactersOriginal 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; def input = new FileInputStream ( DictionaryFile ); if ( DictionaryFile.endsWith ( '.gz' ) ) { input = new GZIPInputStream ( input ) } input.eachLine { stuffWord ( it.toLowerCase(), root ) count++ } -
blezek revised this gist
Oct 26, 2012 . 1 changed file with 19 additions and 2 deletions.There are no files selected for viewing
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 charactersOriginal 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() ) 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' ) 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 ) } -
blezek created this gist
Oct 26, 2012 .There are no files selected for viewing
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 charactersOriginal 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" )