TextBlob中的翻译无效,语言始终未知

时间:2019-01-25 11:04:40

标签: python pandas textblob

我有下面的脚本,可以阐明一些评论背后的观点。 一切都很好,但我也想将其使用的任何语言翻译成英语。

但是,使用以下内容,响应始终是“未知语言”。有人对此有何想法?

   import pandas as pd
   from textblob import TextBlob
   import numpy as np
   import os

   path = 'Desktop/fanbase.csv'
   df = pd.read_csv(path, delimiter=',', header='infer', encoding = "ISO-8859-1")
   df2 = df[['response']]
   df2['subjectivity'] = df2.response.apply(lambda x: TextBlob(str(x)).sentiment.subjectivity)
   df2['polarity'] = df2.response.apply(lambda x: TextBlob(str(x)).sentiment.polarity)
   df2['neutral'] = np.where(((df2['polarity']>-0.3) & (df2['polarity']<0.3)),'YES','NO')
   df2['positive'] = np.where(((df2['polarity']>0.3) & (df2['polarity']<1.1)),'YES','NO')
   df2['negative'] = np.where(((df2['polarity']>-1.1) & (df2['polarity']<-0.3)),'YES','NO')
   df2['response_length'] = df2['response'].str.len()
   df3 = df2.loc[df2['response_length'] > 10]
   try:
       df3['Language'] = df3.response.apply(lambda x: TextBlob(str(x)).detect_language())
       try:
           df3['Translated'] = df3.apply(lambda x: TextBlob(str(x.response)).translate(from_lang=x.Language, to='en'), axis=1)
       except:
           df3['Translated'] = df3['response']
   except:
       df3['Language'] = 'Language Unknown'
   file_path = 'Desktop/sentiment'
   if not os.path.exists(file_path):
       os.makedirs(file_path)
   outpath = "Desktop/sentiment/out.csv"

0 个答案:

没有答案