Merge lp:~stefanor/ibid/dict-398764 into lp:~ibid-core/ibid/old-trunk-pack-0.92

Proposed by Stefano Rivera
Status: Merged
Approved by: Stefano Rivera
Approved revision: 718
Merged at revision: 720
Proposed branch: lp:~stefanor/ibid/dict-398764
Merge into: lp:~ibid-core/ibid/old-trunk-pack-0.92
Diff against target: None lines
To merge this branch: bzr merge lp:~stefanor/ibid/dict-398764
Reviewer Review Type Date Requested Status
Jonathan Hitchcock Approve
Michael Gorven Approve
Review via email: mp+8788@code.launchpad.net
To post a comment you must log in.
Revision history for this message
Stefano Rivera (stefanor) wrote :

Don't know what you think of the encoding situation here. The dict protocol is UTF-8 capable, but the strategies don't do a good job with it, and none of the dictionaries currently include non-7-bit characters.

The Python dictclient library just deals in strings.

Query: spell naïve
Response: That doesn't seem correct, but I can't find anything to suggest

One thought is to make the bot say "My dictionaries can't understand non-latin characters, try asking again, in plain text" or just silently strip them and hope that "lev" will do the trick. Although in that case, the "you've spelt that correctly" would refer to the stripped version, and thus be wrong.

Revision history for this message
Michael Gorven (mgorven) wrote :

 review approve

review: Approve
Revision history for this message
Jonathan Hitchcock (vhata) wrote :

This bot is getting sarcier every merge.

review: Approve
Revision history for this message
Jonathan Hitchcock (vhata) wrote :

> The Python dictclient library just deals in strings.

Biggest win - fix the dictclient library. Anything else is a workaround.

> One thought is to make the bot say "My dictionaries can't understand non-latin
> characters, try asking again, in plain text" or just silently strip them and
> hope that "lev" will do the trick. Although in that case, the "you've spelt
> that correctly" would refer to the stripped version, and thus be wrong.

You could strip the non-latin characters, and return "I know about this latin word, but not sure about your funky variant"? Otherwise I think simply stripping is the biggest win (but it should definitely report which word is correct, rather than the current situation where it just says "yebo") - saying 'this latin word is right' is better than incorrectly saying that a funky non-latin word is wrong.

review: Needs Fixing
lp:~stefanor/ibid/dict-398764 updated
718. By Stefano Rivera

Don't try and put the utf-8 encoded word in the unicode response

Revision history for this message
Stefano Rivera (stefanor) wrote :

Seems it did work. Made a minor fix to get define to work when given a non-ASCII word.

18:45 < tumbleweed> tibid: define naïve
18:45 < tibid> tumbleweed: I don't know about naïve. Maybe you meant naive?
18:45 < tumbleweed> tibid: spell naïve
18:45 < tibid> tumbleweed: Suggestions: naive

Revision history for this message
Jonathan Hitchcock (vhata) :
review: Approve

Preview Diff

[H/L] Next/Prev Comment, [J/K] Next/Prev File, [N/P] Next/Prev Hunk
1=== modified file 'ibid/plugins/dict.py'
2--- ibid/plugins/dict.py 2009-07-10 14:07:41 +0000
3+++ ibid/plugins/dict.py 2009-07-14 23:26:37 +0000
4@@ -1,7 +1,10 @@
5+from random import choice
6+
7 from dictclient import Connection
8
9 from ibid.plugins import Processor, match
10 from ibid.config import Option, IntOption
11+from ibid.utils import human_join
12
13 help = {'dict': u'Defines words and checks spellings.'}
14
15@@ -18,30 +21,54 @@
16 @match(r'^define\s+(.+?)(?:\s+using\s+(.+))?$')
17 def define(self, event, word, dictionary):
18 connection = Connection(self.server, self.port)
19- try:
20- definitions = connection.define(dictionary or '*', word)
21- except Exception, e:
22- event.addresponse(u'The dictionary complained: %s' % unicode(e))
23+ dictionary = dictionary is None and '*' or dictionary.lower()
24+ word = word.encode('utf-8')
25+ dictionaries = connection.getdbdescs().keys()
26+
27+ if dictionary != '*' and dictionary not in dictionaries:
28+ event.addresponse(
29+ u"I'm afraid I don't have a dictionary of that name. I know about: %s",
30+ human_join(sorted(dictionaries)))
31 return
32
33+ definitions = connection.define(dictionary, word)
34 if definitions:
35- event.addresponse(u', '.join([d.getdefstr() for d in definitions]))
36+ event.addresponse(u', '.join(d.getdefstr() for d in definitions))
37 else:
38- event.addresponse(u"I don't have a definition for that. Is it even a word?")
39+ suggestions = connection.match(dictionary, 'lev', word)
40+ if suggestions:
41+ event.addresponse(
42+ u"I don't know about %(word)s. Maybe you meant %(suggestions)s?", {
43+ 'word': word,
44+ 'suggestions': human_join([d.getword() for d in suggestions], conjunction=u'or'),
45+ })
46+ else:
47+ event.addresponse(u"I don't have a definition for that. Is it even a word?")
48
49 @match(r'^spell\s+(.+?)(?:\s+using\s+(.+))?$')
50 def handle_spell(self, event, word, strategy):
51 connection = Connection(self.server, self.port)
52- try:
53- correct = connection.match('*', 'exact', word)
54- if correct:
55- event.addresponse(u'That seems correct. Carry on')
56- return
57- suggestions = connection.match('*', strategy or 'lev', word)
58- except Exception, e:
59- event.addresponse(u'The dictionary complained: %s' % unicode(e))
60+ word = word.encode('utf-8')
61+ strategies = connection.getstratdescs().keys()
62+
63+ if connection.match('*', 'exact', word):
64+ event.addresponse(choice((
65+ u'That seems correct. Carry on',
66+ u'Looks good to me',
67+ u"Yup, that's a word all right",
68+ u'Yes, you *can* spell',
69+ )))
70+ return
71+
72+ strategy = strategy is None and 'lev' or strategy.lower()
73+ if strategy not in strategies:
74+ event.addresponse(
75+ u"I'm afraid I don't know about such a strategy. I know about: %s",
76+ human_join(sorted(strategies)))
77+
78+ suggestions = connection.match('*', strategy, word)
79 if suggestions:
80- event.addresponse(u'Suggestions: %s', u', '.join([d.getword() for d in suggestions]))
81+ event.addresponse(u'Suggestions: %s', human_join([d.getword() for d in suggestions]))
82 else:
83 event.addresponse(u"That doesn't seem correct, but I can't find anything to suggest")
84
85@@ -49,20 +76,21 @@
86 def handle_dictionaries(self, event):
87 connection = Connection(self.server, self.port)
88 dictionaries = connection.getdbdescs()
89- event.addresponse(u'Dictionaries: %s', u', '.join(sorted(dictionaries.keys())) or u'none')
90+ event.addresponse(u'My Dictionaries: %s', human_join(sorted(dictionaries.keys())))
91
92 @match(r'^strater?gies$')
93 def handle_strategies(self, event):
94 connection = Connection(self.server, self.port)
95 strategies = connection.getstratdescs()
96- event.addresponse(u'Strategies: %s', u', '.join(sorted(strategies.keys())))
97+ event.addresponse(u'My Strategies: %s', human_join(sorted(strategies.keys())))
98
99 @match(r'^dictionary\s+(.+?)$')
100 def handle_dictionary(self, event, dictionary):
101 connection = Connection(self.server, self.port)
102 dictionaries = connection.getdbdescs()
103+ dictionary = dictionary.lower()
104 if dictionary in dictionaries:
105- event.addresponse(dictionaries[dictionary])
106+ event.addresponse(unicode(dictionaries[dictionary]))
107 else:
108 event.addresponse(u"I don't have that dictionary")
109
110@@ -70,8 +98,9 @@
111 def handle_strategy(self, event, strategy):
112 connection = Connection(self.server, self.port)
113 strategies = connection.getstratdescs()
114+ strategy = strategy.lower()
115 if strategy in strategies:
116- event.addresponse(strategies[strategy])
117+ event.addresponse(unicode(strategies[strategy]))
118 else:
119 event.addresponse(u"I don't have that strategy")
120
121
122=== modified file 'ibid/utils.py'
123--- ibid/utils.py 2009-07-12 15:26:14 +0000
124+++ ibid/utils.py 2009-07-14 23:26:37 +0000
125@@ -221,5 +221,10 @@
126 return json.loads(data)
127 except ValueError, e:
128 raise JSONException(e)
129-
130+
131+def human_join(list, conjunction=u'and'):
132+ "Create a list like: a, b, c and d"
133+ return ((u' %s ' % conjunction)
134+ .join(filter(None, [u', '.join(list[:-1])] + list[-1:])))
135+
136 # vi: set et sta sw=4 ts=4:

Subscribers

People subscribed via source and target branches