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}";
@yeahnoob
Copy link

yeahnoob commented Sep 3, 2014

Now, much Better time performance than my original code. Thank You very much! 👍

$ time perl6 cp-mod-colomon.pl wordpairs.txt
True
The pairs count of the key word "her" in wordpairs.txt is 1036

real    0m28.846s
user    0m53.366s
sys 0m2.232s

$ perl6 -e 'say $*VM'
jvm (1.7)

$ nqp-j --version
This is nqp version 2014.08 built on JVM

$ grep "model name" /proc/cpuinfo -m 1
model name  : Intel(R) Core(TM) i7-2600K CPU @ 3.40GHz

@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