【发布时间】:2013-12-31 17:40:40
【问题描述】:
我有一个包含“键”和“段落”的列表。每个“键”都与一个“段落”相关联。
我的目标是将每个段落分成单独的句子,将每个句子分配给它们最初以段落形式属于的“键”。例如:
(['2925729', 'Patrick came outside and greeted us promptly.'], ['2925729', 'Patrick did not shake our hands nor ask our names. He greeted us promptly and politely, but it seemed routine.'], ['2925728', 'Patrick sucks. He farted politely, but it seemed routine.'])
现在我已经能够编写代码来将句子分成段落,并根据字典获取每个句子的命中数。我现在想为每个问题关联一个 ID。
这是处理没有任何“键”的句子的代码。第 1 步和第 2 步为了节省空间我省略了:
Dictionary = ['book', 'should have', 'open']
####Step3#####
#Create Blank list to append final output
final_out = []
##Find Matches
for sent in sentences:
for sent in sentences:
final_out.append((sent, sum(sent.count(col) for col in dictionary)))
#####Spit out final distinct output
##Output in dictionary structure
final_out = dict(sorted(set(final_out)))
####Get sentences and rank by max first
import operator
sorted_final_out = sorted(final_out.iteritems(),key = operator.itemgetter(1), reverse = True)
由此产生的输出是: (['johny ate the antelope', 80], ['sally has a friend',20]) 等等。然后我选择顶部的 X b 量级。我现在想要实现的是这样的:(['12222','johny ate the antelope', 80], [22332,'sally has a friend',20]).所以我基本上想确保解析出的所有句子都分配给一个“键”。这很复杂对不起。这也是 John 早期解决方案适用于更简单案例的原因。
【问题讨论】:
-
你能发布你正在谈论的代码吗?以及预期的输出。
-
你接受的答案有什么问题?
-
它回答了这个问题,但结果是部分的,数据集更大更复杂。我不想不相信约翰的回答,因为事实上他回答了这个问题。