给定词形及其偏移量,找出它在句子中的位置/顺序

2024-09-30 20:35:20 发布

您现在位置:Python中文网/ 问答频道 /正文

我有这样一张单子:

{'content': '@aaaaaaaaaaa People are going to find it hard to believe that Miliband makes it up as he goes along more than Osborne.',
 'entities': [{'named_entity': 'miliband', 'offset': 62}]}

因此,我有一些实体,比如“miliband”,它与内容中的“miliband”(即第一个miliband)匹配(我使用re.compile(r'\b({0})\b')进行匹配),以及起始字母的偏移量。我想找出“Miliband.”在句子中的位置,即“Miliband.”是句子中的第11个单词。你知道吗

有什么我能做到的吗??我想我必须首先标记这个句子,但是我如何利用偏移量来识别正确的匹配词,从而确定它在标记化列表中的位置。。。你知道吗

谢谢!你知道吗

更新: 关于实体词前面的特殊字符/标点符号。e、 例如,“#miliband”和s = 'hello world #miliband'; pos = 13; pos_word = len(s[:62].split()) = 3,应该是2(从0开始),因为“#”。程序有没有办法使用这个偏移量(即13)并确定这个字符在第三个标记化单词“#miliband”的偏移量范围内,因此=2?你知道吗


Tags: to标记positcontentpeople单词are
2条回答

你就快到了,用你已经找到的位置(62)。这就是你的位置,你所需要做的就是数一数你匹配的单词。你知道吗

s="People are going to find it hard to believe Miliband. Miliband makes it up as he goes along more than Osborne.',"

pos = 62

pos_word = len(s[:62].split())

print pos_word

结果是11。像往常一样,您可能需要-1或+1来获得正确的数字,但这应该是可行的。你知道吗

从你的问题来看,我不确定你想达到什么目的。不过,希望这会有所帮助。首先需要拆分句子,然后使用索引查找列表中第一个出现的单词。你知道吗

d = {'content': 'People are going to find it hard to believe Miliband. Miliband makes it up as he goes along more than Osborne.', 'entities': [{'named_entity': 'miliband', 'offset': 62}]}

>>> d['content'].split().index('Miliband')
10

请注意,偏移量62表示第二毫米波段。你知道吗

>>> d['content'][:62]
'People are going to find it hard to believe Miliband. Miliband'

使用字典的偏移量:

>>> len(d['content'][0:d['entities'][0]['offset']].split())
11

相关问题 更多 >