Pandas:LangDetectException:文本中没有功能

2024-05-23 17:43:39 发布

您现在位置:Python中文网/ 问答频道 /正文

我使用这段代码来检测comments列中的语言。遇到了这个错误。这里是初学者。我理解这可能是因为comments列中的某些行没有包含正确的描述

for r in reviews_tk.comments:
    lang = detect(r)
    reviews_tk['language'] = reviews_tk['comments'].apply(detect)
---------------------------------------------------------------------------
LangDetectException                       Traceback (most recent call last)
<ipython-input-11-c920d56aebd6> in <module>
      1 for r in reviews_tk.comments:
      2     lang = detect(r)
----> 3     reviews_tk['language'] = reviews_tk['comments'].apply(detect)

~\anaconda3\lib\site-packages\pandas\core\series.py in apply(self, func, convert_dtype, args, **kwds)
   3846             else:
   3847                 values = self.astype(object).values
-> 3848                 mapped = lib.map_infer(values, f, convert=convert_dtype)
   3849 
   3850         if len(mapped) and isinstance(mapped[0], Series):

pandas\_libs\lib.pyx in pandas._libs.lib.map_infer()

~\anaconda3\lib\site-packages\langdetect\detector_factory.py in detect(text)
    128     detector = _factory.create()
    129     detector.append(text)
--> 130     return detector.detect()
    131 
    132 

~\anaconda3\lib\site-packages\langdetect\detector.py in detect(self)
    134         which has the highest probability.
    135         '''
--> 136         probabilities = self.get_probabilities()
    137         if probabilities:
    138             return probabilities[0].lang

~\anaconda3\lib\site-packages\langdetect\detector.py in get_probabilities(self)
    141     def get_probabilities(self):
    142         if self.langprob is None:
--> 143             self._detect_block()
    144         return self._sort_probability(self.langprob)
    145 

~\anaconda3\lib\site-packages\langdetect\detector.py in _detect_block(self)
    148         ngrams = self._extract_ngrams()
    149         if not ngrams:
--> 150             raise LangDetectException(ErrorCode.CantDetectError, 'No features in text.')
    151 
    152         self.langprob = [0.0] * len(self.langlist)

LangDetectException: No features in text.


Tags: inpyselfiflibpackagessitedetector