[英]Python tokenize sentence with optional key/val pairs
我正在嘗試解析一個句子(或一行文本),其中有一個句子,並且可以選擇在同一行中跟隨一些鍵/值對。 鍵/值對不僅是可選的,而且是動態的。 我正在尋找結果是這樣的:
輸入:
"There was a cow at home. home=mary cowname=betsy date=10-jan-2013"
輸出:
Values = {'theSentence' : "There was a cow at home.",
'home' : "mary",
'cowname' : "betsy",
'date'= "10-jan-2013"
}
輸入:
"Mike ordered a large hamburger. lastname=Smith store=burgerville"
輸出:
Values = {'theSentence' : "Mike ordered a large hamburger.",
'lastname' : "Smith",
'store' : "burgerville"
}
輸入:
"Sam is nice."
輸出:
Values = {'theSentence' : "Sam is nice."}
感謝您的任何輸入/指示。 我知道這句話是一個家庭作業問題,但我只是python新手。 我知道這可能是正則表達式的解決方案,但對於正則表達式我並不是最好的解決方案。
我會用re.sub
:
import re
s = "There was a cow at home. home=mary cowname=betsy date=10-jan-2013"
d = {}
def add(m):
d[m.group(1)] = m.group(2)
s = re.sub(r'(\w+)=(\S+)', add, s)
d['theSentence'] = s.strip()
print d
如果您願意,可以使用以下更緊湊的版本:
d = {}
d['theSentence'] = re.sub(r'(\w+)=(\S+)',
lambda m: d.setdefault(m.group(1), m.group(2)) and '',
s).strip()
或者,也許findall
是更好的選擇:
rx = '(\w+)=(\S+)|(\S.+?)(?=\w+=|$)'
d = {
a or 'theSentence': (b or c).strip()
for a, b, c in re.findall(rx, s)
}
print d
第一步是要做
inputStr = "There was a cow at home. home=mary cowname=betsy date=10-jan-2013"
theSentence, others = str.split('.')
然后,您要分手“其他”。 試一試split()(您傳入的參數告訴Python分割字符串的內容),然后看看可以做什么。 :)
如果您的句子肯定會結束.
,那么您可以采用以下方法。
>>> testList = inputString.split('.')
>>> Values['theSentence'] = testList[0]+'.'
對於其余的值,只需執行。
>>> for elem in testList[1].split():
key, val = elem.split('=')
Values[key] = val
給你這樣的Values
>>> Values
{'date': '10-jan-2013', 'home': 'mary', 'cowname': 'betsy', 'theSentence': 'There was a cow at home.'}
>>> Values2
{'lastname': 'Smith', 'theSentence': 'Mike ordered a large hamburger.', 'store': 'burgerville'}
>>> Values3
{'theSentence': 'Sam is nice.'}
假設可能只有1個點,該點將句子和作業對分開:
input = "There was a cow at home. home=mary cowname=betsy date=10-jan-2013"
sentence, assignments = input.split(". ")
result = {'theSentence': sentence + "."}
for item in assignments.split():
key, value = item.split("=")
result[key] = value
print result
印刷品:
{'date': '10-jan-2013',
'home': 'mary',
'cowname': 'betsy',
'theSentence': 'There was a cow at home.'}
假設=
不在句子本身中出現。 這似乎比假設句子以.
結尾更有效.
。
s = "There was a cow at home. home=mary cowname=betsy date=10-jan-2013"
eq_loc = s.find('=')
if eq_loc > -1:
meta_loc = s[:eq_loc].rfind(' ')
s = s[:meta_loc]
metastr = s[meta_loc + 1:]
metadict = dict(m.split('=') for m in metastr.split())
else:
metadict = {}
metadict["theSentence"] = s
因此,像往常一樣,有很多方法可以做到這一點。 這是一種基於正則表達式的方法,用於查找鍵=值對:
import re
sentence = "..."
values = {}
for match in re.finditer("(\w+)=(\S+)", sentence):
if not values:
# everything left to the first key/value pair is the sentence
values["theSentence"] = sentence[:match.start()].strip()
else:
key, value = match.groups()
values[key] = value
if not values:
# no key/value pairs, keep the entire sentence
values["theSentence"] = sentence
假定鍵是Python樣式的標識符,並且該值包含一個或多個非空白字符。
假設第一個句點將句子與值分開,則可以使用類似以下的內容:
#! /usr/bin/python3
a = "There was a cow at home. home=mary cowname=betsy date=10-jan-2013"
values = (lambda s, tail: (lambda d, kv: (d, d.update (kv) ) ) ( {'theSentence': s}, {k: v for k, v in (x.split ('=') for x in tail.strip ().split (' ') ) } ) ) (*a.split ('.', 1) ) [0]
print (values)
沒有人發布可理解的單線。 問題得到了回答,但是必須在一行中完成,這是Python方式!
{"theSentence": sentence.split(".")[0]}.update({item.split("=")[0]: item.split("=")[1] for item in sentence.split(".")[1].split()})
嗯,不是超級優雅,但完全是同一行。 甚至沒有進口。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.