import random import csv def print_matrix(matrix): for row in matrix: print(','.join(map(str, row))) def generate_random_values(num_values, zero_weight=0.5): # Generate random values with 50% chance of being 0 in the first position values = [0] + [random.randint(1, 11) for _ in range(num_values - 1)] return [random.choice([0, val]) for val in values] def randomize_l1(matrix, random_values, identical_positions, combination_positions): # If there are no matches or combination positions, set all numbers to the same value if not identical_positions and not combination_positions: common_random_value = random_values[0] for i in range(len(matrix)): matrix[i][0] = common_random_value else: for i in range(len(matrix)): if (i, i + 1) in identical_positions or (i + 1, i) in identical_positions: # Use the same random value for identical positions matrix[i][0] = random_values[i] matrix[i + 1][0] = random_values[i] elif (i, i) in combination_positions: # Use the same random value for each letter within the combination matrix[i][0] = random_values[i] else: # Use a common random value for non-identical and non-combination positions matrix[i][0] = random_values[0] # Function to find positions of the same letters in the text def find_same_letter_positions(text): same_letter_positions = [] for i in range(len(text)): for j in range(i + 1, len(text)): if text[i] == text[j]: same_letter_positions.append((i, j)) same_letter_positions.append((j, i)) # Include (j, i) as well return same_letter_positions # Function to find positions of the specified combinations in the text def find_combination_positions(text, combinations): combination_positions = [] for combination in combinations: i = 0 while i < len(text) - len(combination) + 1: if text[i:i+len(combination)] == combination: for j in range(i, i+len(combination)): combination_positions.append((j, j)) i += len(combination) # Move to the next position after the combination else: i += 1 return combination_positions # Function to create a matrix from the text def create_matrix_from_text(text): num_rows = len(text) num_columns = num_rows # Initialize a matrix with zeros matrix = [[0] * num_columns for _ in range(num_rows)] # Fill the matrix with data for i in range(num_rows): matrix[i][0] = text[i] # Assign letters to the first column return matrix # Process all text files from text1.csv to text5.csv for i in range(1, 20): csv_filename = f'text{i}.csv' combo_filename = f'text{i}combo2.csv' # Read the text from the CSV file with open(csv_filename, 'r') as file: reader = csv.reader(file) text = next(reader)[0] # Find positions of the same letters same_letter_positions = find_same_letter_positions(text) print(f"\n{text} - Same Letter Positions:", same_letter_positions) # Specify the combinations to find combinations_to_find = ["bl", "wh", "sa", "wo", "no", "ve", "ab", "gro", "pu", "lo", "co", "bus", "pla", "ac", "at", "pr", "fa", "gr", "to", "or", "fa", "fr", "ki", "qu", "cl", "ok", "fig", "run", "ee", "BL", "WH", "SA", "WO", "NO", "VE", "AB", "GRO", "PU", "LO", "CO", "BUS", "PLA", "AC", "AT", "PR", "FA", "GR", "TO", "OR", "FA", "FR", "KI", "QU", "CL", "OK", "FIG", "RUN", "EE"] # Find positions of the specified combinations combination_positions = find_combination_positions(text, combinations_to_find) print(f"{text} - Combination Positions:", combination_positions) # Create a matrix based on the number of letters in the text matrix = create_matrix_from_text(text) # Generate random values for each letter position with 50% chance of being 0 random_values = generate_random_values(len(text), zero_weight=0.5) # Randomly set values for the L1 column randomize_l1(matrix, random_values, same_letter_positions, combination_positions) # Print the matrix after the first modifications print(f"{text} - Matrix After Randomization:") print_matrix(matrix) # Save the matrix to the combo file with open(combo_filename, 'w', newline='') as csvfile: writer = csv.writer(csvfile) for row in matrix: writer.writerow(row)