Beispiel #1
0
def run(x, y, z, num_lines=0, max_retry=1, basename=None):
    """
    指定されたサイズ、線数の問題データと正解データを自動生成して、ファイルbasename*.txtに書き出す。
    @param x,y,z 盤のサイズ
    @param num_lines 線の本数
    @param basename 出力先ファイル名。問題ファイルはbasename_adc.txt、正解ファイルはbasename_adc_sol.txtになる。
    """
    Q = {'size': (x, y, z)}
    num_lines, ban = generate(x,
                              y,
                              z,
                              num_lines=num_lines,
                              max_retry=max_retry,
                              Q_data=Q)
    Q['line_num'] = num_lines
    Q['empty_cells'] = ban.empty_cells()
    print('number of lines:', Q['line_num'])
    print('number of empty cells:', Q['empty_cells'])
    #if verbose: ban.print()
    #if verbose: print('Q=', Q)
    txtQ = Q_text(Q)
    txtA = ban.A_data()

    # nlcheckする
    nlc = NLCheck()
    q = nlc.read_input_str(txtQ)
    a = nlc.read_target_str(txtA)
    #nlc.verbose = verbose
    judges = nlc.check(q, a)
    print("judges = ", judges)

    # 描画する
    nldraw2.setup_font('nonexistent')  # あとで考える
    images = nldraw2.draw(q, a, nlc)
    for num, img in enumerate(images):
        ifile = "%s.%d.gif" % (basename, num + 1)  # 層の番号は1から始まる
        img.save(ifile, 'gif')
        print(ifile)
    if 1 < len(images):
        nldraw2.merge_images(images).save(basename + '.gif', 'gif')

    # QとAを出力する
    if basename is None:
        print(txtQ)
        print(txtA)
    else:
        qfile = '%s_adc.txt' % basename
        with open(qfile, 'w') as f:
            f.write(txtQ)
        afile = '%s_adc_sol.txt' % basename
        with open(afile, 'w') as f:
            f.write(txtA)
        excel(ban, basename)
Beispiel #2
0
def check_A_data(a_text, q_text):
    """回答データのチェックをする"""
    nlc = NLCheck()
    #nlc.debug = True
    #print "Q=",q_text
    #print "A=",a_text
    out = io.BytesIO() # io.StringIO()だとTypeError: unicode argument expected, got 'str'
    #out = open("/tmp/nlcheck_log.txt","w")
    #--------------------------------------
    sys.stdout = out # 標準出力を付け替える
    input_data  = nlc.read_input_str(q_text)  # 問題データ
    target_data = nlc.read_target_str(a_text) # 回答データ
    nlc.verbose = True
    judges = nlc.check(input_data, target_data)
    print "judges = ", judges
    #results = escape(out.getvalue()) # from xml.sax.saxutils import escape, unescape ここでは不要か?!
    msg = out.getvalue()
    out.close()
    sys.stdout = sys.__stdout__ # もとに戻す
    #--------------------------------------
    return judges, msg
Beispiel #3
0
def check_A_data(a_str, q_uni):
    """回答データのチェックをする"""
    # 問題データ q_uni (unicode)
    # 回答データ a_str (str)
    nlc = NLCheck()
    #nlc.debug = True
    q_str = q_uni.encode('utf-8')  # unicode -> str
    #print "Q=",q_str
    #print "A=",a_str
    #--------------------------------------
    out = io.BytesIO(
    )  # io.StringIO()だとTypeError: unicode argument expected, got 'str'
    sys.stdout = out  # 標準出力を付け替える
    input_data = nlc.read_input_str(q_str)
    target_data = nlc.read_target_str(a_str)
    nlc.verbose = True
    judges = nlc.check(input_data, target_data)
    print "judges = ", judges
    #results = escape(out.getvalue()) # from xml.sax.saxutils import escape, unescape ここでは不要か?!
    msg = out.getvalue()
    out.close()
    sys.stdout = sys.__stdout__  # もとに戻す
    #--------------------------------------
    return judges, msg
Beispiel #4
0
def read_input_data(text):
    nlc = NLCheck()
    ok = True
    msg = ""
    #--------------------------------------
    out = io.BytesIO(
    )  # io.StringIO()だとTypeError: unicode argument expected, got 'str'
    backup_stdout = sys.stdout
    sys.stdout = out  # 標準出力を付け替える
    ret = None
    try:
        ret = nlc.read_input_str(text)
    except:
        ok = False
        ret = (None, None, None, None, None)
    size, line_num, line_mat, via_mat, via_dic = ret
    msg = out.getvalue()
    out.close()
    sys.stdout = backup_stdout  # もとに戻す
    #--------------------------------------
    if "ERROR" in msg:
        ok = False
        print msg
    return (size, line_num, line_mat, via_mat, via_dic, msg, ok)