本文整理汇总了Python中sgf.parse方法的典型用法代码示例。如果您正苦于以下问题:Python sgf.parse方法的具体用法?Python sgf.parse怎么用?Python sgf.parse使用的例子?那么, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类sgf
的用法示例。
在下文中一共展示了sgf.parse方法的10个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。
示例1: maybe_correct_next
# 需要导入模块: import sgf [as 别名]
# 或者: from sgf import parse [as 别名]
def maybe_correct_next(pos, next_node):
if (('B' in next_node.properties and not pos.to_play == GOPARAMETERS.BLACK) or
('W' in next_node.properties and not pos.to_play == GOPARAMETERS.WHITE)):
pos.flip_playerturn(mutate=True)
# def replay_sgf(sgf_contents):
# collection = sgf.parse(sgf_contents)
# game = collection.children[0]
# props = game.root.properties
# assert int(sgf_prop(props.get('GM', ['1']))) == 1, "Not a Go SGF!"
#
# komi = 0
# if props.get('KM') != None:
# komi = float(sgf_prop(props.get('KM')))
# result = parse_game_result(sgf_prop(props.get('RE')))
#
# pos = BoardState(komi=komi)
# current_node = game.root
# while pos is not None and current_node.next is not None:
# pos = handle_node(pos, current_node)
# maybe_correct_next(pos, current_node.next)
# next_move = get_next_move(current_node)
# yield PositionWithContext(pos, next_move, result)
# current_node = current_node.next
示例2: get_sgf_props
# 需要导入模块: import sgf [as 别名]
# 或者: from sgf import parse [as 别名]
def get_sgf_props(sgf_path):
with open(sgf_path) as f:
sgf_contents = f.read()
collection = sgf.parse(sgf_contents)
game = collection.children[0]
props = game.root.properties
return props
示例3: parse_sgf
# 需要导入模块: import sgf [as 别名]
# 或者: from sgf import parse [as 别名]
def parse_sgf(sgf_path):
with open(sgf_path) as f:
sgf_contents = f.read()
collection = sgf.parse(sgf_contents)
game = collection.children[0]
props = game.root.properties
assert int(sgf_prop(props.get('GM', ['1']))) == 1, "Not a Go SGF!"
result = utils.parse_game_result(sgf_prop(props.get('RE')))
positions, moves = zip(*[(p.position, p.next_move) for p in sgf_wrapper.replay_sgf(sgf_contents)])
return positions, moves, result, props
示例4: sample_positions_from_games
# 需要导入模块: import sgf [as 别名]
# 或者: from sgf import parse [as 别名]
def sample_positions_from_games(sgf_files, num_positions=1):
pos_data = []
move_data = []
result_data = []
move_idxs = []
fail_count = 0
for i, path in enumerate(tqdm(sgf_files, desc="loading sgfs", unit="games")):
try:
positions, moves, result, props = parse_sgf(path)
except KeyboardInterrupt:
raise
except:
fail_count += 1
continue
#add entire game
if num_positions== -1:
pos_data.extend(positions)
move_data.extend(moves)
move_idxs.extend(range(len(positions)))
result_data.extend([result for i in range(len(positions))])
else:
for idx in np.random.choice(len(positions), num_positions):
pos_data.append(positions[idx])
move_data.append(moves[idx])
result_data.append(result)
move_idxs.append(idx)
print("Sampled {} positions, failed to parse {} files".format(len(pos_data), fail_count))
return pos_data, move_data, result_data, move_idxs
示例5: replay_sgf
# 需要导入模块: import sgf [as 别名]
# 或者: from sgf import parse [as 别名]
def replay_sgf(sgf_contents):
'''
Wrapper for sgf files, returning go.PositionWithContext instances.
It does NOT return the very final position, as there is no follow up.
To get the final position, call pwc.position.play_move(pwc.next_move)
on the last PositionWithContext returned.
Example usage:
with open(filename) as f:
for position_w_context in replay_sgf(f.read()):
print(position_w_context.position)
'''
collection = sgf.parse(sgf_contents)
game = collection.children[0]
props = game.root.properties
assert int(sgf_prop(props.get('GM', ['1']))) == 1, "Not a Go SGF!"
komi = 0
if props.get('KM') != None:
komi = float(sgf_prop(props.get('KM')))
result = utils.parse_game_result(sgf_prop(props.get('RE')))
assert result is not None
pos = Position(komi=komi)
current_node = game.root
while pos is not None and current_node.next is not None:
pos = handle_node(pos, current_node)
maybe_correct_next(pos, current_node.next)
next_move = get_next_move(current_node)
yield PositionWithContext(pos, next_move, result)
current_node = current_node.next
示例6: replay_sgf
# 需要导入模块: import sgf [as 别名]
# 或者: from sgf import parse [as 别名]
def replay_sgf(sgf_contents):
'''
Wrapper for sgf files, exposing contents as position_w_context instances
with open(filename) as f:
for position_w_context in replay_sgf(f.read()):
print(position_w_context.position)
'''
collection = sgf.parse(sgf_contents)
game = collection.children[0]
props = game.root.properties
assert int(sgf_prop(props.get('GM', ['1']))) == 1, "Not a Go SGF!"
komi = 0
if props.get('KM') != None:
komi = float(sgf_prop(props.get('KM')))
metadata = GameMetadata(
result=sgf_prop(props.get('RE')),
handicap=int(sgf_prop(props.get('HA', [0]))),
board_size=int(sgf_prop(props.get('SZ'))))
go.set_board_size(metadata.board_size)
pos = Position(komi=komi)
current_node = game.root
while pos is not None and current_node is not None:
pos = handle_node(pos, current_node)
maybe_correct_next(pos, current_node.next)
next_move = get_next_move(current_node)
yield PositionWithContext(pos, next_move, metadata)
current_node = current_node.next
示例7: replay_sgf
# 需要导入模块: import sgf [as 别名]
# 或者: from sgf import parse [as 别名]
def replay_sgf(board_size, sgf_contents):
"""Wrapper for sgf files.
It does NOT return the very final position, as there is no follow up.
To get the final position, call pwc.position.play_move(pwc.next_move)
on the last PositionWithContext returned.
Example usage:
with open(filename) as f:
for position_w_context in replay_sgf(f.read()):
print(position_w_context.position)
Args:
board_size: the go board size.
sgf_contents: the content in sgf.
Yields:
The go.PositionWithContext instances.
"""
collection = sgf.parse(sgf_contents)
game = collection.children[0]
props = game.root.properties
assert int(sgf_prop(props.get('GM', ['1']))) == 1, 'Not a Go SGF!'
komi = 0
if props.get('KM') is not None:
komi = float(sgf_prop(props.get('KM')))
result = utils.parse_game_result(sgf_prop(props.get('RE')))
pos = Position(board_size, komi=komi)
current_node = game.root
while pos is not None and current_node.next is not None:
pos = handle_node(board_size, pos, current_node)
maybe_correct_next(pos, current_node.next)
next_move = get_next_move(current_node)
yield PositionWithContext(pos, next_move, result)
current_node = current_node.next
示例8: get_sgf_root_node
# 需要导入模块: import sgf [as 别名]
# 或者: from sgf import parse [as 别名]
def get_sgf_root_node(sgf_contents):
collection = sgf.parse(sgf_contents)
game = collection.children[0]
return game.root
示例9: replay_sgf
# 需要导入模块: import sgf [as 别名]
# 或者: from sgf import parse [as 别名]
def replay_sgf(sgf_contents):
'''
Wrapper for sgf files, exposing contents as position_w_context instances
with open(filename) as f:
for position_w_context in replay_sgf(f.read()):
print(position_w_context.position)
'''
collection = sgf.parse(sgf_contents)
game = collection.children[0]
props = game.root.properties
assert int(sgf_prop(props.get('GM', ['1']))) == 1, "Not a Go SGF!"
komi = 0
if props.get('KM') != None:
komi = float(sgf_prop(props.get('KM')))
metadata = GameMetadata(
result=sgf_prop(props.get('RE')),
handicap=int(sgf_prop(props.get('HA', [0]))),
board_size=int(sgf_prop(props.get('SZ', [19]))))
go.set_board_size(metadata.board_size)
pos = Position(komi=komi)
current_node = game.root
while pos is not None and current_node is not None:
pos = handle_node(pos, current_node)
maybe_correct_next(pos, current_node.next)
next_move = get_next_move(current_node)
yield PositionWithContext(pos, next_move, metadata)
current_node = current_node.next
示例10: sgf_iter_states
# 需要导入模块: import sgf [as 别名]
# 或者: from sgf import parse [as 别名]
def sgf_iter_states(sgf_string, include_end=True):
"""Iterates over (GameState, move, player) tuples in the first game of the given SGF file.
Ignores variations - only the main line is returned. The state object is
modified in-place, so don't try to, for example, keep track of it through
time
If include_end is False, the final tuple yielded is the penultimate state,
but the state will still be left in the final position at the end of
iteration because 'gs' is modified in-place the state. See sgf_to_gamestate
"""
collection = sgf.parse(sgf_string)
game = collection[0]
gs = _sgf_init_gamestate(game.root)
if game.rest is not None:
for node in game.rest:
props = node.properties
if 'W' in props:
move = _parse_sgf_move(props['W'][0])
player = go.WHITE
elif 'B' in props:
move = _parse_sgf_move(props['B'][0])
player = go.BLACK
yield (gs, move, player)
# update state to n+1
gs.do_move(move, player)
if include_end:
yield (gs, None, None)