为 spacy [python] 反序列化对象

deserializing object for spacy [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)

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

nlp.add_pipe(ruler)