This repository was archived by the owner on Jun 8, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 1.9k
/
Copy pathwordnik.coffee
104 lines (85 loc) · 3.27 KB
/
wordnik.coffee
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
# Description:
# Dictionary definitions with the Wordnik API.
#
# Dependencies:
# None
#
# Configuration:
# WORDNIK_API_KEY
#
# Commands:
# hubot define me <word> - Grabs a dictionary definition of a word.
# hubot pronounce me <word> - Links to a pronunciation of a word.
# hubot spell me <word> - Suggests correct spellings of a possible word.
# hubot bigram me <word> - Grabs the most frequently used bigram phrases containing this word
#
# Notes:
# You'll need an API key from http://developer.wordnik.com/
# FIXME This should be merged with word-of-the-day.coffee
#
# Author:
# Aupajo
# markpasc
module.exports = (robot) ->
# Word definition
robot.respond /define( me)? (.*)/i, (msg) ->
word = msg.match[2]
fetch_wordnik_resource(msg, word, 'definitions', {}) (err, res, body) ->
definitions = JSON.parse(body)
if definitions.length == 0
msg.send "No definitions for \"#{word}\" found."
else
reply = "Definitions for \"#{word}\":\n"
lastSpeechType = null
definitions = definitions.forEach (def) ->
# Show the part of speech (noun, verb, etc.) when it changes
if def.partOfSpeech != lastSpeechType
reply += " (#{def.partOfSpeech})\n" if def.partOfSpeech != undefined
# Track the part of speech
lastSpeechType = def.partOfSpeech
# Add the definition
reply += " - #{def.text}\n"
msg.send reply
# Pronunciation
robot.respond /(pronounce|enunciate)( me)? (.*)/i, (msg) ->
word = msg.match[3]
fetch_wordnik_resource(msg, word, 'audio', {}) (err, res, body) ->
pronunciations = JSON.parse(body)
if pronunciations.length == 0
msg.send "No pronounciation for \"#{word}\" found."
else
pronunciation = pronunciations[0]
msg.send pronunciation.fileUrl
robot.respond /spell(?: me)? (.*)/i, (msg) ->
word = msg.match[1]
fetch_wordnik_resource(msg, word, '', {includeSuggestions: 'true'}) (err, res, body) ->
wordinfo = JSON.parse(body)
if wordinfo.canonicalForm
msg.send "\"#{word}\" is a word."
else if not wordinfo.suggestions
msg.send "No suggestions for \"#{word}\" found."
else
list = wordinfo.suggestions.join(', ')
msg.send "Suggestions for \"#{word}\": #{list}"
# Bigrams
robot.respond /bigram( me)? (.*)/i, (msg) ->
word = msg.match[2]
fetch_wordnik_resource(msg, word, 'phrases', {}) (err, res, body) ->
phrases = JSON.parse(body)
if phrases.length == 0
msg.send "No bigrams for \"#{word}\" found."
else
reply = "Bigrams for \"#{word}\":\n"
phrases = phrases.forEach (phrase) ->
if phrase.gram1 != undefined and phrase.gram2 != undefined
reply += "#{phrase.gram1} #{phrase.gram2}\n"
msg.send reply
fetch_wordnik_resource = (msg, word, resource, query, callback) ->
# FIXME prefix with HUBOT_ for
if process.env.WORDNIK_API_KEY == undefined
msg.send "Missing WORDNIK_API_KEY env variable."
return
msg.http("http://api.wordnik.com/v4/word.json/#{escape(word)}/#{escape(resource)}")
.query(query)
.header('api_key', process.env.WORDNIK_API_KEY)
.get(callback)