Update chess_ground-targz.py
Browse files- chess_ground-targz.py +13 -5
chess_ground-targz.py
CHANGED
@@ -59,7 +59,7 @@ class ChessImageTextDataset(datasets.GeneratorBasedBuilder):
|
|
59 |
]
|
60 |
|
61 |
def _generate_examples(self, image_tar_path, text_filepath):
|
62 |
-
"""Generate examples by linking images with multiple related texts."""
|
63 |
idx = 0
|
64 |
# Extract and map text IDs to their corresponding images
|
65 |
image_mapping = self._extract_images_from_tar(image_tar_path)
|
@@ -85,11 +85,14 @@ class ChessImageTextDataset(datasets.GeneratorBasedBuilder):
|
|
85 |
|
86 |
# Ensure the image exists and yield the example
|
87 |
if image_file:
|
88 |
-
#
|
89 |
-
|
|
|
|
|
|
|
90 |
yield idx, {
|
91 |
"image": image_file,
|
92 |
-
"text": combined_text, # Link all related
|
93 |
}
|
94 |
idx += 1
|
95 |
else:
|
@@ -97,7 +100,6 @@ class ChessImageTextDataset(datasets.GeneratorBasedBuilder):
|
|
97 |
|
98 |
def _extract_images_from_tar(self, tar_path):
|
99 |
"""Extracts the images from the tar.gz archive and returns a mapping of image filenames to file paths."""
|
100 |
-
|
101 |
image_mapping = {}
|
102 |
extraction_directory = "images_extracted" # Temporary directory to store extracted images
|
103 |
os.makedirs(extraction_directory, exist_ok=True)
|
@@ -118,3 +120,9 @@ class ChessImageTextDataset(datasets.GeneratorBasedBuilder):
|
|
118 |
image_mapping[image_filename] = extracted_image_path
|
119 |
|
120 |
return image_mapping
|
|
|
|
|
|
|
|
|
|
|
|
|
|
59 |
]
|
60 |
|
61 |
def _generate_examples(self, image_tar_path, text_filepath):
|
62 |
+
"""Generate examples by linking images with multiple related texts and clean up the text data."""
|
63 |
idx = 0
|
64 |
# Extract and map text IDs to their corresponding images
|
65 |
image_mapping = self._extract_images_from_tar(image_tar_path)
|
|
|
85 |
|
86 |
# Ensure the image exists and yield the example
|
87 |
if image_file:
|
88 |
+
# Clean the text to keep only chess notation
|
89 |
+
cleaned_texts = [self._extract_chess_notation(text) for text in texts]
|
90 |
+
|
91 |
+
# Join the cleaned notations related to the same image
|
92 |
+
combined_text = " ".join(cleaned_texts)
|
93 |
yield idx, {
|
94 |
"image": image_file,
|
95 |
+
"text": combined_text, # Link all related cleaned notations together
|
96 |
}
|
97 |
idx += 1
|
98 |
else:
|
|
|
100 |
|
101 |
def _extract_images_from_tar(self, tar_path):
|
102 |
"""Extracts the images from the tar.gz archive and returns a mapping of image filenames to file paths."""
|
|
|
103 |
image_mapping = {}
|
104 |
extraction_directory = "images_extracted" # Temporary directory to store extracted images
|
105 |
os.makedirs(extraction_directory, exist_ok=True)
|
|
|
120 |
image_mapping[image_filename] = extracted_image_path
|
121 |
|
122 |
return image_mapping
|
123 |
+
|
124 |
+
def _extract_chess_notation(self, text):
|
125 |
+
"""Extracts the chess notation from the full text string."""
|
126 |
+
# Assuming the chess notation comes after the filename and space, e.g., '001_0_30_white.png Rxc6'
|
127 |
+
notation = text.split(" ", 1)[-1] # Extract everything after the first space
|
128 |
+
return notation.strip()
|