Created
November 29, 2012 14:40
-
-
Save RickCarlino/4169502 to your computer and use it in GitHub Desktop.
Use the Lingq API to practice your foreign listening language skills
This file contains 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
require 'rest_client' | |
require 'json' | |
#Initialization: Load the words into a usable format from the API. | |
#These are the only settings you would need to change if you were a different user: | |
#Goto: https://www.lingq.com/accounts/apikey/ to find your API Key | |
api_key = 'YOUR API KEY GOES HERE. CHANGE THIS!' | |
lang_code = 'ko' #for Korean, en for English, etc. | |
#The voice to use for your target language | |
#check the mac text to speech documentation to see what langauges are available for you | |
target_voice = 'yuna' #Again, for Korean | |
speech_rate = 130 #in words per minute | |
#The voice to use for your native language. I recommend 'Alex' for English | |
native_voice = 'alex' | |
#shuffle words? set false to turn this off. | |
shuffle_words = true | |
#all of these are for internal usage. Pretty much nothing to see from here on if you don't plan on modifying the script. | |
lessons_url = "http://www.lingq.com/api_v2/#{lang_code}/lessons/?apikey=#{api_key}" | |
srs_url = "http://www.lingq.com/api_v2/#{lang_code}/repetition-lingqs/?apikey=#{api_key}" | |
update_url = "http://www.lingq.com/api_v2/#{lang_code}/lingqs/?apikey=#{api_key}" | |
system 'clear' | |
puts 'Loading word list now' | |
word_list = RestClient.get(srs_url) | |
word_list = JSON.parse(word_list) | |
word_list = word_list.values.last | |
system 'clear' | |
puts 'Success!' | |
word_list.shuffle! if shuffle_words == true | |
word_list.each do |word| | |
puts "PROGRESS: #{word_list.index(word)}/#{word_list.length}" | |
#Prevent user from getting quizzes for blank entries | |
next if (word['hint'].length < 2) | |
distractors = [] | |
word_list.each{|x| distractors << x['hint']} | |
choices = distractors.sample(7) | |
choices << word['hint'] | |
choices.shuffle! | |
system "say -v #{native_voice} 'In the following sentence'" | |
system "say -v #{target_voice} -r #{speech_rate} #{word['fragment'].strip}." | |
system "say -v #{native_voice} 'whats the meaning of: '" | |
system "say -v #{target_voice} -r #{speech_rate} #{word['term']}" | |
#TODO remove any escape characters from word fragments. They upset the mac 'say' command and Ruby | |
choices.each{|choice| | |
puts "#{choices.index(choice)} ) #{choice}" | |
} | |
puts 'Enter your choice (9 to fail item, q to quit, s to skip): ' | |
answer = gets.chomp | |
if answer == 'q' | |
abort('Goodbye!') | |
end | |
if answer == 's' | |
next | |
end | |
if answer == '9' | |
system 'clear' | |
puts "Word: #{word['term']}" | |
puts "Hint: #{word['hint']}" | |
puts "Phrase: #{word['fragment']}" | |
system "say -v #{target_voice} -r #{speech_rate} '#{word['term']}'" | |
system "say -v #{native_voice} -r 200 ' means #{word['hint']} as in '" | |
system "say -v #{target_voice} -r #{speech_rate} '#{word['fragment']}'" | |
next | |
end | |
if (choices[answer.to_i] == word['hint']) | |
system 'clear' | |
puts "Word: #{word['term']}" | |
puts "Hint: #{word['hint']}" | |
puts "Phrase: #{word['fragment']}" | |
system "say -v #{native_voice} -r 200 'You are correct, '" | |
system "say -v #{target_voice} -r #{speech_rate} '#{word['term']}'" | |
system "say -v #{native_voice} 'means #{choices[answer.to_i]}\'" | |
system "say -v #{target_voice} -r #{speech_rate} '#{word['fragment']}'" | |
RestClient.put(update_url, :id => word['id'], :status => (word['status']+1)) | |
else | |
system 'clear' | |
puts "Word: #{word['term']}" | |
puts "Hint: #{word['hint']}" | |
puts "Phrase: #{word['fragment']}" | |
system "say -v #{native_voice} \"'incorrect. You answered #{choices[answer.to_i]}, but the correct response is #{word['hint']}'\"" | |
system "say -v #{target_voice} -r #{speech_rate} '#{word['fragment']}'" | |
RestClient.put(update_url, :id => word['id'], :status => 0) | |
end | |
system 'clear' | |
puts 'listen...' | |
system 'clear' | |
end |
I want to use this program. Is there a certain program I need to open the file in? like for instance python or another program like that.
Never mind I found out about using ruby. I can't wait to try it but unfortunately I'm having problems starting it. Hope I'll be able to figure it out.
Jigree- just saw your message now. Sorry for the slow response time. What issues are you having?
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
OPPS! Half implemented something on line 63 and then forgot about it. Sorry.