Life In 19x19
http://www.lifein19x19.com/

Is there a .pdf to sgf utility?
http://www.lifein19x19.com/viewtopic.php?f=10&t=4633
Page 1 of 1

Author:  Suji [ Fri Sep 09, 2011 11:20 pm ]
Post subject:  Is there a .pdf to sgf utility?

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?

Author:  danielm [ Sat Sep 10, 2011 4:09 am ]
Post subject:  Re: Is there a .pdf to sgf utility?

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.

Author:  tchan001 [ Sat Sep 10, 2011 5:47 am ]
Post subject:  Re: Is there a .pdf to sgf utility?

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

Author:  lorill [ Sun Sep 11, 2011 2:20 am ]
Post subject:  Re: Is there a .pdf to sgf utility?

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.

Author:  kivi [ Mon Sep 12, 2011 2:15 pm ]
Post subject:  Re: Is there a .pdf to sgf utility?

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:
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:])


Author:  Jaafar [ Mon Jun 09, 2014 4:01 pm ]
Post subject:  Re: Is there a .pdf to sgf utility?

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:
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:])

Author:  RBerenguel [ Mon Jun 09, 2014 4:38 pm ]
Post subject:  Re: Is there a .pdf to sgf utility?

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.)

Page 1 of 1 All times are UTC - 8 hours [ DST ]
Powered by phpBB © 2000, 2002, 2005, 2007 phpBB Group
http://www.phpbb.com/