【问题标题】:Problem using spacy tokenizer for count vectorizer使用 spacy 标记器进行计数向量器的问题
【发布时间】:2021-11-21 20:22:50
【问题描述】:

我正在尝试使用 Spacy 模块对亚马逊产品评论进行情绪分析,以预处理文本数据。我使用的代码正是this。我根据链接中显示的内容修改了我正在使用的数据集。我收到了错误:

 TypeError       Traceback (most recent call last)
<ipython-input-139-bcbf2d3c9cce> in <module>
      4                  ('classifier', classifier)])
      5 # Fit our data
----> 6 pipe_countvect.fit(X_train,y_train)
      7 # Predicting with a test dataset
      8 sample_prediction = pipe_countvect.predict(X_test)

~\.conda\envs\py36\lib\site-packages\sklearn\pipeline.py in fit(self, X, y, **fit_params)
    328         """
    329         fit_params_steps = self._check_fit_params(**fit_params)
--> 330         Xt = self._fit(X, y, **fit_params_steps)
    331         with _print_elapsed_time('Pipeline',
    332                                  self._log_message(len(self.steps) - 1)):

~\.conda\envs\py36\lib\site-packages\sklearn\pipeline.py in _fit(self, X, y, **fit_params_steps)
    294                 message_clsname='Pipeline',
    295                 message=self._log_message(step_idx),
--> 296                 **fit_params_steps[name])
    297             # Replace the transformer of the step with the fitted
    298             # transformer. This is necessary when loading the transformer

~\.conda\envs\py36\lib\site-packages\joblib\memory.py in __call__(self, *args, **kwargs)
    350 
    351     def __call__(self, *args, **kwargs):
--> 352         return self.func(*args, **kwargs)
    353 
    354     def call_and_shelve(self, *args, **kwargs):

~\.conda\envs\py36\lib\site-packages\sklearn\pipeline.py in _fit_transform_one(transformer, X, y, weight, message_clsname, message, **fit_params)
    738     with _print_elapsed_time(message_clsname, message):
    739         if hasattr(transformer, 'fit_transform'):
--> 740             res = transformer.fit_transform(X, y, **fit_params)
    741         else:
    742             res = transformer.fit(X, y, **fit_params).transform(X)

~\.conda\envs\py36\lib\site-packages\sklearn\feature_extraction\text.py in fit_transform(self, raw_documents, y)
   1197 
   1198         vocabulary, X = self._count_vocab(raw_documents,
-> 1199                                           self.fixed_vocabulary_)
   1200 
   1201         if self.binary:

~\.conda\envs\py36\lib\site-packages\sklearn\feature_extraction\text.py in _count_vocab(self, raw_documents, fixed_vocab)
   1108         for doc in raw_documents:
   1109             feature_counter = {}
-> 1110             for feature in analyze(doc):
   1111                 try:
   1112                     feature_idx = vocabulary[feature]

~\.conda\envs\py36\lib\site-packages\sklearn\feature_extraction\text.py in _analyze(doc, analyzer, tokenizer, ngrams, preprocessor, decoder, stop_words)
    104             doc = preprocessor(doc)
    105         if tokenizer is not None:
--> 106             doc = tokenizer(doc)
    107         if ngrams is not None:
    108             if stop_words is not None:

TypeError: 'str' object is not callable

我不确定是什么导致了这个错误以及如何摆脱它。我很确定计数矢量化器会产生一个稀疏矩阵,而不是一个字符串。我考虑过的一件事是我正在使用 spacy 标记器,它在链接中用作vectorizer = CountVectorizer(tokenizer = spacy_tokenizer, ngram_range=(1,1)) 但是当我运行程序时它说 spacy_tokenizer 未定义。所以我改用vectorizer = CountVectorizer(tokenizer = 'spacy', ngram_range=(1,1))。但是,如果我删除它,那么我不知道如何使用 spacy 标记器,无论哪种方式,我都不确定这确实是问题的原因。请帮帮我!

【问题讨论】:

    标签: debugging compiler-errors nlp spacy tokenize


    【解决方案1】:

    错误出现在这一行:

    doc = tokenizer(doc)
    

    因为它说 'str' is not callable 并且这里唯一调用的是 tokenizer 对象,所以看起来你的标记器是一个字符串,出于某种原因。

    根据您链接的代码,spacy_tokenizer 对象的配置似乎不正确。但是,尽管作为选项传递,但该变量并未在代码中的任何位置定义,因此您链接到的代码看起来不可能运行。

    如果您可以制作一个可以在此处实际粘贴问题的最小示例,这将有所帮助。

    【讨论】:

    • 您好!感谢那。我进行了更多搜索,发现我可以将此代码用于标记器: import spacy nlp = spacy.load('en', disable=['ner', 'parser', 'tagger']) def spacy_tokenizer(doc ): return [x.orth_ for x in nlp(doc)] 这行得通!
    猜你喜欢
    • 2018-07-05
    • 2022-08-14
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2018-11-27
    相关资源
    最近更新 更多