Is there a .pdf to sgf utility?

General conversations about Go belong here.
Post Reply
Suji
Lives in gote
Posts: 302
Joined: Wed May 19, 2010 2:25 pm
Rank: DDK
GD Posts: 0
KGS: Sujisan 12 kyu
OGS: Sujisan 13 kyu
Has thanked: 70 times
Been thanked: 8 times

Is there a .pdf to sgf utility?

Post by Suji »

I have the free version without solutions of Cho Chikun's life and death problems, and I want to convert them to sgf. They are in pdf format, and I'm wondering if someone has written software that takes a pdf in and spits an sgf file out.

If not, would it be possible to write such a utilty?
My plan to become an SDK is here.
danielm
Dies in gote
Posts: 60
Joined: Sun May 30, 2010 4:12 pm
Rank: KGS 4 kyu
GD Posts: 0
KGS: danielm
Has thanked: 33 times
Been thanked: 16 times

Re: Is there a .pdf to sgf utility?

Post by danielm »

A PDF will just have the problems as images, or (not likely) vector graphics. Analysing those to create SGF files may be theoretically possible, but practically not. :) Unless I am missing something and there is something special about this PDF.

The other way around it would be much easier.
User avatar
tchan001
Gosei
Posts: 1582
Joined: Wed Apr 21, 2010 6:44 pm
GD Posts: 1292
Location: Hong Kong
Has thanked: 54 times
Been thanked: 534 times
Contact:

Re: Is there a .pdf to sgf utility?

Post by tchan001 »

The pdfs are made without answers because the problems are originally from a commercial software which used to be available from Kiseido. There was a freeware program which converted the problems from the software into sgf format.

http://senseis.xmp.net/?EncyclopediaOfLifeAndDeath
http://tchan001.wordpress.com
A blog on Asian go books, go sightings, and interesting tidbits
Go is such a beautiful game.
lorill
Lives with ko
Posts: 281
Joined: Wed Apr 21, 2010 1:03 am
Rank: yes
GD Posts: 0
Location: France
Has thanked: 69 times
Been thanked: 25 times

Re: Is there a .pdf to sgf utility?

Post by lorill »

This specific pdf is even simpler: copy the whole content from your pdf and paste it in a text editor, and you'll see. From that, it's not that hard to convert it to sgf.
kivi
Lives with ko
Posts: 159
Joined: Mon Mar 21, 2011 7:14 am
Rank: EGF 3d
GD Posts: 0
Has thanked: 5 times
Been thanked: 36 times

Re: Is there a .pdf to sgf utility?

Post by kivi »

Since long time I have these pdf's on my android phone and I do occasionally solve a few problems. But it would be more practical if they were sgf files, so that I can keep track of it easier, especially as there are now a few nice apps.

With lorill's finding, it is indeed not too difficult. Actually, if we emailed tasuki, he would probably send the original sgfs. Anyways, here is a simple python script for parsing text files. Feel free to correct or edit for purposes you see fit.

Code: Select all

Usage = """
        Usage: python tasuki2sgf.py textfile1 [textfile2 ...]
        E.g.:  python tasuki2sgf.py cho2.txt cho3.txt gokyo.txt
        It will create seperate sgf files for each problem.

        Explanation on <textfile>s:
        Download tasuki's tsumego pdf's.
        Open using a pdf reader, select all (e.g., Crtl-A), copy (Ctrl-C)
        and then paste to a new text file.
        This script is meant to parse that text file.
        """

import os
import sys

def coord(i):
    return chr(ord('a') + i)

white_char = "!"
black_char = "@"

def dia_to_sgf(problem, prob_id, foldername, comment):
    """ Takes a single problem as a list of lines in tasuki format.
    Writes (overwrites if exists) a sgf file with the name problem_<prob_id>.sgf
    """
    black_indices = []
    white_indices = []
    for x, row_str in enumerate(problem):
        black_indices += [(x, y) for (y, ch) in enumerate(row_str) if ch == black_char]
        white_indices += [(x, y) for (y, ch) in enumerate(row_str) if ch == white_char]
   
    f = open(foldername + "/problem_" + str(prob_id) + ".sgf", "w")
    f.write("(;GM[1]FF[4]SZ[19]")
    if len(comment) > 0:
        f.write("C[" + comment + "]")
    f.write("\nAB")
    for (x, y) in black_indices:
        f.write("[" + coord(y) + coord(x) + "]")
    f.write("\nAW")
    for (x, y) in white_indices:
        f.write("[" + coord(y) + coord(x) + "]")
    f.write("\n)")
    f.close()


def file_to_collection(filename):
    foldername = filename.split(".")[0] + "_folder"
    if not os.path.exists(foldername):
        print "Creating folder:", foldername
        os.makedirs(foldername)
    print "Opening:", filename
    fhandle = open(filename)
    print "Running..."

    # Get past the preamble
    for line in fhandle:
        if line[:6] == "tasuki":
            line = fhandle.next() # pass the date under "tasuki"
            print line.strip()
            break

    problem = []
    for line in fhandle:
        if len(line.strip()) == 0 or (line[0] >= "1" and line[0] <= "9"): # page numbers
            continue
        split_line = line.split()
        if len(split_line) >= 2 and split_line[0] == "problem":
            dia_to_sgf(problem, split_line[1].strip(","), foldername, " ".join(split_line[2:]))
            #print ".",
            problem = []
        else:
            problem.append(line)

    fhandle.close()
    print "done.\n"

def main(file_list):
    if len(file_list) < 1:
        print Usage
    else:
        for filename in file_list:
            file_to_collection(filename)

if __name__ == '__main__':
    main(sys.argv[1:])

Jaafar
Dies in gote
Posts: 23
Joined: Sun Oct 13, 2013 9:09 pm
GD Posts: 0
Has thanked: 28 times
Been thanked: 1 time

Re: Is there a .pdf to sgf utility?

Post by Jaafar »

Thanks a lot for your script kivi, I had to make some small changes for python 3.4.1 on windows (print() and next())

Here is the script that worked for me, it's almost the same

Code: Select all

Usage = """
        Usage: python tasuki2sgf.py textfile1 [textfile2 ...]
        E.g.:  python tasuki2sgf.py cho2.txt cho3.txt gokyo.txt
        It will create seperate sgf files for each problem.

        Explanation on <textfile>s:
        Download tasuki's tsumego pdf's.
        Open using a pdf reader, select all (e.g., Crtl-A), copy (Ctrl-C)
        and then paste to a new text file.
        This script is meant to parse that text file.
        """

import os
import sys

def coord(i):
    return chr(ord('a') + i)

white_char = "!"
black_char = "@"

def dia_to_sgf(problem, prob_id, foldername, comment):
    """ Takes a single problem as a list of lines in tasuki format.
    Writes (overwrites if exists) a sgf file with the name problem_<prob_id>.sgf
    """
    black_indices = []
    white_indices = []
    for x, row_str in enumerate(problem):
        black_indices += [(x, y) for (y, ch) in enumerate(row_str) if ch == black_char]
        white_indices += [(x, y) for (y, ch) in enumerate(row_str) if ch == white_char]
   
    f = open(foldername + "/problem_" + str(prob_id) + ".sgf", "w")
    f.write("(;GM[1]FF[4]SZ[19]")
    if len(comment) > 0:
        f.write("C[" + comment + "]")
    f.write("\nAB")
    for (x, y) in black_indices:
        f.write("[" + coord(y) + coord(x) + "]")
    f.write("\nAW")
    for (x, y) in white_indices:
        f.write("[" + coord(y) + coord(x) + "]")
    f.write("\n)")
    f.close()


def file_to_collection(filename):
    foldername = filename.split(".")[0] + "_folder"
    if not os.path.exists(foldername):
        print ("Creating folder:", foldername)
        os.makedirs(foldername)
    print ("Opening:", filename)
    fhandle = open(filename)
    print ("Running...")

    # Get past the preamble
    for line in fhandle:
        if line[:6] == "tasuki":
            #line = fhandle.next() # pass the date under "tasuki"
            print (line.strip())
            break

    problem = []
    for line in fhandle:
        if len(line.strip()) == 0 or (line[0] >= "1" and line[0] <= "9"): # page numbers
            continue
        split_line = line.split()
        if len(split_line) >= 2 and split_line[0] == "problem":
            dia_to_sgf(problem, split_line[1].strip(","), foldername, " ".join(split_line[2:]))
            #print ".",
            problem = []
        else:
            problem.append(line)

    fhandle.close()
    print ("done.\n")

def main(file_list):
    if len(file_list) < 1:
        print (Usage)
    else:
        for filename in file_list:
            file_to_collection(filename)

if __name__ == '__main__':
    main(sys.argv[1:])
User avatar
RBerenguel
Gosei
Posts: 1585
Joined: Fri Nov 18, 2011 11:44 am
Rank: KGS 5k
GD Posts: 0
KGS: RBerenguel
Tygem: rberenguel
Wbaduk: JohnKeats
Kaya handle: RBerenguel
Online playing schedule: KGS on Saturday I use to be online, but I can be if needed from 20-23 GMT+1
Location: Barcelona, Spain (GMT+1)
Has thanked: 576 times
Been thanked: 298 times
Contact:

Re: Is there a .pdf to sgf utility?

Post by RBerenguel »

A few months ago I found someone had posted them already converted:

https://github.com/MonsieurCactus/scrapeGo

This is what I use now to generate the images for Anki, instead of recompiling Tasuki's TeX sources, which is more labor-intensive (not accounting for the effort taken in tweaking sgftopng.)
Geek of all trades, master of none: the motto for my blog mostlymaths.net
Post Reply