基于 Spacy 令牌的匹配,令牌之间的令牌数为 'n'
Spacy token-based matching with 'n' number of tokens between tokens
我正在使用 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": "?"}
可选标记,然后添加三个可选 IS_ALPHA
标记:
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" : "?"
表示令牌可以重复 1 次或 0 次,即它只能出现一次或消失。
我正在使用 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": "?"}
可选标记,然后添加三个可选 IS_ALPHA
标记:
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" : "?"
表示令牌可以重复 1 次或 0 次,即它只能出现一次或消失。