Created
July 16, 2012 08:57
-
-
Save Yuffster/3121661 to your computer and use it in GitHub Desktop.
Spanish homework study helper
This file contains hidden or 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
| ./node_modules |
This file contains hidden or 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
| dict = require('./dictionary'), | |
| prompt = require('prompt'), | |
| sys = require('util'); | |
| require ('string-color'); | |
| Array.prototype.shuffle = function() { | |
| var len = this.length; | |
| var i = len; | |
| while (i--) { | |
| var p = parseInt(Math.random()*len); | |
| var t = this[i]; | |
| this[i] = this[p]; | |
| this[p] = t; | |
| } | |
| }; | |
| var vocab = ['ser', 'autobús', 'capital', 'chico', 'chica', 'computadora', 'comunidad', 'conductor', 'conversación', | |
| 'cosa', 'cuaderno', 'dia', 'diario', 'diccionario', 'escuela', 'estudiante', 'fotografia', 'hombre', | |
| 'joven', 'lápiz', 'lección', 'maleta', 'mano', 'mapa', 'mujer', 'nacionalidad', 'número', 'pais', | |
| 'palabra', 'pasajero', 'problema', 'profesor', 'programa', 'turista', 'video', 'biblioteca', 'cafeteria', | |
| 'casa', 'estadio', 'laboratorio', 'libreria', 'residencia estudiantil', 'universidad', 'compañero de clase', | |
| 'compañero de cuarto', 'clase', 'curso', 'especialización', 'examen', 'horario', 'prueba', 'semestre', | |
| 'tarea', 'trimestre', 'administración de empresas', 'arte', 'biologia', 'ciencias', 'computación', 'contabilidad', | |
| 'economía', 'física', 'geografia', 'música', 'bailar', 'buscar', 'caminar', 'cantar', 'cenar', 'comprar', 'contestar', | |
| 'conversar', 'desayunar', 'desear', 'dibujar', 'enseñar', 'escuchar', 'esperar', 'estudiar', 'explicar', 'halbar', | |
| 'llegar', 'llevar', 'mirar', 'necesitar', 'practicar', 'preparar', 'regresar', 'terminar', 'tomar', 'trabajar', | |
| 'viajar', 'donde', 'adónde', 'cómo', 'cuál', 'cuáles', 'cuándo', 'cuántos', 'por qué', 'qué', 'quién', 'quiénes', | |
| 'al lado', 'a la derecha', 'treinta', 'cuarenta', 'cincuenta', 'sesenta', 'setenta', 'ochenta', 'noventa', 'cien', | |
| 'ciento', 'doscientos', 'trescientos', 'cuatrocientos', 'quinientos', 'seiscientos', 'setencientos', 'ochocientos', | |
| 'novecientos', 'mil', 'millón', 'derecha', 'izquierda', 'allá', 'alli', 'cerca', 'con', 'debajo', | |
| 'delante', 'detrás', 'en', 'encima', 'entre', 'lejos', 'sin', 'sobre'] | |
| , word = vocab[Math.floor((Math.random()*vocab.length))]; | |
| vocab = [ 'bailar', 'buscar', 'caminar', 'cantar', 'cenar', 'comprar', 'contestar', | |
| 'conversar', 'desayunar', 'desear', 'dibujar', 'enseñar', 'escuchar', 'esperar', 'estudiar', 'explicar', 'halbar', | |
| 'llegar', 'llevar', 'mirar', 'necesitar', 'practicar', 'preparar', 'regresar', 'terminar', 'tomar', 'trabajar' ] | |
| function drill() { | |
| prompt.start(); | |
| vocab.shuffle(); | |
| dict.getWord(vocab[0], function(e,d) { | |
| prompt.get(d.translation, function(e, data) { | |
| if (data[d.translation] == vocab[0]) { | |
| console.log("Muy bien!".color('green')); | |
| //If it's a verb, randomize and cycle through each conjugation. | |
| if (d.conjugations) { | |
| var subjects = []; | |
| for (var subject in d.conjugations.present) { | |
| subjects.push(subject); | |
| } | |
| subjects.shuffle(); | |
| function nextSubject() { | |
| if (subjects.length==0) return drill(); | |
| var next = subjects.shift(); | |
| prompt.get(next, function(e, sub) { | |
| if (sub[next]==d.conjugations.present[next]) { | |
| console.log("Muy bien!".color('green')); | |
| nextSubject(); | |
| } else { | |
| console.log("Los sientos. :(".color('red')); | |
| prompt.get(d.conjugations.present[next], nextSubject); | |
| } | |
| }); | |
| }; | |
| nextSubject(); | |
| } | |
| } else { | |
| console.log("Los siento. :(".color('red')); | |
| console.log(vocab[0]); | |
| prompt.get(vocab[0], function(e, r) { | |
| drill(); | |
| }); | |
| } | |
| }); | |
| }); | |
| }; | |
| drill(); |
This file contains hidden or 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
| jsdom = require('jsdom'); | |
| exports.getWord = function(word, fun) { | |
| jsdom.env("http://www.spanishdict.com/translate/"+word, [ 'http://code.jquery.com/jquery-1.5.min.js' ], function(e, window) { | |
| $ = window.$; | |
| data = { | |
| translation: $($('.quick_def')[0]).text().trim().replace(/^to /, ''), | |
| }; | |
| if ($('.conj-block table tr').length>0) { | |
| data.conjugations = {}; | |
| $.each($('.conj-block table tr'), function(k,v) { | |
| if (k==0) return; | |
| var tds = $(v).find('td'), | |
| subject; | |
| if (!tds) return; | |
| subject = $(tds[0]).text().trim(); | |
| $.each(['present', 'preterit', 'imperfecto', 'conditional', 'future'], function(i,tense) { | |
| if (!data.conjugations[tense]) data.conjugations[tense] = {}; | |
| if (subject.match(/\//)) { | |
| $.each(subject.split('/'), function(k,v) { | |
| if (v=="Uds.") v = "ustedes"; | |
| if (v=="Ud.") v = "usted"; | |
| data.conjugations[tense][v] = $(tds[i+1]).text().trim(); | |
| }); | |
| } else data.conjugations[tense][subject] = $(tds[i+1]).text().trim(); | |
| }); | |
| }); | |
| }; | |
| if ($('.gender')) { | |
| data.gender = $('.gender').text().replace(/\(|\)/g, ''); | |
| } | |
| fun(e, data); | |
| }); | |
| }; |
This file contains hidden or 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
| { | |
| "name":"spanish.js", | |
| "description":"No habla español. :(", | |
| "version":"0.4.0", | |
| "repository": { | |
| "type": "git", | |
| }, | |
| "dependencies": { | |
| "redis":">=0", | |
| "jsdom":">0", | |
| "prompt":">0", | |
| "string-color":">0" | |
| }, | |
| "author": { | |
| "name": "Michelle Steigerwalt", | |
| "homepage": "http://msteigerwalt.com" | |
| }, | |
| "main": "app.js" | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment