我有两个列表,我想检查两个列表中每个单词之间的相似性,找出最大的相似度.这是我的代码,
from nltk.corpus import wordnet
list1 = ['Compare', 'require']
list2 = ['choose', 'copy', 'define', 'duplicate', 'find', 'how', 'identify', 'label', 'list', 'listen', 'locate', 'match', 'memorise', 'name', 'observe', 'omit', 'quote', 'read', 'recall', 'recite', 'recognise', 'record', 'relate', 'remember', 'repeat', 'reproduce', 'retell', 'select', 'show', 'spell', 'state', 'tell', 'trace', 'write']
list = []
for word1 in list1:
for word2 in list2:
wordFromList1 = wordnet.synsets(word1)[0]
wordFromList2 = wordnet.synsets(word2)[0]
s = wordFromList1.wup_similarity(wordFromList2)
list.append(s)
print(max(list))
Run Code Online (Sandbox Code Playgroud)
但这会导致错误:
wordFromList2 = wordnet.synsets(word2)[0]
IndexError: list index out of range
Run Code Online (Sandbox Code Playgroud)
请帮我解决这个问题.
感谢您
我需要使用Prolog来解决Ship Puzzle问题.这是事实.
有5艘船.
- 希腊船只在六点离开并运载咖啡.
- 中间的船有一个黑色的烟囱.
- 英国船只在九点发车.
- 带有蓝色烟囱的法国船位于运载咖啡的船的左侧.
- 在运载可可的船的右边是一艘前往马赛的船.
- 巴西船正驶往马尼拉.
- 在运载大米的船旁边是一艘带有绿色烟囱的船.
- 前往热那亚的一艘船在五点离开.
- 这艘西班牙船在七点离开,位于前往马赛的船的右侧.
- 带红色烟囱的船前往汉堡.
- 在七号船离开的旁边是一艘带有白色烟囱的船.
- 边境上的船载着玉米.
- 带有黑色烟囱的船在八点落叶.
- 运载玉米的船停泊在运载大米的船旁边.
- 前往汉堡的船只在六点离开.
哪艘船去了赛义德港?哪艘船载茶?
我搜索互联网寻找答案,但我找不到任何答案.所以我提到'斑马拼图',因此我安排了这个问题的代码.所以这是我的Prolog代码问题.
exists(A,(A,_,_,_,_)).
exists(A,(_,A,_,_,_)).
exists(A,(_,_,A,_,_)).
exists(A,(_,_,_,A,_)).
exists(A,(_,_,_,_,A)).
rightOf(A,B,(B,A,_,_,_)).
rightOf(A,B,(_,B,A,_,_)).
rightOf(A,B,(_,_,B,A,_)).
rightOf(A,B,(_,_,_,B,A)).
middleShip(A,(_,_,A,_,_)).
lastShip(A,(_,_,_,_,A)).
nextTo(A,B,(B,A,_,_,_)).
nextTo(A,B,(_,B,A,_,_)).
nextTo(A,B,(_,_,B,A,_)).
nextTo(A,B,(_,_,_,B,A)).
nextTo(A,B,(A,B,_,_,_)).
nextTo(A,B,(_,A,B,_,_)).
nextTo(A,B,(_,_,A,B,_)).
nextTo(A,B,(_,_,_,A,B)).
solution(PortSaidShip, TeaCarrier) :-
Shipes = (ship(_,_,_,_,_),ship(_,_,_,_,_),ship(_,_,_,_,_),ship(_,_,_,_,_),ship(_,_,_,_,_)),
exists(ship('Greek',6,'Coffee',_,_),Shipes),
middleShip(ship(_,_,_,_,'Black',_),Shipes),
exists(ship('English',9,_,_,_),Shipes),
rightOf(ship(_,_,'Coffee',_,_),ship('French',_,_,'Blue',_),Shipes),
rightOf(ship(_,_,_,_,'Marseille'),ship(_,_,'Cocoa',_,_),Shipes),
exists(ship('Brazilian',_,_,_,'Manila'),Shipes),
nextTo(ship(_,_,_,'Green',_),ship(_,_,'Rice',_,_),Shipes),
exists(ship(_,5,_,_,'Genoa'),Shipes),
rightOf(ship('Spanish',7,_,_,_),ship(_,_,_,_,'Marseille'),Shipes),
exists(ship(_,_,_,'Red','Hamburg'),Shipes),
nextTo(ship(_,_,_,'White',_),ship(_,7,_,_,_),Shipes),
lastShip(ship(_,_,'Corn',_,_),Shipes),
exists(ship(_,8,_,'Black',_),Shipes),
nextTo(ship(_,_,'Corn',_,_),ship(_,_,'Rice',_,_),Shipes),
exists(ship(_,6,_,_,'Hamburg'),Shipes),
exists(ship(PortSaidShip,_,_,_,'Port Said'),Shipes),
exists(ship(TeaCarrier,_,'Tea',_,_),Shipes).
Run Code Online (Sandbox Code Playgroud)
但是当我运行该程序时,它会说"假".
那我怎么解决这个问题呢?
谢谢