import random
import os

def generate_matrix():
    return [[random.choice([0, 1]) for _ in range(3)] for _ in range(3)]

def matrix_to_string(matrix):
    return '\n'.join(''.join(str(x) for x in row) for row in matrix)



def parse_matrices_from_file(filename):
    matrices = []
    with open(filename, 'r') as f:
        lines = f.readlines()
    i = 0
    while i < len(lines):
        if 'T[i]' not in lines[i]:
            matrix = [list(map(int, list(lines[j].strip()))) for j in range(i, i+3)]
            matrices.append(matrix)
            i += 4  # 3行矩阵加1行T[i]
        else:
            i += 1
    return matrices

def Check2(filename="ds.txt"):
    C = [
        [1, 0, 1],
        [0, 1, 1],
        [1, 1, 1]
    ]
    matrices = parse_matrices_from_file(filename)
    for index, matrix in enumerate(matrices):
        if matrix_exists(matrix, [C]):
            return index + 1  # +1是因为索引从1开始计数
    return -1

#------------------------

def matrix_exists_in_file(matrix, filename):
    with open(filename, 'r') as f:
        content = f.read()
    matrix_str = matrix_to_string(matrix)
    return matrix_str in content

def matrix_exists(matrix, matrix_list):
    return any(all(matrix[row][col] == m[row][col] for col in range(3) for row in range(3)) for m in matrix_list)

def main():
    if not os.path.exists('ds.txt'):
        with open('ds.txt', 'w') as f:
            pass

    A = []
    count = 0
    
    Check2("ds.txt")

    while count < 514:
        matrix = generate_matrix()

        if matrix_exists(matrix, A):
            count += 1
            continue
        if matrix_exists_in_file(matrix, 'ds.txt'):
            count += 1
            continue
        if Check2()<0:
            count +=1
            continue

        count = 0
        A.append(matrix)
        
        print(matrix_to_string(matrix))
        target = input("请输入对应的target标签:")

        with open('ds.txt', 'a') as f:
            f.write(matrix_to_string(matrix) + '\n')
            f.write(f'T[i]={target}\n')

    print("完成!")

if __name__ == "__main__":
    print(Check2())  # 添加此行以测试Check2函数
    
    main()

数据集的例子如下:



011
110
011
T[i]=c
101
011
111
T[i]=]
100
001
111
T[i]=>
101
110
111
T[i]=C
011
100
101
T[i]=c
101
000
111
T[i]==
011
001
101
T[i]=)
011
000
111
T[i]==
001
111
100
T[i]=+
011
010
010
T[i]=1
001
010
111
T[i]=2
111
001
000
T[i]=7
010
101
011
T[i]=0
010
100
111
T[i]=l
000
111
100
T[i]=2
010
011
101
T[i]=+
111
111
010
T[i]=9
101
100
010
T[i]=1
110
000
110
T[i]==
111
010
000
T[i]=7
010
100
010
T[i]=1
001
101
110
T[i]=j
100
101
101
T[i]=U
010
110
001
T[i]=1
111
010
010
T[i]=7
011
111
001
T[i]=7
001
010
011
T[i]=1
100
111
100
T[i]=+
000
100
011
T[i]=_
110
000
111
T[i]==
101
010
001
T[i]=Y
100
010
001
T[i]=\
000
001
110
T[i]=,
110
110
000
T[i]=@
000
001
010
T[i]=,
011
111
100
T[i]=f
111
110
100
T[i]=【
101
110
101
T[i]=C
100
001
100
T[i]=!
000
011
011
T[i]=。
011
011
101
T[i]=#
110
001
010
T[i]=7
000
110
111
T[i]=z
001
101
011
T[i]=J
011
100
011
T[i]=C
010
111
001
T[i]=+
110
101
011
T[i]=O
110
101
000
T[i]=n
101
010
000
T[i]=v
111
011
110
T[i]=7
000
100
001
T[i]=\
101
011
110
T[i]=Y
111
100
110
T[i]=c
110
111
010
T[i]=+
010
000
110
T[i]==
111
100
100
T[i]=C
101
000
101
T[i]=x
010
010
001
T[i]=1
110
010
100
T[i]=7
001
100
100
T[i]=1
010
010
111
T[i]=┴
10-27 05:11