-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy path__init__.py
114 lines (98 loc) · 3.69 KB
/
__init__.py
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
105
106
107
108
109
110
111
112
113
114
from random import sample
import nltk
from nltk.corpus import swadesh, wordnet
from opsdroid.matchers import match_regex
from opsdroid.skill import Skill
class WordsHelp(Skill):
@match_regex(r"help scrabble: (.*) (.*)", case_sensitive=False)
async def help_scrabble(self, message):
"""Opsdroid will help you with scrabble."""
scrabble_letters = message.regex.group(1)
board_letter = message.regex.group(2)
puzzle_letters = nltk.FreqDist(scrabble_letters)
wordlist = nltk.corpus.words.words()
await message.respond("Please give a second, I'm thinking...")
words = [
word
for word in wordlist
if len(word) >= 4
and board_letter in word
and nltk.FreqDist(word) <= puzzle_letters
]
if len(words) > 5:
words = sample(words, 5)
if not words:
reply = "Sorry, I can't help you. You better replace some letters."
else:
reply = "Hmm... How about: {}".format(words)
await message.respond(reply)
@match_regex(r"define: (.*)", case_sensitive=False)
async def define(self, message):
"""Opsdroid will define a word and show you how it's used."""
term = message.regex.group(1)
try:
synset = wordnet.synsets(term)
word = str(term) + str(synset[0])[-7:-2]
definition = wordnet.synset(word).definition()
examples = wordnet.synset(word).examples()
synonyms = wordnet.synset(word).lemma_names()
await message.respond(
"Definition of the word '{}': {} \n"
"Synonyms: {} \n"
"You can use this word like such: {}".format(
term, definition, str(synonyms).replace("_", " "), examples
)
)
except nltk.corpus.reader.wordnet.WordNetError:
await message.respond("Sorry, I can't find anything about that word.")
@match_regex(r"translate: (.*) from: (.*) to: (.*)", case_sensitive=False)
async def translate(self, message):
"""Opsdroid with translate a word from one language to another."""
term = message.regex.group(1)
from_language = message.regex.group(2)
to_language = message.regex.group(3)
_dictionary = dict()
languages_dict = {
"spanish": "es",
"belorussian": "be",
"bulgarian": "bg",
"catalan": "cs",
"czech": "cs",
"german": "de",
"english": "en",
"french": "fr",
"croatian": "hr",
"italian": "it",
"latin": "la",
"macedonian": "mk",
"dutch": "nl",
"polish": "pl",
"portuguese": "pt",
"romanian": "ro",
"russian": "ru",
"slovak": "sk",
"slovenian": "sl",
"serbian": "sr",
"ukrainian": "uk",
}
entries = swadesh.entries(
[
languages_dict.get(from_language, "english"),
languages_dict.get(to_language, "english"),
]
)
for word in entries:
_word = word[0].split(", ")
if len(_word) > 1:
_dictionary[_word[0]] = word[1]
_dictionary[_word[1]] = word[1]
else:
_dictionary[word[0]] = word[1]
translation = _dictionary.get(
term, "Sorry, I can't find the " "translation for that word :("
)
await message.respond(
"The {} word '{}' in {} is: {}".format(
from_language, term, to_language, translation
)
)