我必须在课文中寻找概念。这些概念的表达方式如下:
"blue 5 house"
>>>意味着我必须找到单词所在的位置
blue
和
house
出现在一个
distance of 5 or less words
.
"little 3 cat"
那就意味着找到单词所在的位置
little
和
cat
出现在一个
distance of max 3 words
. (即“小猫咪”、“小烦人的猫咪”而不是“我奶奶的猫咪很小”)。
我想你明白了。
到目前为止,我有一个(不是很复杂的)代码,如下所示。我刚刚实现了两个嵌套循环,遍历文本中的所有单词,当第一个循环命中时,开始在单词周围查找另一个循环,并将结果添加到列表中:
with open('applicationtext.txt', 'r') as f:
content=f.read()
# content = ' Lorem ipsum dolor sit amet, consectetur (23) adipiscing elit, sed do ( 23 , 45 ) eiusmod ( 23, 45 ) tempor incididunt ut '
# Note: the text contains several times: "sit amet eros vestibulum"
elasticTerm1="sit"
elasticTerm2="vestibulum"
distance=5
content=content.strip()
# replace all the line breaks and two spaces.
content = content.replace('\n', ' ').replace('\r', '').replace(' ',' ')
listofHits=[]
content_tokenized = content.split(" ")
for i,word in enumerate(content_tokenized):
if word==elasticTerm1:
for j in range(distance):
if content_tokenized[i+j]==elasticTerm2:
# I got a hit
position1=i
myhitTupple=(i,elasticTerm1)
listofHits.append(myhitTupple)
for i,tupple in enumerate(listofHits):
print(tupple)
到目前为止还可以。
想象一下,我正在考虑如何在此基础上进行构建,以便以递归方式构造代码,从而获得以下结果:
(little 3 cat) 4 third_word
甚至
concept1 5 concept2
;在哪里
concept1=("blue 3 cat")
和
concept2=("little 4 dollar")
????
我该怎么想?一个班?这已经包含在Scikit学习中了吗?不仅仅是一个代码(我想这会很复杂),我还要求你确定方向。如何思考用代码递归解决的问题。
谢谢
注1:请忘记“小猫咪”和“小猫咪”的顺序,这是另一个问题。
注2:(在第一次回答之后)请注意,这是一个非常简单的案例,实际上,我正在研究这样的案例:
((concept1 n1 concept2) n2 concept 3)) n3 (concept1 n4 concept 5)