Skip to content
This repository has been archived by the owner on Sep 3, 2022. It is now read-only.

AttributeError: 'dict_values' has no attribute 'index' #154

Open
demetrii opened this issue Feb 26, 2018 · 3 comments
Open

AttributeError: 'dict_values' has no attribute 'index' #154

demetrii opened this issue Feb 26, 2018 · 3 comments

Comments

@demetrii
Copy link

demetrii commented Feb 26, 2018

Upon running for s in sources: source, predicted = predictor.predict(s) print('\n---SOURCE----\n' + source) print('---PREDICTED----\n' + predicted)

I get the following error:
INFO:tensorflow:Restoring parameters from /content/lab/datalab/punctuation/model/punctuation-5796

AttributeError Traceback (most recent call last)
in ()
9
10 for s in sources:
---> 11 source, predicted = predictor.predict(s)
12 print('\n---SOURCE----\n' + source)
13 print('---PREDICTED----\n' + predicted)

in predict(self, content)
88 for i in indices:
89 words1[i], words1[i-1] = words1[i-1], words1[i]
---> 90 words2 = [self._word_to_id.keys()[self._word_to_id.values().index(data_x[index])] for index in range(len(puncts) - 1, len(data_x))]
91 all_words = words1 + [puncts[-1]] + words2
92 content = ' '.join(all_words)

in (.0)
88 for i in indices:
89 words1[i], words1[i-1] = words1[i-1], words1[i]
---> 90 words2 = [self._word_to_id.keys()[self._word_to_id.values().index(data_x[index])] for index in range(len(puncts) - 1, len(data_x))]
91 all_words = words1 + [puncts[-1]] + words2
92 content = ' '.join(all_words)

AttributeError: 'dict_values' object has no attribute 'index'

This is how index is defined:
words1 = [self._word_to_id.keys()[self._word_to_id.values().index(data_x[index])] for index in range(len(puncts) - 1)]

indices = [i for i, w in enumerate(words1) if w in PUNCTUATIONS]

for i in indices:

words1[i], words1[i-1] = words1[i-1], words1[i] #only line in for loop

words2 = [self._word_to_id.keys()[self._word_to_id.values().index(data_x[index])] for index in range(len(puncts) - 1, len(data_x))]
all_words = words1 + [puncts[-1]] + words2
content = ' '.join(all_words)
min_step = len(puncts)

Can anyone explain why this occurs and/or how to fix this?

@demetrii
Copy link
Author

demetrii commented Mar 1, 2018

This error is because the .index() method is supported by Python 2, but unsupported by Python 3. I am currently trying to find a way around. Feel free to share (potential) solutions

@demetrii
Copy link
Author

demetrii commented Mar 4, 2018

Rewrote:
[self._word_to_id.keys()[self._word_to_id.values().index(data_x[index])] for index in range(len(puncts) - 1, len(data_x))]
to the following:
[self._word_to_id.keys()[list(self._word_to_id.values()).index(data_x[index])] for index in range(len(puncts) - 1, len(data_x))]
However, the error that I get is 'dict_keys' object does not support indexing. Any suggested solution?

@shubham8garg
Copy link

Did you try this:-
[list(self._word_to_id.keys())[list(self._word_to_id.values()).index(data_x[index])]

I guess this can unblock you.

Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants