0

I have been working on a chat bot. I have used nltk.chat quite extensively for my bot. But I want to add more features to the pairs other than the default one. when i try to add new pair i'm, getting a error.

alex=Chat(pair,reflections)
File "/usr/local/lib/python2.7/dist-packages/nltk/chat/util.py", in __init__
self._pairs = [(re.compile(x, re.IGNORECASE),y) for (x,y) in pairs]
ValueError: too many values to unpack

my code:

pair=(
      (r'test (.*)',('a','b'))
     )
sisanared
  • 4,175
  • 2
  • 27
  • 42
Abu Tahir
  • 362
  • 3
  • 16

2 Answers2

1

Your pair is losing the structure you intended, for example:

pair=((((r'test (.*)', ('a','b')))))

would be the same as this:

('test (.*)', ('a', 'b'))

Where the first element is 'test (.*)' and the second is ('a', 'b').

Basically just adding parentheses to a tuple structure does not mean you are nesting it. In order to achieve what you intended, you should be explicit in your definition of the tuple. If you change the pair initialization to this:

pair=( 
      (r'test (.*)', ('a','b')),
     )

(Note the comma before the final closing parenthesis)

Now your iteration would work. As the first element is ('test (.*)', ('a', 'b')) and not just the string test (.*)

Hope this explains the cause of the error.

sisanared
  • 4,175
  • 2
  • 27
  • 42
1

The accepted answer is spot-on about the source of the problem, but it leaves out an important detail: Python style dictates that you use tuples only for collections in which each element has a specific role. For undifferentiated collections, use list (square brackets). Each of the tuples you pass in the list pair is a pair of (<regexp>, <data>), and hence properly written as a tuple. But pair itself is just a collection of such tuples, and should be written as a list, not as a tuple. As a bonus, list brackets are unambiguous and you don't need to remember to add a comma when your list only has one element:

# (works properly)
pair = [
      (r'test (.*)', ('a','b'))
     ]
Community
  • 1
  • 1
alexis
  • 48,685
  • 16
  • 101
  • 161
  • the problem is to append my data to existing data structure so i have do it that way! – Abu Tahir Jan 20 '17 at 06:09
  • No you don't. You can't even append _anything_ to a tuple, don't you know that? – alexis Jan 20 '17 at 22:20
  • i have merged it .. like i created a new data structure for it. because the package i'm using it is like it need in such a fashion – Abu Tahir Jan 21 '17 at 04:43
  • "The package you are using" will not work if you change the round brackets to square brackets, thus passing a list? That's frankly unbelievable. Why don't you try it instead of telling me "it won't work". – alexis Jan 21 '17 at 14:15