0
votes

I've got a problem with this line:

processed = process(cleaned, lemmatizer=nltk.stem.wordnet.WordNetLemmatizer());

Why is the unexpected keyword argument popping up?

Error: TypeError: process() got an unexpected keyword argument 'lemmatizer'

Here is my code:

def process(text, filters=nltk.corpus.stopwords.words('english')):
""" Normalizes case and handles punctuation
Inputs:
    text: str: raw text
    lemmatizer: an instance of a class implementing the lemmatize() method
                (the default argument is of type nltk.stem.wordnet.WordNetLemmatizer)
Outputs:
    list(str): tokenized text
"""
lemmatizer=nltk.stem.wordnet.WordNetLemmatizer()
word_list = nltk.word_tokenize(text);

lemma_list = [];
for i in word_list:
    if i not in filters:
        try:
            lemma = lemmatizer.lemmatize(i);
            lemma_list.append(str(lemma));
        except:
            pass
return " ".join(lemma_list)


if __name__ == '__main__':
#construct filter for processor
file = open("accountant.txt").read().lower()
filters = set(nltk.word_tokenize(file))
filters.update(nltk.corpus.stopwords.words('english'))
filters = list(filters)

#webcrawling
webContent = []
dataJobs = pd.read_csv("test.csv");
webContent = []
for i in dataJobs["url"]:
    content = webCrawl(i);
    webContent.append(content);

#clean the crawled text
cleaned_list = []
for j in webContent:
        cleaned = extractUseful(j);
        processed = process(cleaned, lemmatizer=nltk.stem.wordnet.WordNetLemmatizer());
        cleaned_list.append(processed)

#save to csv
contents = pd.DataFrame({ "Content":webContent, "Cleaned": cleaned_list})
contents.to_csv("testwebcrawled.csv")


dataJobs[['jd']]= cleaned_list
dataJobs.to_csv("test_v2_crawled.csv")
1
Please edit your question to correct your indentation, and to add the full text of any errors or tracebacks. Additionally, Python does not have a line termination character, all the semi-colons ; are completely unnecessary. - MattDMo

1 Answers

0
votes

You only define one keyword argument filters in the function signature for process (the def process(...) line). If the lemmatizer is what you intend to pass as the filter try:

processed = process(cleaned, filter=nltk.stem.wordnet.WordNetLemmatizer())

If you want to be able to pass a lemmatizer as well, you should change your function signature to something like this:

def process(text, 
            filters=nltk.corpus.stopwords.words('english'),
            lemmatizer=nltk.stem.wordnet.WordNetLemmatizer()):

But note that you only need the = and the content that follows it in your function signature if you want the values after the = to be passed as the default arguments for these parameters. Otherwise, you can just do:

def process(text, filter, lemmatizer):
    ...

And call it like:

processed = process(cleaned,
                    filter=nltk.corpus.stopwords.words('english'),
                    lemmatizer=nltk.stem.wordnet.WordNetLemmatizer())