Ошибка типа: tokenize_lemmatize_spacy () отсутствует 1 обязательный позиционный аргумент: 'first_arg' - PullRequest
1 голос
/ 25 февраля 2020

Итак, я получаю эту ошибку, пытаясь вернуть другое значение моего векторизатора sklearn:

>>>  python features.py lemmatize_PS Gold.xlsx

Traceback (most recent call last):
  File "features.py", line 351, in <module>
    fea1, fea0, fe, fi, fo, fu, fo, fea2 = build_feature_matrix_S(sentences)
  File "features.py", line 100, in build_feature_matrix_S
    vectorizer_freq = CountVectorizer(tokenizer = tokenize_lemmatize_spacy(first_arg), binary=False, min_df=5, ngram_range=gram)
TypeError: tokenize_lemmatize_spacy() missing 1 required positional argument: 'first_arg'


Функция tokenize_lemmatize выглядит так:

def tokenize_lemmatize_spacy(texte, first_arg):
    texte = normalize(texte)
    mytokens = nlp(texte)

    if first_arg == 'lemmatize_only':
        # Lemmatizing each token and converting each token into lowercase
        mytokens = [word.lemma_.lower().strip() for word in mytokens if word.pos_ != "SPACE"]

    elif first_arg == 'lemmatize_PS':
        # Lemmatizing each token and converting each token into lowercase
        mytokens = [word.lemma_.lower().strip() for word in mytokens if word.pos_ != "SPACE" ]
        # Removing stop words and punctuations
        mytokens = [word for word in mytokens if word not in stopwords and word not in punctuations]

    else:
        raise Exception("Wrong feature type entered. Possible values:  'lemmatize_only', 'lemmatize_PS'")
    return mytokens

Я протестировал функция token_lemmatize, и она работает, но теперь, когда я пытаюсь использовать ее в другом скрипте, я получаю сообщение об ошибке ниже.

1 Ответ

0 голосов
/ 26 февраля 2020

CountVectorizer ожидает вызова, но вы пытаетесь передать вывод функции.

использование partial

from functools import partial
vectorizer_freq = CountVectorizer(tokenizer=partial(tokenize_lemmatize_spacy,
                                                    first_arg='lemmatize_PS')
                                  binary=False, min_df=5, ngram_range=gram)
...