-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathbot.py
More file actions
167 lines (131 loc) · 5.04 KB
/
bot.py
File metadata and controls
167 lines (131 loc) · 5.04 KB
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
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
import io
import re
import yaml
import chess
import chess.pgn
import chess.engine
from chatterbot import ChatBot
from chatterbot.conversation import Statement
from chatterbot.trainers import ChatterBotCorpusTrainer
from chatterbot.comparisons import LevenshteinDistance
import collections.abc
collections.Hashable = collections.abc.Hashable
def extract_moves(question):
move_pattern = re.compile(r'\b(?:[KQRBN]?[a-h]?[1-8]?x?[a-h][1-8]|O-O(?:-O)?)\b')
moves = re.findall(move_pattern, question)
result_string = ' '.join(moves)
return result_string
def categorize_question(question):
keywords = ["best move", "best next move", "to do next", "optimal move", "recommended move", "strategy", "tactics"]
for keyword in keywords:
if keyword in question.lower():
return True
return
def parse_moves(moves_str):
moves = chess.pgn.read_game(io.StringIO(moves_str)).mainline_moves()
return [move.uci()[:-1] if move.uci().endswith('+') else move.uci() for move in moves]
def count_consecutive_moves_in_file(move1, move2):
total_count = 0
win_count = 0
with open('games.txt', 'r') as file:
games = file.read().split('#')
for game_str in games:
if game_str.strip():
moves = parse_moves(game_str + "#")
for i in range(len(moves) - 1):
if moves[i] == move1 and moves[i + 1] == move2:
total_count += 1
remaining_moves = len(moves) - (i + 2)
if remaining_moves % 2 == 0:
win_count += 1
break
return total_count, win_count
def get_best_move(moves_string):
engine = chess.engine.SimpleEngine.popen_uci("stockfish\stockfish-windows-x86-64-modern.exe")
board = chess.Board()
moves = parse_moves(moves_string)
was_check = False
for move in moves:
board.push(chess.Move.from_uci(move))
print("♟️ Current chess table: ")
print(board)
result = engine.play(board, chess.engine.Limit(time=2.0))
best_move = result.move
if best_move is None:
print("♟️ No best move available.")
return None
engine.quit()
is_in_check = board.is_check()
if is_in_check:
was_check = True
print("\n ♟️ Chess table after taking the best move: ")
board.push(best_move)
print(board)
best_move_uci = best_move.uci()
last_move = moves[-1]
x, y = count_consecutive_moves_in_file(last_move, best_move_uci)
s = 0
if y == 0 or x == 0:
s = 0
else:
s = y/x*100
if board.is_checkmate():
print("\n♟️ This is a checkmate move.")
s = 100
if was_check:
print("\n♟️ The king was in check.")
print("\n♟️ The winning percentage for the next move:", s, "%")
return best_move
def compare_statements(statement):
levenshtein = LevenshteinDistance()
statement1 = Statement(statement)
with open('data.yml', 'r') as f:
data = yaml.safe_load(f)
statements = [Statement(conversation[0]) for conversation in data['conversations']]
max_similarity = 0
most_similar_statement = None
for statement in statements:
similarity = levenshtein.compare(statement1, statement)
if similarity > max_similarity:
max_similarity = similarity
most_similar_statement = statement
return most_similar_statement.text, max_similarity
chatbot = ChatBot("ChessBot")
trainer = ChatterBotCorpusTrainer(chatbot)
trainer.train("chatterbot.corpus.english.ai")
trainer.train("chatterbot.corpus.english.botprofile")
trainer.train("chatterbot.corpus.english.conversations")
trainer.train("chatterbot.corpus.english.greetings")
trainer.train("chatterbot.corpus.english.humor")
trainer.train("chatterbot.corpus.english.trivia")
trainer.train("data")
player_color = "w" # player_color = "b"
exit_conditions = (":q", "quit", "exit")
while True:
request = input("> ")
if request in exit_conditions:
break
moves = extract_moves(request)
if moves:
player_color = "w" if len(moves.split()) % 2 == 0 else "b"
if categorize_question(request):
best_move = get_best_move(moves)
print(f"♟️ Best move in your case: {best_move}")
else:
print ("♟️ I don't understand the question.")
else:
most_similar_statement, max_similarity = compare_statements(request)
# print(f"Cel mai similar statement pentru '{request}' este: '{most_similar_statement}' cu o similaritate de {max_similarity}")
if max_similarity > 0.55:
response = chatbot.get_response(most_similar_statement)
else:
response = chatbot.get_response(request)
print(f"♟️ {response}")
# Comenzi:
# python -m venv venv
# venv\Scripts\activate
#e4 c5 Nf3 Nf6 Bc4 e6 d3
#e4 Nc6 Nf3 a6 Bc4 Na5 Qe2 b5 Bd3
#d4 g6 e4 Bg7 Nf3 d5 e5 e6 Nc3 Ne7 Be2 a6 O-O O-O a3 Bd7 b4 c5 bxc5 b6 cxb6
#next move is checkmate: f2f3 e7e5 g2g4
#the king was in check: e2e4 e7e5 d1h5 g8f6 h5e5