Skip to content

Instantly share code, notes, and snippets.

@RickCarlino
Created November 29, 2012 14:40
Show Gist options
  • Star 2 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save RickCarlino/4169502 to your computer and use it in GitHub Desktop.
Save RickCarlino/4169502 to your computer and use it in GitHub Desktop.
Use the Lingq API to practice your foreign listening language skills
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
@RickCarlino
Copy link
Author

OPPS! Half implemented something on line 63 and then forgot about it. Sorry.

@Jigree1
Copy link

Jigree1 commented Dec 6, 2012

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.

@Jigree1
Copy link

Jigree1 commented Dec 7, 2012

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.

@RickCarlino
Copy link
Author

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