Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Python NLP: TypeError: not all arguments converted during string formatting

I tried the code on "Natural language processing with python", but a type error occurred.

import nltk
from nltk.corpus import brown

suffix_fdist = nltk.FreqDist()
for word in brown.words():
    word = word.lower()
    suffix_fdist.inc(word[-1:])
    suffix_fdist.inc(word[-2:])
    suffix_fdist.inc(word[-3:])
common_suffixes = suffix_fdist.items()[:100]

def pos_features(word):
    features = {}
    for suffix in common_suffixes:
        features['endswith(%s)' % suffix] = word.lower().endswith(suffix)
    return features
pos_features('people')

the error is below:

Traceback (most recent call last):
  File "/home/wanglan/javadevelop/TestPython/src/FirstModule.py", line 323, in <module>
    pos_features('people')
  File "/home/wanglan/javadevelop/TestPython/src/FirstModule.py", line 321, in pos_features
    features['endswith(%s)' % suffix] = word.lower().endswith(suffix)
TypeError: not all arguments converted during string formatting

Does anyone could help me find out where i am wrong?

like image 686
allenwang Avatar asked Aug 12 '14 04:08

allenwang


People also ask

What does TypeError not all arguments converted during string formatting mean?

In Python, “typeerror: not all arguments converted during string formatting” primarily occurs when: There is no format specifier. The format specifiers and the number of values are different. Two format specifiers are mixed.

What does %s mean in Python?

The % symbol is used in Python with a large variety of data types and configurations. %s specifically is used to perform concatenation of strings together. It allows us to format a value inside a string. It is used to incorporate another string within a string.


1 Answers

suffix is a tuple, because .items() returns (key,value) tuples. When you use %, if the right hand side is a tuple, the values will be unpacked and substituted for each % format in order. The error you get is complaining that the tuple has more entries than % formats.

You probably want just the key (the actual suffix), in which case you should use suffix[0], or .keys() to only retrieve the dictionary keys.

like image 158
nneonneo Avatar answered Oct 27 '22 15:10

nneonneo