如何使用Python Pyparsing解析带括号的单词?

2024-04-19 13:31:18 发布

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

我试图在用户查询中找到自由文本查询

让我举个例子。用户输入:

domain:example.com and Welcome to my website

目前,输出将是:

>> parser.parseString("domain:example.com and Welcome to my website")
([(['domain', ':', 'example.com'], {}), 'and Welcome to my website'], {})

我的pyparsing代码是:

word = pp.Word(pp.printables, excludeChars=":")
non_tag = word + ~pp.FollowedBy(":")
# tagged value is two words with a ":"
tag = pp.Group(word + ":" + word)
# one or more non-tag words - use originalTextFor to get back
# a single string, including intervening white space
phrase = pp.originalTextFor(non_tag[1, ...])
parser = (phrase | tag)[...]
free_text_search_res = parser.parseString(filters)

这很好,工作正常。我的问题是,我还需要正确解析以下查询:

>> parser.parseString("domain:example.com and date:[2012-12-12 TO 2014-12-12] and Welcome to my website")
([(['domain', ':', 'example.com'], {}), 'and', (['date', ':', '[2012-12-12'], {}), 'TO 2014-12-12] and Welcome to my website'], {})

date部分是错误的。我本想成为['date', ':', '[2012-12-12 TO 2014-12-12]']。我哪里做错了


Tags: andtocomparserdateexamplemydomain
1条回答
网友
1楼 · 发布于 2024-04-19 13:31:18

你可以试试下面的方法

word = pp.Word(pp.printables, excludeChars=":")
word = ("[" + pp.Word(pp.printables+ " ", excludeChars=":[]") + "]") | word
non_tag = word + ~pp.FollowedBy(":")
# tagged value is two words with a ":"
tag = pp.Group(word + ":" + word)
# one or more non-tag words - use originalTextFor to get back
# a single string, including intervening white space
phrase = pp.originalTextFor(non_tag[1, ...])
parser = (phrase | tag)[...]

# free_text_search_res = parser.parseString(filters)
# tag.parseString("date:[2012-12-12 TO 2014-12-12]")

parser.parseString("domain:example.com and date:[2012-12-12 TO 2014-12-12] and Welcome to my website")

将给你以下结果

([(['domain', ':', 'example.com'], {}), 'and', (['date', ':', '[', '2012-12-12 TO 2014-12-12', ']'], {}), 'and Welcome to my website'], {})

相关问题 更多 >