这出现在另一个问题,但我认为最好问这是一个单独的问题。给句子(100个几千顺序)的大名单:索引文档中单词的最有效方法?
[
"This is sentence 1 as an example",
"This is sentence 1 as another example",
"This is sentence 2",
"This is sentence 3 as another example ",
"This is sentence 4"
]
什么是编写以下功能的最佳方式?
def GetSentences(word1, word2, position):
return ""
,其中给出了两个词,word1
,word2
和位置position
,函数应该返回满足该限制所有语句列表。例如:
GetSentences("sentence", "another", 3)
应该返回句子1
和3
作为句子的指数。我目前的做法是使用字典是这样的:
Index = defaultdict(lambda: defaultdict(lambda: defaultdict(lambda: [])))
for sentenceIndex, sentence in enumerate(sentences):
words = sentence.split()
for index, word in enumerate(words):
for i, word2 in enumerate(words[index:):
Index[word][word2][i+1].append(sentenceIndex)
但这种快速打击一切不成比例的对数据集大小为130 MB作为我的48GB的RAM在不到5分钟耗尽。我以某种方式感觉这是一个常见问题,但无法找到任何有关如何有效解决此问题的参考。有关如何解决这个问题的任何建议?
只是为了澄清:'position'是句子中两个单词之间的距离吗? – misha
@misha:是的。这是正确的。 – Legend
有两个“句子1”令人困惑。它是否匹配第二个“1”而不是第一个? – shookster