Skip to content

Instantly share code, notes, and snippets.

@colomon
Created September 3, 2014 13:15
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save colomon/c2d2c1940b92d8d9df01 to your computer and use it in GitHub Desktop.
Save colomon/c2d2c1940b92d8d9df01 to your computer and use it in GitHub Desktop.
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}";
@colomon
Copy link
Author

colomon commented Sep 3, 2014

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