iLanguage / ilanguagelab

Automatically exported from code.google.com/p/ilanguagelab
0 stars 0 forks source link

Some code to add to the javascript recipies #11

Open GoogleCodeExporter opened 9 years ago

GoogleCodeExporter commented 9 years ago
Purpose of addition of this task:

add a javascript file in the ToolsForFieldLinguistics repository for this

One Liner to get word frequencies from a String :

String.prototype.map_reduce = function(){return 
this.toLowerCase().split(/\W+/g).reduce(function (t, w) { if (w) t[w] = (t[w] 
|| 0) + 1; return t; }, {}).toSource()}

Prettier version :

String.prototype.map_reduce = function () {
return this.toLowerCase().
split(/\W+/g).
reduce(function (t, w) {
if (w) {
t[w] = (t[w] || 0) + 1;
}
return t;
}, {}).
toSource()
}

Example :

>>> "The quick brown fox jumped over the lazy".map_reduce()

>>> "({the:2, quick:1, brown:1, fox:1, jumped:1, over:1, lazy:1, dog:1})"

Original issue reported on code.google.com by gina.c.c...@gmail.com on 23 Oct 2011 at 6:23

GoogleCodeExporter commented 9 years ago
This code extends teh prototype for strings this is bad practice and might 
break libraries if used in conjunction with other projects

Original comment by gina.c.c...@gmail.com on 5 Nov 2011 at 4:59

GoogleCodeExporter commented 9 years ago

Original comment by gina.c.c...@gmail.com on 25 Nov 2011 at 4:50