When jQuery met Wikipedia for a game of Scrabble

I needed to obtain a JSON object containing the tile values on scrabble tiles for a project I took on. This was across 8 dictionaries (6 languages). The problem was, I couldn’t  find a good source for the required data. I have since found a far easier way to obtain the data but it was still a fun challenge.

The best I could do at the time was http://en.wikipedia.org/wiki/Scrabble_letter_distributions.

The data was laid out as displayed below and it didn’t look very friendly.

English

English-language editions of Scrabble contain 100 letter tiles, in the following distribution:

  • 2 blank tiles (scoring 0 points)
  • 1 point: E ×12, A ×9, I ×9, O ×8, N ×6, R ×6, T ×6, L ×4, S ×4, U ×4
  • 2 points: D ×4, G ×3
  • 3 points: B ×2, C ×2, M ×2, P ×2
  • 4 points: F ×2, H ×2, V ×2, W ×2, Y ×2
  • 5 points: K ×1
  • 8 points: J ×1, X ×1
  • 10 points: Q ×1, Z ×1
With a little inspection, I was able to identify the formatting applied to the required elements.Enter Google Chrome plus the jsshell extension. Jsshell allows you to run jQuery on any page. Now isn’t that somethin’ :-).So after a few (debatable) minutes I came up with the following

 

langs =["English","German","Spanish","Italian","French","Portuguese"];
var allvals ={};

$.each(langs,function(){
 lang = this.toString();
 $lets = $("span#" + this.toString()).parent().nextAll("ul").eq(0);
 allvals[lang] = {};
 $lets.find("li b").each(function(){
  allvals[lang][$(this).text()] = parseInt($(this).siblings("li i").eq(0).text());
 });
});
console.log(JSON.stringify(allvals));

In other words

  1. For each of the six languages specified, find the span with an id  equal to the language.
  2. Find the first list following the parent of the identified span
  3. For each bold letter in the list, assign the integer value of the italicized text which is a sibling of that letter to that letters index in that languages array.
    • Example, for the English dictionary, Q and Z (bold) are 10 points (italicized). The integer value of  “10 points” is of course 10.

The result,

{"English":{"E":1,"A":1,"I":1,"O":1,"N":1,"R":1,"T":1,"L":1,"S":1,"U":1,"D":2,"G":2,"B":3,"C":3,"M":3,"P":3,"F":4,"H":4,"V":4,"W":4,"Y":4,"K":5,"J":8,"X":8,"Q":10,"Z":10},"German":{"E":1,"N":1,"S":1,"I":1,"R":1,"T":1,"U":1,"A":1,"D":1,"H":2,"G":2,"L":2,"O":2,"M":3,"B":3,"W":3,"Z":3,"C":4,"F":4,"K":4,"P":4,"Ä":6,"J":6,"Ü":6,"V":6,"Ö":8,"X":8,"Q":10,"Y":10},"Spanish":{"A":1,"E":1,"O":1,"I":1,"S":1,"N":1,"L":1,"R":1,"U":1,"T":1,"D":2,"G":2,"C":3,"B":3,"M":3,"P":3,"H":4,"F":4,"V":4,"Y":4,"CH":5,"Q":5,"J":8,"LL":8,"Ñ":8,"RR":8,"X":8,"Z":10},"Italian":{"O":1,"A":1,"I":1,"E":1,"C":2,"R":2,"S":2,"T":2,"L":3,"M":3,"N":3,"U":3,"B":5,"D":5,"F":5,"P":5,"V":5,"G":8,"H":8,"Z":8,"Q":10},"French":{"E":1,"A":1,"I":1,"N":1,"O":1,"R":1,"S":1,"T":1,"U":1,"L":1,"D":2,"M":2,"G":2,"B":3,"C":3,"P":3,"F":4,"H":4,"V":4,"J":8,"Q":8,"K":10,"W":10,"X":10,"Y":10,"Z":10},"Portuguese":{"A":1,"E":1,"I":1,"O":1,"S":1,"U":1,"M":1,"R":1,"T":1,"D":2,"L":2,"C":2,"P":2,"N":3,"B":3,"Ç":3,"F":4,"G":4,"H":4,"V":4,"J":5,"Q":6,"X":8,"Z":8}}
Lines of code : 10
  • Cool, its good to see people working on stuff that they are passionate about

  • The main driver is the need to sharpen my tools. It’s definitely a plus when you get to enjoy what you’re working on though.

  • Elliott Manley

    So elegant! Love this.
    I’m tempted to have a crack in Perl…

    • Dale Vivian Ross

      You should! Where is the clock you did?

Proudly powered by WordPress   Premium Style Theme by www.gopiplus.com