给定文本中单词的索引,我需要获取字符索引。 例如,在下面的文字:从单词索引获取字符索引
"The cat called other cats."
“猫”字的指数是1 我需要猫即c的第一个字符的索引,这将是4 我不知道如果这是相关的,但我正在使用python-nltk来获取单词。 现在我能想到这样做的唯一方法是:
- Get the first character, find the number of words in this piece of text
- Get the first two characters, find the number of words in this piece of text
- Get the first three characters, find the number of words in this piece of text
Repeat until we get to the required word.
但是,这将是非常低效的。 任何想法将不胜感激。
谢谢你的想法。但是,我不能仅仅在空白处分割文字。我正在使用TreebankWordTokenizer。 – GDev