gpt4 book ai didi

用键中的空格替换Python字典

转载 作者:太空狗 更新时间:2023-10-30 01:28:03 26 4
gpt4 key购买 nike

我有一个字符串和一个字典,我必须替换该文本中每次出现的字典键。

text = 'I have a smartphone and a Smart TV'
dict = {
'smartphone': 'toy',
'smart tv': 'junk'
}

如果keys中没有空格,我会把文本打成word,和dict一一比较。看起来它花了 O(n)。但是现在 key 里面有空间所以事情更复杂了。请建议我执行此操作的好方法,请注意 key 可能与文本大小写不匹配。

更新

我想过这个解决方案,但效率不高。 O(m*n) 或更多...

for k,v in dict.iteritems():
text = text.replace(k,v) #or regex...

最佳答案

如果文本中的关键字彼此不接近(keyword other keyword)我们可能会这样做。花了 O(n) 给我 >"<

def dict_replace(dictionary, text, strip_chars=None, replace_func=None):
"""
Replace word or word phrase in text with keyword in dictionary.

Arguments:
dictionary: dict with key:value, key should be in lower case
text: string to replace
strip_chars: string contain character to be strip out of each word
replace_func: function if exist will transform final replacement.
Must have 2 params as key and value

Return:
string

Example:
my_dict = {
"hello": "hallo",
"hallo": "hello", # Only one pass, don't worry
"smart tv": "http://google.com?q=smart+tv"
}
dict_replace(my_dict, "hello google smart tv",
replace_func=lambda k,v: '[%s](%s)'%(k,v))
"""

# First break word phrase in dictionary into single word
dictionary = dictionary.copy()
for key in dictionary.keys():
if ' ' in key:
key_parts = key.split()
for part in key_parts:
# Mark single word with False
if part not in dictionary:
dictionary[part] = False

# Break text into words and compare one by one
result = []
words = text.split()
words.append('')
last_match = '' # Last keyword (lower) match
original = '' # Last match in original
for word in words:
key_word = word.lower().strip(strip_chars) if \
strip_chars is not None else word.lower()
if key_word in dictionary:
last_match = last_match + ' ' + key_word if \
last_match != '' else key_word
original = original + ' ' + word if \
original != '' else word
else:
if last_match != '':
# If match whole word
if last_match in dictionary and dictionary[last_match] != False:
if replace_func is not None:
result.append(replace_func(original, dictionary[last_match]))
else:
result.append(dictionary[last_match])
else:
# Only match partial of keyword
match_parts = last_match.split(' ')
match_original = original.split(' ')
for i in xrange(0, len(match_parts)):
if match_parts[i] in dictionary and \
dictionary[match_parts[i]] != False:
if replace_func is not None:
result.append(replace_func(match_original[i], dictionary[match_parts[i]]))
else:
result.append(dictionary[match_parts[i]])
result.append(word)
last_match = ''
original = ''

return ' '.join(result)

关于用键中的空格替换Python字典,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/35143749/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com