为spacy反序列化对象[python]

2024-06-08 07:23:53 发布

您现在位置:Python中文网/ 问答频道 /正文

我正在尝试从序列化对象加载EntityRuler。加载对象的过程不会失败或发生任何事情,但我确信它不会失败。我用try和except语句进行了双重检查,但仍然没有设置新的规则。我不确定这是否与EntityRuler有关

这就是我序列化的方式:

def save_loc_rules(stations):
    print('Adding rules')
    for station in stations:
        ruler.add_patterns([{"label": "GPE", "pattern": station.lower()}])
    nlp.add_pipe(ruler)
    rules = ruler.to_bytes()
    with open("location_rules.pickle", "wb") as f:
        pickle.dump((rules), f)
    print('Rules added')

下面是我如何反序列化的:

def load_loc_rules(ruler):
    #ruler = EntityRuler(nlp)
    print('Loading location rules')
    with open("location_rules.pickle", "rb") as f:
        rules = pickle.load(f)
    print('Loading locations failed')
    #print('Location rules loaded')
    ruler = ruler.from_bytes(rules)

Tags: 对象addbytes序列化nlpdeflocationloc
1条回答
网友
1楼 · 发布于 2024-06-08 07:23:53

在文档页面上花了很多时间之后,我意识到当我加载EntityRuler对象时,我必须将其添加到NLP管道中。 因此,在加载之后,我添加了:

nlp.add_pipe(ruler)

相关问题 更多 >

    热门问题