Created
September 3, 2014 13:15
-
-
Save colomon/c2d2c1940b92d8d9df01 to your computer and use it in GitHub Desktop.
This file contains 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 characters
grammar WordPairs { | |
rule word-pair { <key=word> <value=word> } | |
token word { \S+ } | |
rule TOP { <word-pair>* } | |
} | |
my %dict; | |
class WordPairsActions { | |
method word-pair($/) { %dict{~$<key>}.push(~$<value>) } | |
} | |
my $match = WordPairs.parse(slurp, :actions(WordPairsActions)); | |
say ?$match; | |
say "The pairs count of the key word \"her\" in wordpairs.txt is {%dict{"her"}.elems}"; |
One note -- I think you might be able to get better performance feeding in the original text file, instead of the paired version? But I admit I'm not at all certain of that, just suspicious. (ie an entirely new script which takes a complete text file and maps each word to the next word, so the end hash is the same.)
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Now, much Better time performance than my original code. Thank You very much! 👍