嘿,我有一个带有多语言文字的csv。我想要的只是一列,后面附有检测到的语言。所以我编码如下

from langdetect import detect
import csv
with open('C:\\Users\\dell\\Downloads\\stdlang.csv') as csvinput:
with open('C:\\Users\\dell\\Downloads\\stdlang.csv') as csvoutput:
writer = csv.writer(csvoutput, lineterminator='\n')
reader = csv.reader(csvinput)

    all = []
    row = next(reader)
    row.append('Lang')
    all.append(row)

    for row in reader:
        row.append(detect(row[0]))
        all.append(row)

    writer.writerows(all)

但是我得到的错误是LangDetectException: No features in text
追溯如下
runfile('C:/Users/dell/.spyder2-py3/temp.py', wdir='C:/Users/dell/.spyder2-py3')
Traceback (most recent call last):

  File "<ipython-input-25-5f98f4f8be50>", line 1, in <module>
    runfile('C:/Users/dell/.spyder2-py3/temp.py', wdir='C:/Users/dell/.spyder2-py3')

  File "C:\Users\dell\Anaconda3\lib\site-packages\spyderlib\widgets\externalshell\sitecustomize.py", line 714, in runfile
    execfile(filename, namespace)

  File "C:\Users\dell\Anaconda3\lib\site-packages\spyderlib\widgets\externalshell\sitecustomize.py", line 89, in execfile
    exec(compile(f.read(), filename, 'exec'), namespace)

  File "C:/Users/dell/.spyder2-py3/temp.py", line 21, in <module>
    row.append(detect(row[0]))

  File "C:\Users\dell\Anaconda3\lib\site-packages\langdetect\detector_factory.py", line 130, in detect
    return detector.detect()

  File "C:\Users\dell\Anaconda3\lib\site-packages\langdetect\detector.py", line 136, in detect
    probabilities = self.get_probabilities()

  File "C:\Users\dell\Anaconda3\lib\site-packages\langdetect\detector.py", line 143, in get_probabilities
    self._detect_block()

  File "C:\Users\dell\Anaconda3\lib\site-packages\langdetect\detector.py", line 150, in _detect_block
    raise LangDetectException(ErrorCode.CantDetectError, 'No features in text.')

LangDetectException:文本中没有功能。

这就是我的csv的样子
1)最臭的,最烟熏的,最消极的张力止痛药和情绪提升剂
2)放松,欣快感,素养,嗜睡,集中,贴身,食欲大增,素拉·杜吉尔·吉格利,体质,精神沉迷
3)Reduzierte Angst,Ruhe,gehobener Stimmung,zerebrale Energie,KörperSedierung
4)Calmante,肌肉放松,RelajaciónMental,disminucióndenáuseas
5)重いフルーティーな幸せ非常に强力な头石のバースト

请帮我解决一下这个。

最佳答案

您可以使用类似这样的方法来检测文件中的哪一行引发错误:

for row in reader:
    try:
        language = detect(row[0])
    except:
        language = "error"
        print("This row throws and error:", row[0])
    row.append(language)
    all.append(row)

您会看到的是,它可能在“重いフルーティーな幸せ非常に强力な头石のバースト”失败了。我的猜测是detect()无法“识别”该行中要分析的任何字符,这就是错误所隐含的含义。

其他情况(例如input is only a URL时)也会导致此错误。

关于python - 在python : "No features in text"中使用langdetect时出错,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/40783383/

10-12 21:01