SabakiHQ / sgf

A library for parsing SGF files.

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

Parse komi correctly with SGF files from Fox Weiqi

kevinsung opened this issue · comments

Fox Weiqi (https://www.foxwq.com/) is a popular Go server. Unfortunately, it populates the komi field (KM) in a non-standard way for downloaded SGF files. An even game will have the komi listed as "375" instead of "7.5," causing Sabaki to parse the komi incorrectly. It could be argued that nothing should be done since Fox Weiqi is not following the SGF standard. However, adding a special case to parse these files would greatly improve Sabaki's usability. KaTrain does this and is able to parse the files. The code it uses is at
https://github.com/sanderland/katrain/blob/485b55c42df4dd1c77abf21eefc23c9a17d6a512/katrain/core/sgf_parser.py#L422-L430

@yishn , are you interested in having someone take this on? I was considering a change for it just now.

My Proposal: I figured that it'd probably be best to make some form of "fox-helper.js" that has fixup code in it.

Why a separate function? This means that by default, sgf's parse functions are still strict in what they accept but users could also call the fixup on a tree of nodes after parsing is completed -- its an attempt to balance strictness vs. usability. I do worry about discoverability though so open to putting it directly in parse.

Why a separate file? If this is going to be its own helper, then this function would work on nodes like stringify does. All the other functions and files deal with different themes -- helper's functions take strings, parse's functions deal with tokens, files, buffers, and iterators.

I did consider following Postel's law and modifying _parseTokens and add the option to fixup fox's KM node as a default option. Open to feedback on this.

Option 1:

const sgf = require('./main')
const path = require('path')

let pathname = path.resolve(__dirname,'../tests/sgf/fox_go_server.sgf')
let rootNodes = sgf.parseFile(pathname)
let changedKomi = sgf.fixupFoxKomi(rootNodes) 
console.log(changedKomi) // true

Option 2:

const sgf = require('./main')
const path = require('path')

let pathname = path.resolve(__dirname,'../tests/sgf/fox_go_server.sgf')
let rootNodes = sgf.parseFile(pathname, {fixupFoxKomi:true)
console.log(rootNodes)  // KM node already adjusted

More I look at it, I'm thinking to handle it like KaTrain does -- inside the parser directly.