It is currently Wed May 07, 2025 9:28 am

All times are UTC - 8 hours [ DST ]




Post new topic Reply to topic  [ 7 posts ] 
Author Message
Offline
 Post subject: Is there a .pdf to sgf utility?
Post #1 Posted: Fri Sep 09, 2011 11:20 pm 
Lives in gote

Posts: 302
Liked others: 70
Was liked: 8
Rank: DDK
KGS: Sujisan 12 kyu
OGS: Sujisan 13 kyu
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.

Top
 Profile  
 
Offline
 Post subject: Re: Is there a .pdf to sgf utility?
Post #2 Posted: Sat Sep 10, 2011 4:09 am 
Dies in gote

Posts: 60
Liked others: 33
Was liked: 16
Rank: KGS 4 kyu
KGS: 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.

Top
 Profile  
 
Offline
 Post subject: Re: Is there a .pdf to sgf utility?
Post #3 Posted: Sat Sep 10, 2011 5:47 am 
Gosei
User avatar

Posts: 1582
Location: Hong Kong
Liked others: 54
Was liked: 544
GD Posts: 1292
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.

Top
 Profile  
 
Offline
 Post subject: Re: Is there a .pdf to sgf utility?
Post #4 Posted: Sun Sep 11, 2011 2:20 am 
Lives with ko

Posts: 281
Location: France
Liked others: 69
Was liked: 25
Rank: yes
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.


This post by lorill was liked by: snorri
Top
 Profile  
 
Offline
 Post subject: Re: Is there a .pdf to sgf utility?
Post #5 Posted: Mon Sep 12, 2011 2:15 pm 
Lives with ko

Posts: 159
Liked others: 5
Was liked: 36
Rank: EGF 3d
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:])



This post by kivi was liked by 2 people: Jaafar, snorri
Top
 Profile  
 
Offline
 Post subject: Re: Is there a .pdf to sgf utility?
Post #6 Posted: Mon Jun 09, 2014 4:01 pm 
Dies in gote

Posts: 23
Liked others: 28
Was liked: 1
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:])

Top
 Profile  
 
Offline
 Post subject: Re: Is there a .pdf to sgf utility?
Post #7 Posted: Mon Jun 09, 2014 4:38 pm 
Gosei
User avatar

Posts: 1585
Location: Barcelona, Spain (GMT+1)
Liked others: 577
Was liked: 298
Rank: KGS 5k
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
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

Top
 Profile  
 
Display posts from previous:  Sort by  
Post new topic Reply to topic  [ 7 posts ] 

All times are UTC - 8 hours [ DST ]


Who is online

Users browsing this forum: No registered users and 1 guest


You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot post attachments in this forum

Search for:
Jump to:  
Powered by phpBB © 2000, 2002, 2005, 2007 phpBB Group