gpt4 book ai didi

python - 基于 Spacy token 的匹配, token 之间的 token 数量为 'n'

转载 作者:行者123 更新时间:2023-12-01 06:50:13 27 4
gpt4 key购买 nike

我正在使用 spacy 来匹配某些文本(意大利语)中的特定表达式。我的文本可以多种形式出现,我正在尝试学习编写一般规则的最佳方式。我有如下 4 个案例,我想写一个适用于所有案例的通用模式。像这样的东西:

# case 1
text = 'Superfici principali e secondarie: 90 mq'
# case 2
# text = 'Superfici principali e secondarie di 90 mq'
# case 3
# text = 'Superfici principali e secondarie circa 90 mq'
# case 4
# text = 'Superfici principali e secondarie di circa 90 mq'

nlp = spacy.load('it_core_news_sm')
doc = nlp(text)

matcher = Matcher(nlp.vocab)

pattern = [{"LOWER": "superfici"}, {"LOWER": "principali"}, {"LOWER": "e"}, {"LOWER": "secondarie"}, << "some token here that allows max 3 tokens or a IS_PUNCT or nothing at all" >>, {"IS_DIGIT": True}, {"LOWER": "mq"}]

matcher.add("Superficie", None, pattern)

matches = matcher(doc)
for match_id, start, end in matches:
string_id = nlp.vocab.strings[match_id] # Get string representation
span = doc[start:end] # The matched span
print(match_id, string_id, start, end, span.text)

最佳答案

您可以添加一个 {"IS_PUNCT": True, "OP": "?"} 可选 token ,然后添加三个可选 IS_ALPHA token :

pattern = [
{"LOWER": "superfici"},
{"LOWER": "principali"},
{"LOWER": "e"},
{"LOWER": "secondarie"},
{"IS_PUNCT": True, "OP": "?"},
{"IS_ALPHA": True, "OP": "?"},
{"IS_ALPHA": True, "OP": "?"},
{"IS_ALPHA": True, "OP": "?"},
{"IS_DIGIT": True},
{"LOWER": "mq"}
]

"OP": "?" 表示 token 可以重复 1 次或 0 次,即它只能出现一次或丢失。

关于python - 基于 Spacy token 的匹配, token 之间的 token 数量为 'n',我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/59050296/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com