# Path: coolisf/common.py
# def read_file(file_path, mode='rt'):
#     file_path = FileHelper.abspath(file_path)  # normalize path
#     if not os.path.isfile(file_path):
#         raise Exception("Input file not found: {}".format(file_path))
#     if file_path.endswith('.gz'):
#         with gzip.open(file_path, mode) as infile:
#             return infile.read()
#     else:
#         return FileHelper.read(file_path, mode)
# 
# def overlap(cfrom1, cto1, cfrom2, cto2):
#     if cfrom1 is None or cto1 is None or cfrom2 is None or cto2 is None:
#         raise ValueError("cfrom:cto must be numbers")
#     return (cfrom1 <= cfrom2 < cto1) or (cfrom2 <= cfrom1 < cto2)


import os
import logging
import json
from collections import defaultdict as dd
from lxml import etree
from lelesk.util import ptpos_to_wn
from chirptext import TextReport, Counter, piter
from chirptext.cli import CLIApp, setup_logging
from chirptext.leutile import StringTool
from chirptext import chio
from chirptext.chio import CSV
from chirptext import texttaglib as ttl
from yawlib import SynsetID
from yawlib.helpers import get_omw, get_wn
from coolisf.common import read_file, overlap
        current = []
        for row in p:
            if row:
                current.append(row)
            if not p.peep() or not p.peep().value:
                if current:
                    groups.append(current)
                    current = []
        print("Found {} sentences.".format(len(groups)))
        if len(groups) != len(doc):
            print("Wrong token files")
            exit()
        for sent, row in zip(doc, groups):
            sent.tokens = [tk[0] for tk in row]
            for token, (text, pos) in zip(sent, row):
                token.text = text
                token.pos = pos
    # output stuff
    if args.output:
        ttl.TxtWriter.from_path(args.output).write_doc(doc)
        print("Written {} sentences to {}".format(len(doc), args.output))
    print("Done")


def corenlp_to_ttl(cli, args):
    print("Core NLP output file: {}".format(args.input))
    print("TTL file: {}".format(args.output))
    print("Source (raw) file: {}".format(args.raw))
    cn_sents = json.loads(chio.read_file(args.input))['sentences']
    print("Found {} core-nlp sents".format(len(cn_sents)))

# Path: coolisf/common.py
# def read_file(file_path, mode='rt'):
#     file_path = FileHelper.abspath(file_path)  # normalize path
#     if not os.path.isfile(file_path):
#         raise Exception("Input file not found: {}".format(file_path))
#     if file_path.endswith('.gz'):
#         with gzip.open(file_path, mode) as infile:
#             return infile.read()
#     else:
#         return FileHelper.read(file_path, mode)
# 
# def overlap(cfrom1, cto1, cfrom2, cto2):
#     if cfrom1 is None or cto1 is None or cfrom2 is None or cto2 is None:
#         raise ValueError("cfrom:cto must be numbers")
#     return (cfrom1 <= cfrom2 < cto1) or (cfrom2 <= cfrom1 < cto2)


# Path: isftk/ttl.py
import os
import logging
import json
from collections import defaultdict as dd
from lxml import etree
from lelesk.util import ptpos_to_wn
from chirptext import TextReport, Counter, piter
from chirptext.cli import CLIApp, setup_logging
from chirptext.leutile import StringTool
from chirptext import chio
from chirptext.chio import CSV
from chirptext import texttaglib as ttl
from yawlib import SynsetID
from yawlib.helpers import get_omw, get_wn
from coolisf.common import read_file, overlap

        current = []
        for row in p:
            if row:
                current.append(row)
            if not p.peep() or not p.peep().value:
                if current:
                    groups.append(current)
                    current = []
        print("Found {} sentences.".format(len(groups)))
        if len(groups) != len(doc):
            print("Wrong token files")
            exit()
        for sent, row in zip(doc, groups):
            sent.tokens = [tk[0] for tk in row]
            for token, (text, pos) in zip(sent, row):
                token.text = text
                token.pos = pos
    # output stuff
    if args.output:
        ttl.TxtWriter.from_path(args.output).write_doc(doc)
        print("Written {} sentences to {}".format(len(doc), args.output))
    print("Done")


def corenlp_to_ttl(cli, args):
    print("Core NLP output file: {}".format(args.input))
    print("TTL file: {}".format(args.output))
    print("Source (raw) file: {}".format(args.raw))
    cn_sents = json.loads(chio.read_file(args.input))['sentences']
    print("Found {} core-nlp sents".format(len(cn_sents)))

    raw_sents = chio.read_file(args.raw).splitlines()