Poe*_*dit 12 python language-detection textblob
我有一个数据框,其中一列在每一行都有一个字符串列表。
平均而言,每个列表有 150 个单词,每个单词大约 6 个字符。
数据框的 700 行中的每一行都与一个文档有关,每个字符串都是该文档的一个单词;所以基本上我已经标记了文档的文字。
我想检测每个文档的语言,为此我首先尝试检测文档中每个单词的语言。
为此,我执行以下操作:
from textblob import TextBlob
def lang_detect(document):
lang_count = {}
for word in document:
if len(word) >= 4:
word_textblob = TextBlob(word)
lang_result = word_textblob.detect_language()
response = lang_count.get(lang_result)
if response is None:
lang_count[f"{lang_result}"] = 1
else:
lang_count[f"{lang_result}"] += 1
return lang_count
df_per_doc['languages_count'] = df_per_doc['complete_text'].apply(lambda x: lang_detect(x))
Run Code Online (Sandbox Code Playgroud)
当我这样做时,我收到以下错误:
---------------------------------------------------------------------------
HTTPError Traceback (most recent call last)
<ipython-input-42-772df3809bcb> in <module>
25
---> 27 df_per_doc['languages_count'] = df_per_doc['complete_text'].apply(lambda x: lang_detect(x))
28
29
.
.
.
647 class HTTPDefaultErrorHandler(BaseHandler):
648 def http_error_default(self, req, fp, code, msg, hdrs):
--> 649 raise HTTPError(req.full_url, code, msg, hdrs, fp)
650
651 class HTTPRedirectHandler(BaseHandler):
HTTPError: HTTP Error 429: Too Many Requests
Run Code Online (Sandbox Code Playgroud)
错误要长得多,我在中间省略了其余部分。
现在,即使我尝试仅对两个文档/行执行此操作,我也会遇到相同的错误。
有什么方法可以让我得到textblob更多文字和文件的回复?
当我尝试翻译推文时,我遇到了同样的问题。由于我超出了速率限制,它开始返回 HTTP 429 请求过多错误。
因此,对于其他可能想要在 TextBlob 上工作的人来说,最好检查一下速率限制。Google 提供有关限制的信息: https: //cloud.google.com/translate/quotas?hl =en
如果超出速率限制,则必须等到太平洋时间午夜重置配额。可能需要 24 小时才能再次生效。
另一方面,您还可以在请求之间引入延迟,以免打扰 API 服务器。
例如:当您想要翻译列表中的 TextBlob 句子时。
import time
...
for sentence in list_of_sentences:
sentence.translate()
time.sleep(1) #to sleep 1 sec
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
8662 次 |
| 最近记录: |