-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathgame.py
151 lines (131 loc) · 4.95 KB
/
game.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
import parser
import random
import spacyTools
import warnings
class Game:
start = "";
end = "";
current = "",
visited = [];
score = 0;
max_iter = 100;
def __init__(self, start, end):
self.start = start
self.end = end
self.current = start
self.visited.append(start)
self.score = 0
self.visited= [start]
def isValid(self):
wikiParser = parser.WikiParser()
is_valid = True
if not wikiParser.pageExists(self.start):
print("the page " + self.start + " doesn't exist")
is_valid = False
if not wikiParser.pageExists(self.end):
print("the page " + self.end + " doesn't exist")
is_valid = False
return is_valid
def randomPlayer(self):
self.__init__(self.start, self.end)
print("start : " + self.start + ", goal : " + self.end)
wikiParser = parser.WikiParser()
while self.current != self.end and self.score < self.max_iter:
page = wikiParser.getPage(self.current)
next_words = wikiParser.getLinksFromPage(page)
if self.end in next_words:
self.current = self.end
self.visited.append(self.current)
self.score += 1
print("I win")
return
else: #select random
r = random.randint(0, len(next_words)-1)
self.current = next_words[r]
self.visited.append(self.current)
self.score += 1
def nlpMeanPlayer(self):
self.__init__(self.start, self.end)
print("Start : " + self.start + ", Goal : " + self.end)
wikiParser = parser.WikiParser()
while self.current != self.end and self.score < self.max_iter:
page = wikiParser.getPage(self.current)
next_words = wikiParser.getLinksFromPage(page)
max_similarity = 0
max_word = ""
# filter to prevent looping
for word in next_words:
if word in self.visited:
next_words.remove(word)
if next_words == []:
print("I am stuck")
return
for word in next_words:
if self.end in next_words:
self.current = self.end
self.visited.append(self.current)
self.score += 1
print("Found page :", self.end)
return
else: #select most similar
with warnings.catch_warnings():
warnings.simplefilter("ignore")
similarity = spacyTools.getMeanSimilarity(word, self.end)
if similarity > max_similarity and not (word in self.visited):
max_similarity = similarity
max_word = word
if(max_word == ""):
print("I am stuck in a page without unvisited links")
return
self.visited.append(max_word)
self.current=max_word
self.score += 1
print("I loose")
def nlpMaxPlayer(self):
self.__init__(self.start, self.end)
print("Start : " + self.start + ", Goal : " + self.end)
wikiParser = parser.WikiParser()
while self.current != self.end and self.score < self.max_iter:
page = wikiParser.getPage(self.current)
next_words = wikiParser.getLinksFromPage(page)
max_similarity = 0
max_word = ""
# filter to prevent looping
for word in next_words:
if word in self.visited:
next_words.remove(word)
if next_words == []:
print("I am stuck")
return
for word in next_words:
if self.end in next_words:
self.current = self.end
self.visited.append(self.current)
self.score += 1
print("Found page :", self.end)
return
else: #select most similar
with warnings.catch_warnings():
warnings.simplefilter("ignore")
similarity = spacyTools.getMaxSimilarity(word, self.end)
if similarity > max_similarity and not (word in self.visited):
max_similarity = similarity
max_word = word
self.visited.append(max_word)
self.current=max_word
self.score += 1
print("I loose")
return
# setters and getters
def set_start(self, name):
self.start = name
def set_end(self, name):
self.end = name
def get_start(self):
return self.start
def get_end(self):
return self.end
def set_max_iter(self, n):
self.max_iter = n
def get_max_iter(self):
return self.max_iter