[*] Найдите текстовые блоки, которые одинаковы, на основе # ops (с максимальным размером блока, который определяет начальные максимальные блок, затем поведет в № 1 row в At a Max) < /li>> < / /) < /life life rows per -bloc последовательные дубликаты текстового блока x строки, удалите их и замените дубликаты простыми «... имеет дополнительные x аналогичные записи ...» после начального текстового блока, который имеет дубликаты, следуя им, и заменяет эти дубликаты. Перестроится с 1 пространством для варизон
[*] Любые цифры удаляются (включая «слова, которые начинаются с числа и заканчиваются с нуклером или периодом» - которые удаляют даты, например: xx/xx/xx, запятые/десятичные числа, например: 3,444.22 и точки EG: 1.) из каждых строк
SKIPS с ног (LIS -LI LI LILIP> Сравнение (т.е. игнорируется) < /li>
< /ol>
Результатом будет новый файл TXT, в котором удаляются дубликаты текстовых блоков.
Код: Выделить всё
def clean_for_comparison(line):
# Remove dollar amounts (e.g., $1,234.56) - additional example of cleaning row
line = re.sub(r'\$[\d,]+(?:\.\d+)?', '', line)
# Remove numbers with commas and decimals (e.g., 1,234.56)
line = re.sub(r'[\d,]+(?:\.\d+)?', '', line)
# Remove date-like patterns (e.g., 17/01/2020 or 17-01-2020)
line = re.sub(r'\d{1,2}[-/.\s]?\d{1,2}[-/.\s]?\d{2,4}', '', line)
# Remove words starting and ending with digits (e.g., 123abc456)
line = re.sub(r'\b\d+[a-zA-Z]+\d+\b', '', line)
# Normalize spaces and strip
line = re.sub(r'\s+', ' ', line)
line = line.strip()
return line
< /code>
Я пытаюсь использовать хэш -метод для текстового блока, сравнивая, построив блок с помощью x строки, но у меня возникают проблемы с тем, как он должен работать эффективно. < /p>
Я использую эти вспомогательные функции: < /p>
def get_block_hash(block):
cleaned_lines = [clean_for_comparison(line) for line in block]
non_blank_cleaned_lines = [line for line in cleaned_lines if line]
cleaned_block_content = ''.join(non_blank_cleaned_lines)
return hashlib.md5(cleaned_block_content.encode()).hexdigest()
def get_block(lines, start_index, max_size):
block = []
i = start_index
row_count = 0
while i < len(lines) and row_count < max_size:
line = lines[i].strip()
if line:
block.append(lines[i])
row_count += 1
i += 1
if i >= len(lines):
break
if lines[i - 1].strip() == "" and lines[i].strip() == "":
break
return block, i
Код: Выделить всё
def process_file(file_path, max_consecutive_rows_to_check=4):
with open(file_path, 'r') as file:
lines = file.readlines()
filtered_lines = []
i = 0
removed_count = 0
while i < len(lines):
block, j = get_block(lines, i, max_consecutive_rows_to_check)
if not block: # Skip processing.
i = j # Increment
continue # Skip empty block
# Sliding window comparison
for size in range(min(len(block), max_consecutive_rows_to_check), 0, -1):
block_to_compare = block[:size]
position_row = initial_block_start
while True:
compare_start = position_row +1 # use the next row outside the intial block to compare?
compare_block, next_k = get_block(lines, compare_start, size)
compare_line = compare_block
if len(compare_block) != size:
break # Done with comapre - no more
if get_block_hash(block_to_compare) == get_block_hash(compare_block):
print("DUPE FOUND!")
found_dupes = True
total_dupes += 1 # dupe counter
position_row = compare_start #set next dupe position to compare consecutively with
i += compare_start
#continue #continue if it finds?
else: #Did not find anything
print("Not a dupe.")
break
# If duplicates were found:
if found_dupes:
print(f"Total duplicates found: {total_dupes}")
filtered_lines.extend(block)
filtered_lines.append(f".......\n")
removed_count += total_dupes
i = position_row+max_consecutive_rows_to_check #+1 + size?
# If no duplicates were found:
else:
print("No duplicates found, adding to filtered lines.")
filtered_lines.extend(block)
# Advance to next block
i =+max_consecutive_rows_to_check
# Output to new file
output_filename = file_path
if output_filename.endswith(".txt"):
output_filename = output_filename[:-4]
if not output_filename.endswith("-truncated"):
output_filename = f"{output_filename}-truncated.txt"
with open(output_filename, "w") as output_file:
output_file.writelines(filtered_lines)
print(f"Processed {len(lines)} lines from {file_path}")
print(f"Output saved to {output_filename}")
if removed_count > 0:
print(f"Removed {removed_count} duplicate blocks.")
Любая справка в логическом потоке, эффективности или, если кто-то знает о том, что существует либера>
Подробнее здесь: https://stackoverflow.com/questions/795 ... -filtering