[英]Filter positive and negative words from a string in python
我有一個帶有 boolean 邏輯表示的字符串,我想從中提取正面和負面的詞。 否定詞是前面有not
的詞。
示例 1 -
Input -
(A and B and C) and not (E or F)
Output -
positive - A & B & C
nagative - E | F
示例 2 -
(A and B) and not E and C
Output -
positive - A & B & C
nagative - E
我想我可以通過拉平繩子來做到這一點,例如 -
(A and B and C) and not (E or F)
變成A and B and C and not E or not F
然后使用正則表達式提取正負詞,但不知道該怎么做。
最好的方法是什么?
對於無法提供通用解決方案,我深表歉意。 在制定解決方案時,我做了以下假設:
代碼:
import regex
def get_positive_and_negative_statements(logical_string):
output = dict()
positives = regex.findall(r"(?<!not\s+)(\((?:[A-Z]+\s+(?:and|or)\s+)+[A-Z]+\)|(?<!\([^\)]*)[A-Z]+)", logical_string)
output["positive"] = " & ".join([element.replace(" and ", " & ").replace(" or ", " | ") for element in positives])
negatives = regex.findall(r"(?<=not\s+)(\((?:[A-Z]+\s+(?:and|or)\s+)+[A-Z]+\)|(?<!\([^\)]*)[A-Z]+)", logical_string)
output["negative"] = " & ".join([element.replace(" and ", " & ").replace(" or ", " | ") for element in negatives])
return output
str1 = "(A and B and C) and not (E or F)"
str2 = "(A and B) and not E and C"
output1 = get_positive_and_negative_statements(str1)
output2 = get_positive_and_negative_statements(str2)
print(f"In {str1} ---\n{output1}")
print(f"\nIn {str2} ---\n{output2}")
Output:
In (A and B and C) and not (E or F) ---
{'positive': '(A & B & C)', 'negative': '(E | F)'}
In (A and B) and not E and C ---
{'positive': '(A & B) & C', 'negative': 'E'}
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.