spark python pickle对象,Python-PySpark的Pickle Spacy

The documentation for Spacy 2.0 mentions that the developers have added functionality to allow for Spacy to be pickled so that it can be used by a Spark Cluster interfaced by PySpark, however, they don't give instructions on how to do this.

Can someone explain how I can pickle Spacy's English-language NE parser to be used inside of my udf functions?

This doesn't work:

from pyspark import cloudpickle

nlp = English()

pickled_nlp = cloudpickle.dumps(nlp)

解决方案

Not really an answer, but the best workaround I've discovered:

from pyspark.sql.functions import udf

from pyspark.sql.types import StringType, ArrayType

import spacy

def get_entities_udf():

def get_entities(text):

global nlp

try:

doc = nlp(unicode(text))

except:

nlp = spacy.load('en')

doc = nlp(unicode(text))

return [t.label_ for t in doc.ents]

res_udf = udf(get_entities, StringType(ArrayType()))

return res_udf

documents_df = documents_df.withColumn('entities', get_entities_udf()('text'))

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值