Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP
Newer
Older
100644 105 lines (85 sloc) 3.352 kB
3406d66 @technicalpickles Update "w" help comments
technicalpickles authored
1 # Description:
2 # Dictionary definitions with the Wordnik API.
5982e63 @Aupajo Add Dictionary
Aupajo authored
3 #
3406d66 @technicalpickles Update "w" help comments
technicalpickles authored
4 # Dependencies:
5 # None
6 #
7 # Configuration:
8 # WORDNIK_API_KEY
9 #
10 # Commands:
11 # hubot define me <word> - Grabs a dictionary definition of a word.
12 # hubot pronounce me <word> - Links to a pronunciation of a word.
13 # hubot spell me <word> - Suggests correct spellings of a possible word.
bf41933 @hoovercj Added support for Bigrams/Phrases
hoovercj authored
14 # hubot bigram me <word> - Grabs the most frequently used bigram phrases containing this word
3406d66 @technicalpickles Update "w" help comments
technicalpickles authored
15 #
16 # Notes:
17 # You'll need an API key from http://developer.wordnik.com/
18 # FIXME This should be merged with word-of-the-day.coffee
2e1951c @tombell Trim blank line comments, and fix up issues
tombell authored
19 #
20 # Author:
21 # Aupajo
22 # markpasc
c1293df @Aupajo Add pronunciation to Wordnik
Aupajo authored
23
5982e63 @Aupajo Add Dictionary
Aupajo authored
24 module.exports = (robot) ->
01fcb94 @Aupajo Comment tweak
Aupajo authored
25 # Word definition
5982e63 @Aupajo Add Dictionary
Aupajo authored
26 robot.respond /define( me)? (.*)/i, (msg) ->
27 word = msg.match[2]
28
ff0228f @markpasc Merge branch 'master' into wordnik-spell
markpasc authored
29 fetch_wordnik_resource(msg, word, 'definitions', {}) (err, res, body) ->
c1293df @Aupajo Add pronunciation to Wordnik
Aupajo authored
30 definitions = JSON.parse(body)
31
32 if definitions.length == 0
33 msg.send "No definitions for \"#{word}\" found."
34 else
35 reply = "Definitions for \"#{word}\":\n"
36 lastSpeechType = null
37
38 definitions = definitions.forEach (def) ->
39 # Show the part of speech (noun, verb, etc.) when it changes
40 if def.partOfSpeech != lastSpeechType
41 reply += " (#{def.partOfSpeech})\n" if def.partOfSpeech != undefined
42
43 # Track the part of speech
44 lastSpeechType = def.partOfSpeech
45
46 # Add the definition
47 reply += " - #{def.text}\n"
48
49 msg.send reply
01fcb94 @Aupajo Comment tweak
Aupajo authored
50
51 # Pronunciation
c1293df @Aupajo Add pronunciation to Wordnik
Aupajo authored
52 robot.respond /(pronounce|enunciate)( me)? (.*)/i, (msg) ->
53 word = msg.match[3]
54
ff0228f @markpasc Merge branch 'master' into wordnik-spell
markpasc authored
55 fetch_wordnik_resource(msg, word, 'audio', {}) (err, res, body) ->
c1293df @Aupajo Add pronunciation to Wordnik
Aupajo authored
56 pronunciations = JSON.parse(body)
5982e63 @Aupajo Add Dictionary
Aupajo authored
57
c1293df @Aupajo Add pronunciation to Wordnik
Aupajo authored
58 if pronunciations.length == 0
59 msg.send "No pronounciation for \"#{word}\" found."
5982e63 @Aupajo Add Dictionary
Aupajo authored
60 else
c1293df @Aupajo Add pronunciation to Wordnik
Aupajo authored
61 pronunciation = pronunciations[0]
62 msg.send pronunciation.fileUrl
5982e63 @Aupajo Add Dictionary
Aupajo authored
63
52a3c27 @markpasc Add 'spell' command to wordnik script
markpasc authored
64 robot.respond /spell(?: me)? (.*)/i, (msg) ->
65 word = msg.match[1]
66
ff0228f @markpasc Merge branch 'master' into wordnik-spell
markpasc authored
67 fetch_wordnik_resource(msg, word, '', {includeSuggestions: 'true'}) (err, res, body) ->
68 wordinfo = JSON.parse(body)
69 if wordinfo.canonicalForm
70 msg.send "\"#{word}\" is a word."
71 else if not wordinfo.suggestions
72 msg.send "No suggestions for \"#{word}\" found."
73 else
74 list = wordinfo.suggestions.join(', ')
75 msg.send "Suggestions for \"#{word}\": #{list}"
76
bf41933 @hoovercj Added support for Bigrams/Phrases
hoovercj authored
77 # Bigrams
78 robot.respond /bigram( me)? (.*)/i, (msg) ->
79 word = msg.match[2]
80
81 fetch_wordnik_resource(msg, word, 'phrases', {}) (err, res, body) ->
82 phrases = JSON.parse(body)
83
84 if phrases.length == 0
85 msg.send "No bigrams for \"#{word}\" found."
86 else
87 reply = "Bigrams for \"#{word}\":\n"
88
89 phrases = phrases.forEach (phrase) ->
90 if phrase.gram1 != undefined and phrase.gram2 != undefined
91 reply += "#{phrase.gram1} #{phrase.gram2}\n"
92
93 msg.send reply
94
ff0228f @markpasc Merge branch 'master' into wordnik-spell
markpasc authored
95 fetch_wordnik_resource = (msg, word, resource, query, callback) ->
3406d66 @technicalpickles Update "w" help comments
technicalpickles authored
96 # FIXME prefix with HUBOT_ for
c1293df @Aupajo Add pronunciation to Wordnik
Aupajo authored
97 if process.env.WORDNIK_API_KEY == undefined
98 msg.send "Missing WORDNIK_API_KEY env variable."
99 return
100
101 msg.http("http://api.wordnik.com/v4/word.json/#{escape(word)}/#{escape(resource)}")
ff0228f @markpasc Merge branch 'master' into wordnik-spell
markpasc authored
102 .query(query)
c1293df @Aupajo Add pronunciation to Wordnik
Aupajo authored
103 .header('api_key', process.env.WORDNIK_API_KEY)
ff0228f @markpasc Merge branch 'master' into wordnik-spell
markpasc authored
104 .get(callback)
Something went wrong with that request. Please try again.