首页 > 解决方案 > 在笔记本中的 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  

标签: databricksspacyjsonschema

解决方案


我这样解决了这个问题:

  1. 在 Databricks 中定义 Spacy 库,确保不选择自动安装

蓝色 spacy 链接应指向以下内容:https ://pypi.org/project/spacy/

  1. 启动版本 7 ML 集群。一旦开始。

导航到库并手动选择并单击集群上的安装。

  1. 现在打开笔记本并运行单元格,

    %sh

    pip install 'jsonschema>=2.6.0,<3.1.0'

  2. 在下一次单元运行中

    %sh

    python -m spacy 验证

这证实 Spacy 存在于集群上但没有模型

  1. 在下一个单元中安装模型

    %sh

    python -m spacy 下载 en_core_web_md

  2. 验证模型现在存在。

    %sh

    python -m spacy 验证

  3. 在下一个单元格中运行以下导入 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 导入之前运行。


推荐阅读