我是python新手,我想知道是否有一种通过知道单词的偏移量从文本文件中找到原始句子的有效方法。假设我有一个test.txt文件,如下所示:
test.txt
Ceci est une wheat phrase corn.
Ceci est une deuxième phrase barley.
This is the third wheat word.
假设我知道单词“小麦”的偏移量是[13,18]
我的代码如下所示:
import nltk
from nltk.tokenize import word_tokenize
with open("test.txt") as f:
list_phrase = f.readlines()
f.seek(0)
contents = f.read()
for index, phrase in enumerate(list_phrase):
j = word_tokenize(phrase)
if contents[13:18] in j:
print(list_phrase[index])
我的代码输出将打印两个句子,即(“Ceci est une小麦短语玉米”和“这是第三个小麦单词”)
如何通过知道单词的偏移量来准确地检测单词的真实短语
请注意,我考虑的偏移量在多个句子之间继续(本例中为两个句子)。例如,“大麦”一词的偏移量应为[61,67]
上述印刷品的预期输出应为:
Ceci est une wheat phrase corn.
我们知道它的偏移量是[13,18]
对此,任何帮助都将不胜感激。非常感谢你
如果您已经知道单词的位置,那么标记化不是您想要做的。通过标记化,您可以将序列(您知道其位置)更改为单词列表,其中您不知道哪个元素是您的单词
因此,你应该把它留在短语上,只需将短语的部分与你的单词进行比较:
这将只返回
wheat
位于[13:18]
位置的句子。所有其他小麦品种都不会被发现如果您正在寻找原始速度,那么标准库可能是最好的方法
给定我们要查找的行中的
search_word
及其offset
,我们可以计算用于字符串比较的limit
最简单的方法是迭代文本的枚举行,并对每行执行字符串比较
此解决方案的运行时是在2012 Mac mini(2.3GHz i7 CPU)上运行的。对于处理1000001行来说,这似乎相当快,但可以通过在尝试字符串比较之前检查文本长度来改进
改进解决方案的运行时是
71 ms
在同一台计算机上。这是一个显著的改进,但当然里程数会根据文本文件的不同而有所不同生成的输出:
编辑:包括文件偏移量信息
样本输出:
再来一次
此代码检查文本的已知偏移量是否在当前行开始和行结束的偏移量值之间。在偏移处找到的文本也会得到验证
输出:
相关问题 更多 >
编程相关推荐