Full code!
- User
Full code!
Assistant
Other models, same or similar prompt
Responses were produced in different, uncontrolled contexts (system prompts, settings, prior turns). This is not a benchmark.
Full code!
Responses were produced in different, uncontrolled contexts (system prompts, settings, prior turns). This is not a benchmark.
Okay, here is the full code with the 2-row offset applied when combining the HTML table data and the rect data.
import sys
from bs4 import BeautifulSoup
html_file_path = 'table.html'
tsv_file_path = 'table.tsv'
try:
with open(html_file_path, 'r', encoding='utf-8') as f:
html_content = f.read()
except FileNotFoundError:
print(f"Error: Input file '{html_file_path}' not found.", file=sys.stderr)
sys.exit(1)
except Exception as e:
print(f"Error reading file '{html_file_path}': {e}", file=sys.stderr)
sys.exit(1)
try:
soup = BeautifulSoup(html_content, 'lxml')
except Exception as e:
print(f"Error parsing HTML: {e}", file=sys.stderr)
sys.exit(1)
tsv_lines = []
rects_minus_35_data = []
rects_minus_10_data = []
all_rects = soup.find_all('rect')
if all_rects:
for rect in all_rects:
rect_info = {
'width': rect.get('width', ''),
'x': rect.get('x', '')
}
next_sibling = rect.find_next_sibling('text')
if next_sibling:
text_content = next_sibling.get_text(strip=True)
if text_content == "-35":
rects_minus_35_data.append(rect_info)
elif text_content == "-10":
rects_minus_10_data.append(rect_info)
else:
print("No <rect> elements found to extract data from.", file=sys.stderr)
html_tables = soup.find_all('table')
if html_tables:
tsv_lines.append("# --- Combined Table and Rect Data ---")
for table_idx, table in enumerate(html_tables):
if len(html_tables) > 1:
tsv_lines.append(f"# --- Data from Table {table_idx+1} combined with Rects ---")
html_headers = []
html_rows_data = []
rows = table.find_all('tr')
first_row = True
num_html_cols = 0
for row in rows:
cells = row.find_all(['td', 'th'])
row_data = []
for cell in cells:
cell_text = cell.get_text(separator=" ", strip=True)
cell_text = cell_text.replace('\n', ' ').replace('\r', '').replace('\t', ' ')
row_data.append(cell_text)
if first_row:
if row.find('th'):
html_headers = list(row_data)
num_html_cols = len(html_headers)
else:
html_headers = [''] * len(row_data)
num_html_cols = len(row_data)
html_rows_data.append(row_data)
first_row = False
else:
html_rows_data.append(row_data)
if not html_headers and not first_row:
num_html_cols = max(num_html_cols, len(row_data))
if not html_headers and html_rows_data:
num_html_cols = max(len(r) for r in html_rows_data) if html_rows_data else 0
html_headers = [''] * num_html_cols
combined_headers = list(html_headers)
rect_headers_present = False
if rects_minus_35_data or rects_minus_10_data:
combined_headers.extend(["width-35", "x-35", "width-10", "x-10"])
rect_headers_present = True
if combined_headers:
tsv_lines.append('\t'.join(combined_headers))
max_rows_data = len(html_rows_data)
if rects_minus_35_data:
max_rows_data = max(max_rows_data, len(rects_minus_35_data))
if rects_minus_10_data:
max_rows_data = max(max_rows_data, len(rects_minus_10_data))
total_rows_to_generate = max_rows_data + 2 # Add 2 for the shift
for i in range(total_rows_to_generate):
current_row_parts = []
# HTML table part
if i < len(html_rows_data):
current_row_parts.extend(html_rows_data[i])
current_row_parts.extend([''] * (num_html_cols - len(html_rows_data[i])))
else:
current_row_parts.extend([''] * num_html_cols)
# Rect data part, shifted down by 2 rows
rect_index = i - 2 # Calculate the shifted index
if rect_headers_present: # Only add rect columns if rect headers were added
# Rect -35 part
if rect_index >= 0 and rect_index < len(rects_minus_35_data):
current_row_parts.append(rects_minus_35_data[rect_index]['width'])
current_row_parts.append(rects_minus_35_data[rect_index]['x'])
else:
current_row_parts.extend(['', ''])
# Rect -10 part
if rect_index >= 0 and rect_index < len(rects_minus_10_data):
current_row_parts.append(rects_minus_10_data[rect_index]['width'])
current_row_parts.append(rects_minus_10_data[rect_index]['x'])
else:
current_row_parts.extend(['', ''])
tsv_lines.append('\t'.join(current_row_parts))
tsv_lines.append("")
elif rects_minus_35_data or rects_minus_10_data:
tsv_lines.append("# --- Rect Data Only (No HTML Tables Found) ---")
combined_headers = ["width-35", "x-35", "width-10", "x-10"]
tsv_lines.append('\t'.join(combined_headers))
max_rows_data = 0
if rects_minus_35_data:
max_rows_data = max(max_rows_data, len(rects_minus_35_data))
if rects_minus_10_data:
max_rows_data = max(max_rows_data, len(rects_minus_10_data))
total_rows_to_generate = max_rows_data + 2 # Apply shift even if only rects
for i in range(total_rows_to_generate):
current_row_parts = []
rect_index = i - 2 # Calculate the shifted index
# Rect -35 part
if rect_index >= 0 and rect_index < len(rects_minus_35_data):
current_row_parts.append(rects_minus_35_data[rect_index]['width'])
current_row_parts.append(rects_minus_35_data[rect_index]['x'])
else:
current_row_parts.extend(['', ''])
# Rect -10 part
if rect_index >= 0 and rect_index < len(rects_minus_10_data):
current_row_parts.append(rects_minus_10_data[rect_index]['width'])
current_row_parts.append(rects_minus_10_data[rect_index]['x'])
else:
current_row_parts.extend(['', ''])
tsv_lines.append('\t'.join(current_row_parts))
tsv_lines.append("")
else:
print("No <table> elements found and no <rect> data to process.", file=sys.stderr)
if not tsv_lines:
print("No data found to write to TSV.", file=sys.stderr)
try:
with open(tsv_file_path, 'w', encoding='utf-8') as f:
pass
print(f"Created empty file '{tsv_file_path}'.")
except Exception as e:
print(f"Error creating empty output file: {e}", file=sys.stderr)
sys.exit(1)
else:
while tsv_lines and tsv_lines[-1] == "":
tsv_lines.pop()
try:
with open(tsv_file_path, 'w', encoding='utf-8') as f:
f.write('\n'.join(tsv_lines))
if tsv_lines:
f.write('\n')
print(f"Successfully converted data from '{html_file_path}' to '{tsv_file_path}'.")
except Exception as e:
print(f"Error writing output file '{tsv_file_path}': {e}", file=sys.stderr)
sys.exit(1)