【发布时间】:2021-01-26 01:34:51
【问题描述】:
我有一个具有特定格式的句子。
<subject> <action> <object> @ <price> ... // The sentence can continue
我想从句子中提取这些值。
约束:
- 主题始终为
Bob或Alice - 操作是
bought或sold - 对象可以是 1-7 个字母的任何单词 //
4apples应该返回 NULL - 价格是浮点数/整数
-
subject之前可以有句子但保证不会 包含Bob/Alice。 -
@后面可能有也可能没有空格
例子:
Hi there, Bob sold apples @2.0 dollars each
期望的输出:
Subject: Bob
Action: sold
Object: apples
Price: 2.0
目前,我通过以下方式以幼稚的方式做到这一点:
#!/usr/bin/env python3
sentence = "Hi there, alice sold apples @2.0 dollars each"
sentence = sentence.lower()
if 'alice' in sentence or 'bob' in sentence:
s_list = sentence.split(" ")
s_idx = -1
if 'bob' in sentence:
s_idx = s_list.index('bob')
elif 'alice' in sentence:
s_idx = s_list.index('alice')
if s_idx > -1:
Subject = s_list[s_idx]
Action = s_list[s_idx+1]
Object = s_list[s_idx+2] #more if/else to validate Object contraints
Price = s_list[s_idx+3] #more if/else to extract 2.0 if we get @2.0
print("Subject: {}, Action: {}, Object: {}, Price: {}".format(Subject, Action, Object, Price))
我怎样才能做得更好?可能使用re
【问题讨论】:
标签: python python-3.x regex pattern-matching