當前位置: 首頁>>代碼示例>>Python>>正文


Python sgf.parse方法代碼示例

本文整理匯總了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 
開發者ID:PacktPublishing,項目名稱:Python-Reinforcement-Learning-Projects,代碼行數:27,代碼來源:utils.py

示例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 
開發者ID:mlperf,項目名稱:training_results_v0.5,代碼行數:9,代碼來源:training_curve.py

示例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 
開發者ID:mlperf,項目名稱:training_results_v0.5,代碼行數:15,代碼來源:training_curve.py

示例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 
開發者ID:mlperf,項目名稱:training_results_v0.5,代碼行數:32,代碼來源:training_curve.py

示例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 
開發者ID:mlperf,項目名稱:training_results_v0.5,代碼行數:34,代碼來源:sgf_wrapper.py

示例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 
開發者ID:llSourcell,項目名稱:alphago_demo,代碼行數:31,代碼來源:sgf_wrapper.py

示例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 
開發者ID:itsamitgoel,項目名稱:Gun-Detector,代碼行數:38,代碼來源:sgf_wrapper.py

示例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 
開發者ID:mlperf,項目名稱:training,代碼行數:6,代碼來源:sgf_wrapper.py

示例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 
開發者ID:yhyu13,項目名稱:AlphaGOZero-python-tensorflow,代碼行數:31,代碼來源:sgf_wrapper.py

示例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) 
開發者ID:yhyu13,項目名稱:AlphaGOZero-python-tensorflow,代碼行數:31,代碼來源:util.py


注:本文中的sgf.parse方法示例由純淨天空整理自Github/MSDocs等開源代碼及文檔管理平台,相關代碼片段篩選自各路編程大神貢獻的開源項目,源碼版權歸原作者所有,傳播和使用請參考對應項目的License;未經允許,請勿轉載。