databricks - 在笔记本中的 Databrick 上,报告缺少 spacy jsonschema,但已安装
问题描述
在显然已成功加载 Spacy 的笔记本中 Azure 上的 Databricks 上。
使用 Matcher 迭代传递给它的文档时出现以下代码错误。jsonschema被声明为丢失,但是在检查时,json 模式已安装并且还使用
%sh
pip install jsonschema
稍后使其特定于版本
%sh
pip install 'jsonschema>=2.6.0,<3.1.0'
两者都安装成功。
这是错误消息:
---------------------------------------------------------------------------
ValueError Traceback (most recent call last)
<command-3008938145872993> in <module>
---> 12 ALL_DATA_tmp = [parse_train_data_sorted(d) for d in nlp.pipe(text_tmp_ls) ]
13 ALL_DATA = ALL_DATA + ALL_DATA_tmp
14 print(f'length ALL_DATA : {len(ALL_DATA)}')
<command-3008938145872993> in <listcomp>(.0)
10 #ALL_DATA_tmp = [parse_train_data(d) for d in
nlp.pipe(text_tmp_ls) ]
11
---> 12 ALL_DATA_tmp = [parse_train_data_sorted(d) for d in nlp.pipe(text_tmp_ls) ]
13 ALL_DATA = ALL_DATA + ALL_DATA_tmp
14 print(f'length ALL_DATA : {len(ALL_DATA)}')
<command-3008938145872990> in parse_train_data_sorted(doc)
27 prev_end = -1
28
---> 29 detections_unsorted = iterate_matchers(doc)
30
31 detections_sorted = sorted(detections_unsorted, key=lambda element: (element[1], element[2]))
<command-3008938145872990> in iterate_matchers(doc)
5 '''
6 detections = []
----> 7 matcher = Matcher(nlp.vocab, validate=True)
8 matcher.add("STANDARDS_9239", None, *create_patterns_9239())
9 detections = detections + [(doc[start:end].start_char, doc[start:end].end_char, 'STANDARD_9239') for idx, start, end in matcher(doc)]
matcher.pyx in spacy.matcher.matcher.Matcher.__init__()
/databricks/python/lib/python3.7/site-packages/spacy/util.py in get_json_validator(schema)
744 # TODO: replace with (stable) Draft6Validator, if available
745 if jsonschema is None:
--> 746 raise ValueError(Errors.E136)
747 return jsonschema.Draft4Validator(schema)
748
ValueError: [E136] This additional feature requires the jsonschema library to be
installed:
pip install jsonschema
解决方案
我这样解决了这个问题:
- 在 Databricks 中定义 Spacy 库,确保不选择自动安装
蓝色 spacy 链接应指向以下内容:https ://pypi.org/project/spacy/
- 启动版本 7 ML 集群。一旦开始。
导航到库并手动选择并单击集群上的安装。
现在打开笔记本并运行单元格,
%sh
pip install 'jsonschema>=2.6.0,<3.1.0'
在下一次单元运行中
%sh
python -m spacy 验证
这证实 Spacy 存在于集群上但没有模型
在下一个单元中安装模型
%sh
python -m spacy 下载 en_core_web_md
验证模型现在存在。
%sh
python -m spacy 验证
在下一个单元格中运行以下导入 python 代码
进口空间
导入json
8. 在下一个单元格运行并加载(在我的情况下)中等大小的英语模型中,您可能正在为您的应用程序加载和运行不同大小的模型和语言。有关确切名称,请参见 Spacy 网站。
nlp = spacy.load("en_core_web_md")
9 .最后,失败的代码现在可以工作了。
后来,以前出错的代码现在可以工作了。
row_step =20
ALL_DATA = []
for i in range (0,df.shape[0] , row_step) :
start_pos = i
end_pos = i + row_step
print(f'{start_pos} {end_pos}')
text_tmp_ls = df.iloc[start_pos:end_pos]['text'].to_list()
ALL_DATA_tmp = [parse_train_data_sorted(d) for d in nlp.pipe(text_tmp_ls) ]
ALL_DATA = ALL_DATA + ALL_DATA_tmp
print(f'length ALL_DATA : {len(ALL_DATA)}')
现在 Spacy 正在遍历一个文本列表,并使用 Matcher 一次处理列表 20 个项目的子集来查找实体。这避免了内存泄露是一种方便的技术。
我相信所有 shell pip 命令都应该在可能依赖于它们的任何 python 导入之前运行。
推荐阅读
- database - 如何向 DB2 中的列插入超过 32KB 的数据?
- sql - 如何根据多个指标获取不同的记录
- java - 如何从弹簧启动重定向/转发角度页面?
- spring - 在 Spring 4 中 Rest API 调用与 UrlBasedViewResolver 冲突
- asterisk - Astrisk 中的媒体(视频)谈判
- reactjs - Jest/Enzyme 单元测试:如何将存储传递给使用 redux 4 和 react-redux 6 连接功能的浅层组件
- parameters - 如何获取 blob 名称并用作数据工厂 V2 中下一个活动的参数或变量
- sql - “如何为不同的交付ID的不同数量的carton_id编写子查询
- ios - 从结构迁移到 Firebase 时,您的一个或多个项目在链接时遇到错误
- python - 不可序列化 JSON - Python + Flask + Sqlalchemy