根据条件从现有的标记和元组创建新的标记和元组
Create new tokens and tuples from existing ones based on conditions
这与 非常相关,但我很难适应我的用例。
我有一句话:"Forbes Asia 200 Best Under 500 Billion 2011"
我有这样的代币:
oldTokens = [u'Forbes', u'Asia', u'200', u'Best', u'Under', u'500', u'Billion', u'2011']
以及之前的解析器找出位置或数字槽位置的索引:
numberTokenIDs = {(7,): 2011.0, (2,): 200.0, (5,6): 500000000000.00}
locationTokenIDs = {(0, 1): u'Forbes Asia'}
token ID对应有位置或数字的token的索引,objective是获取一组新的token,如:
newTokens = [u'ForbesAsia', u'200', u'Best', u'Under', u'500Billion', u'2011']
使用新的数字和位置标记 ID 可能类似于(以避免索引越界异常):
numberTokenIDs = {(5,): 2011.0, (1,): 200.0, (4,): 500000000000.00}
locationTokenIDs = {(0,): u'Forbes Asia'}
基本上,我想遍历新的简化标记集,并最终能够创建一个名为:
的新句子
"LOCATION_SLOT NUMBER_SLOT Best Under NUMBER_SLOT NUMBER_SLOT"
通过检查新的令牌集并将正确的令牌 ID 替换为 LOCATION_SLOT
或 NUMBER_SLOT
。如果我使用当前的数字和位置令牌 ID 集执行此操作,我将得到:
"LOCATION_SLOT LOCATION_SLOT NUMBER_SLOT Best Under NUMBER_SLOT NUMBER_SLOT NUMBER_SLOT".
我该怎么做?
另一个例子是:
Location token IDs are: (0, 1)
Number token IDs are: (3, 4)
旧 sampleTokens [u'United', u'Kingdom', u'USD', u'1.240', u'billion']
我想删除令牌并更改位置和数字令牌 ID 以便能够替换如下句子:
sampleTokens[numberTokenID] = "NUMBER_SLOT"
sampleTokens[locationTokenID] = "LOCATION_SLOT"
这样替换的标记是 [u'LOCATION_SLOT', u'USD', u'NUMBER_SLOT']
请注意,如果元组中的值不止一个,串联应该串联元组中的所有值(元组也可以包含 >2 个元素,例如 The United States of America
)。
这应该有效(如果我理解正确的话):
token_by_index = dict(enumerate(oldTokens))
groups = numberTokenIDs.keys() + locationTokenIDs.keys()
for group in groups:
token_by_index[group[0]] = ''.join(token_by_index.pop(index)
for index in group)
newTokens = [token for _, token in sorted(token_by_index.items(),
key=lambda (index, _): index)]
查找新的令牌 ID:
new_index_by_token = dict(map(lambda (i, t): (t, i), enumerate(newTokens))
numberTokenIDs = {(new_index_by_token[token_by_index[group[0]]],): value
for group, value in numberTokenIDs.items()}
locationTokenIDs = {(new_index_by_token[token_by_index[group[0]]],): value
for group, value in locationTokenIDs.items()}
这与
我有一句话:"Forbes Asia 200 Best Under 500 Billion 2011"
我有这样的代币:
oldTokens = [u'Forbes', u'Asia', u'200', u'Best', u'Under', u'500', u'Billion', u'2011']
以及之前的解析器找出位置或数字槽位置的索引:
numberTokenIDs = {(7,): 2011.0, (2,): 200.0, (5,6): 500000000000.00}
locationTokenIDs = {(0, 1): u'Forbes Asia'}
token ID对应有位置或数字的token的索引,objective是获取一组新的token,如:
newTokens = [u'ForbesAsia', u'200', u'Best', u'Under', u'500Billion', u'2011']
使用新的数字和位置标记 ID 可能类似于(以避免索引越界异常):
numberTokenIDs = {(5,): 2011.0, (1,): 200.0, (4,): 500000000000.00}
locationTokenIDs = {(0,): u'Forbes Asia'}
基本上,我想遍历新的简化标记集,并最终能够创建一个名为:
的新句子"LOCATION_SLOT NUMBER_SLOT Best Under NUMBER_SLOT NUMBER_SLOT"
通过检查新的令牌集并将正确的令牌 ID 替换为 LOCATION_SLOT
或 NUMBER_SLOT
。如果我使用当前的数字和位置令牌 ID 集执行此操作,我将得到:
"LOCATION_SLOT LOCATION_SLOT NUMBER_SLOT Best Under NUMBER_SLOT NUMBER_SLOT NUMBER_SLOT".
我该怎么做?
另一个例子是:
Location token IDs are: (0, 1)
Number token IDs are: (3, 4)
旧 sampleTokens [u'United', u'Kingdom', u'USD', u'1.240', u'billion']
我想删除令牌并更改位置和数字令牌 ID 以便能够替换如下句子:
sampleTokens[numberTokenID] = "NUMBER_SLOT"
sampleTokens[locationTokenID] = "LOCATION_SLOT"
这样替换的标记是 [u'LOCATION_SLOT', u'USD', u'NUMBER_SLOT']
请注意,如果元组中的值不止一个,串联应该串联元组中的所有值(元组也可以包含 >2 个元素,例如 The United States of America
)。
这应该有效(如果我理解正确的话):
token_by_index = dict(enumerate(oldTokens))
groups = numberTokenIDs.keys() + locationTokenIDs.keys()
for group in groups:
token_by_index[group[0]] = ''.join(token_by_index.pop(index)
for index in group)
newTokens = [token for _, token in sorted(token_by_index.items(),
key=lambda (index, _): index)]
查找新的令牌 ID:
new_index_by_token = dict(map(lambda (i, t): (t, i), enumerate(newTokens))
numberTokenIDs = {(new_index_by_token[token_by_index[group[0]]],): value
for group, value in numberTokenIDs.items()}
locationTokenIDs = {(new_index_by_token[token_by_index[group[0]]],): value
for group, value in locationTokenIDs.items()}