Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/348.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python 使用google translate API转换数据帧时出错_Python_Json_Api_Nlp_Google Translate - Fatal编程技术网

Python 使用google translate API转换数据帧时出错

Python 使用google translate API转换数据帧时出错,python,json,api,nlp,google-translate,Python,Json,Api,Nlp,Google Translate,我正在试着把1.1班的数据集的一部分翻译成僧伽罗语。我不知道是否可以直接使用json文件进行翻译 到目前为止,我尝试的是制作一个球队数据集的小数据框,并尝试将其作为演示翻译给我自己。但我犯了不同的错误。下面是我现在得到的错误。您能否帮助我修复该错误,或者告诉我使用python完成任务的更好方法 ```import googletrans from googletrans import Translator import os from google.cloud import translate

我正在试着把1.1班的数据集的一部分翻译成僧伽罗语。我不知道是否可以直接使用json文件进行翻译 到目前为止,我尝试的是制作一个球队数据集的小数据框,并尝试将其作为演示翻译给我自己。但我犯了不同的错误。下面是我现在得到的错误。您能否帮助我修复该错误,或者告诉我使用python完成任务的更好方法

```import googletrans
from googletrans import Translator

import os
from google.cloud import translate_v2 as translate

os.environ['GOOGLE_APPLICATION_CREDENTIALS']=r"C:\Users\Sathsara\Documents\Python Learning\Translation test\translationAPI\flash-medley-278816-b2012b874797.json"

# create a translator object
translator = Translator()

# use translate method to translate a string - by default, the destination language is english
translated = translator.translate('I am Sathsara Rasantha',dest='si')

# the translate method returns an object
print(translated)


# obtain translated string by using attribute .text
translated.text

import pandas as pd


translate_example = pd.read_json("example2.json")
translate_example

contexts = []
questions = []
answers_text = []
answers_start = []
for i in range(translate_example.shape[0]):
    topic = translate_example.iloc[i,0]['paragraphs']
    for sub_para in topic:
        for q_a in sub_para['qas']:
            questions.append(q_a['question'])
            answers_start.append(q_a['answers'][0]['answer_start'])
            answers_text.append(q_a['answers'][0]['text'])
            contexts.append(sub_para['context'])   
df = pd.DataFrame({"context":contexts, "question": questions, "answer_start": answers_start, "text": answers_text})
df
df=df.loc[0:2,:]
df


# make a deep copy of the data frame
df_si = df.copy()

# translate columns' name using rename function
df_si.rename(columns=lambda x: translator.translate(x).text, inplace=True)


df_si.columns


translations = {}
for column in df_si.columns:
    # unique elements of the column
    unique_elements = df_si[column].unique()
    for element in unique_elements:
        # add translation to the dictionary
        translations[element] = translator.translate(element,dest='si').text

print(translations)

# modify all the terms of the data frame by using the previously created dictionary
df_si.replace(translations, inplace = True)

# check translation
df_si.head()```
这就是我得到的错误

> --------------------------------------------------------------------------- TypeError                                 Traceback (most recent call
> last) <ipython-input-24-f55a5ca59c36> in <module>
>       5     for element in unique_elements:
>       6         # add translation to the dictionary
> ----> 7         translations[element] = translator.translate(element,dest='si').text
>       8 
>       9 print(translations)
> 
> ~\Anaconda3\lib\site-packages\googletrans\client.py in translate(self,
> text, dest, src)
>     170 
>     171         origin = text
> --> 172         data = self._translate(text, dest, src)
>     173 
>     174         # this code will be updated when the format is changed.
> 
> ~\Anaconda3\lib\site-packages\googletrans\client.py in
> _translate(self, text, dest, src)
>      73             text = text.decode('utf-8')
>      74 
> ---> 75         token = self.token_acquirer.do(text)
>      76         params = utils.build_params(query=text, src=src, dest=dest,
>      77                                     token=token)
> 
> ~\Anaconda3\lib\site-packages\googletrans\gtoken.py in do(self, text)
>     199     def do(self, text):
>     200         self._update()
> --> 201         tk = self.acquire(text)
>     202         return tk
> 
> ~\Anaconda3\lib\site-packages\googletrans\gtoken.py in acquire(self,
> text)
>     144         a = []
>     145         # Convert text to ints
> --> 146         for i in text:
>     147             val = ord(i)
>     148             if val < 0x10000:
> 
> TypeError: 'numpy.int64' object is not iterable
-------------------------------------------------------------类型错误回溯(最近的调用)
>最后)在
>5对于唯一_元素中的元素:
>6#将翻译添加到词典中
>--->7个翻译[element]=translator.translate(element,dest='si').text
>       8 
>9印刷品(翻译)
> 
>翻译中的~\Anaconda3\lib\site packages\googletrans\client.py(self,
>文本,目标,src)
>     170 
>171原点=文本
>-->172数据=self.\u翻译(文本、目标、src)
>     173 
>174#此代码将在格式更改时更新。
> 
>中的~\Anaconda3\lib\site packages\googletrans\client.py
>_翻译(self、text、dest、src)
>73 text=text.decode('utf-8')
>      74 
>-->75令牌=self.token\u收单机构.do(文本)
>76 params=utils.build_params(query=text,src=src,dest=dest,
>77令牌=令牌)
> 
>do中的~\Anaconda3\lib\site packages\googletrans\gtoken.py(self,text)
>199 def do(自我,文本):
>200自我更新()
>-->201 tk=self.acquire(文本)
>202返回tk
> 
>获取中的~\Anaconda3\lib\site packages\googletrans\gtoken.py(self,
>(文本)
>144 a=[]
>145#将文本转换为整数
>-->146对于文本中的i:
>147瓦尔=作战需求文件(一)
>148如果val<0x10000:
> 
>TypeError:“numpy.int64”对象不可编辑