플랫 SGF 문자열 구문 분석(패키지 항목 목록 사용)

플랫 SGF 문자열 구문 분석(패키지 항목 목록 사용)

나는SGF패키지와 문자열listofitems. SGF는 텍스트 형식의 트리이지만 지금은 단순성을 위해 단일 분기 SGF에서만 작업하고 있습니다.

내가 하려는 일의 예는 다음과 같습니다.

\documentclass{article}

\usepackage{listofitems}
\usepackage{tikz}

\newcommand{\parseSgf}[1]{
  % From [this answer by @StevenB.Segletes](https://tex.stackexchange.com/a/429895/64441).
  \setsepchar{;}
  \defpair{[]}
  
  \readlist\Z{#1}

  \begin{itemize}
    \foreach \i in {\Z}{
      % TODO: if key is either `B` or `W`:
      \item Color: {\i}[0] and Coords: {\i}[1]
    }
  \end{itemize}
}

\begin{document}
  \def\sgfA{;B[ab];W[cd]}
  \def\sgfB{(;GM[1]FF[4]CA[UTF-8]AP[Sabaki:0.52.2]KM[6.5]SZ[19]DT[2024-02-05];B[as];W[bs];B[cs])}

  \parseSgf{\sgfA}
  \parseSgf{\sgfB}
\end{document}

sgfA적절한 SGF가 아니며 단순한 예입니다. 다음과 같이 순서가 지정되지 않은 목록을 인쇄하고 싶습니다.

  • 색상: B및 좌표:ab
  • 색상: W및 좌표:cd

그러나 나는 사물을 적절하게 사용 \setsepchar하고 \defpair분리하는 방법, 또는 \foreach.

sgfB적절한 SGF 문자열입니다. 그리고 B또는 이 아닌 키를 무시하는 추가적인 문제가 있습니다 W. 누구든지 로 그것을 수행하는 방법을 알고 있다면 listofitems그것은 플러스가 될 것입니다.

답변1

나는 당신 이 주로 구문 B[xy]이 있는 문자열 부분에 관심이 있다는 것을 이해하지 못합니다 . 따라서 아래 코드는 다른 모든 것을 무시합니다.W[xy]xy

이것은 아마도 최종 버전으로 간주되어서는 안 되지만, 이와 같은 것이 가능할 수도 있습니다. 이 경우에는 대부분의 항목이 문자열 분할과 관련되어 있으므로 expl 시퀀스를 사용했습니다(문자열에 적합한 구문이 없으면 "항목 누락" 오류가 나타날 수 있습니다).

\documentclass{article}

\ExplSyntaxOn
\cs_generate_variant:Nn \tl_set:Nn { Ne } % You might need to add this if the version of your TeX installation is not the most recent one 
\cs_generate_variant:Nn \seq_set_split:Nnn { Nne }
\NewDocumentCommand{\parseSgf}{ m }{
  \tl_set:Ne \l_tmpa_tl { #1 }
  % remove ( and ) from string
  \tl_remove_all:Nn \l_tmpa_tl { ( }
  \tl_remove_all:Nn \l_tmpa_tl { ) }
  % store the contents of #1 in a sequence with ; as delimiter
  \seq_set_split:Nne \l_tmpa_seq { ; } { \l_tmpa_tl }
  \begin{itemize}
    % loop over all items in sequence
    \seq_map_inline:Nn \l_tmpa_seq {
      % split string at [ and store result in another sequence
      \seq_set_split:Nne \l_tmpb_seq { [ } { ##1 }
      \bool_if:nT { 
        % test if first part of sequence is B or W
        \str_if_eq_p:ee { \seq_item:Nn \l_tmpb_seq { 1 } } { B } ||
        \str_if_eq_p:ee { \seq_item:Nn \l_tmpb_seq { 1 } } { W } 
      } {
        \tl_set:Ne \l_tmpa_tl { \seq_item:Nn \l_tmpb_seq { 1 } }
        \tl_set:Ne \l_tmpb_tl { \seq_item:Nn \l_tmpb_seq { 2 } }
        % remove ] from string
        \tl_remove_all:Nn \l_tmpb_tl { ] }
        \item Color: ~ \l_tmpa_tl {} ~ and ~ Coords: ~ \l_tmpb_tl
      } 
   }
  \end{itemize}
}
\ExplSyntaxOff

\begin{document}

  \def\sgfA{;B[ab];W[cd]}
  \def\sgfB{(;GM[1]FF[4]CA[UTF-8]AP[Sabaki:0.52.2]KM[6.5]SZ[19]DT[2024-02-05];B[as];W[bs];B[cs])}

  \parseSgf{\sgfA}
  \parseSgf{\sgfB}
  
\end{document}

여기에 이미지 설명을 입력하세요

답변2

여기에 listofitems접근 방식이 있습니다. 그러나 ;B[ab[;W]cd]테스트 사례와 동일한 방식으로 응답하는 와 같은 SGF 문자열의 부적절한 구문은 테스트하지 않습니다 \sgfA.

\documentclass{article}
\usepackage{listofitems,tikz}
\long\def\Firstof#1#2\endFirstof{#1}
\ignoreemptyitems
\newcommand{\parseSgf}[1]{%
  \setsepchar{;/[||]}%
  \readlist*\Z{#1}%
  \begin{itemize}
    \foreachitem \i \in \Z[]{%
      \itemtomacro\Z[\icnt,1]\tmp
      % TODO: if key is either `B` or `W`:
      \expandafter\if\expandafter\Firstof\tmp\endFirstof B
        \item Color: \tmp and Coords: \Z[\icnt,2]
      \else\expandafter\if\expandafter\Firstof\tmp\endFirstof W
        \item Color: \tmp and Coords: \Z[\icnt,2]
      \fi\fi
    }%
  \end{itemize}
}
\begin{document}
  \def\sgfA{;B[ab];W[cd]}
  \def\sgfB{(;GM[1]FF[4]CA[UTF-8]AP[Sabaki:0.52.2]KM[6.5]SZ[19]DT[2024-02-05];B[as];W[bs];B[cs])}
  \parseSgf{\sgfA}\bigskip
  \parseSgf{\sgfB}
\end{document}

여기에 이미지 설명을 입력하세요

관련 정보