Tomer Sagi commited on
Commit ·
bbdd49e
1
Parent(s): bbe548d
added unittest
Browse files- scrape_script.py +2 -4
- test_scraper.py +54 -0
scrape_script.py
CHANGED
|
@@ -31,8 +31,7 @@ def process_file(file_path):
|
|
| 31 |
append_to_parquet(content, file_path, "he", top_level_directory)
|
| 32 |
|
| 33 |
|
| 34 |
-
def append_to_parquet(content, file_path, lang, top_level_directory):
|
| 35 |
-
data_dir = "data"
|
| 36 |
if not os.path.exists(data_dir):
|
| 37 |
os.makedirs(data_dir)
|
| 38 |
|
|
@@ -66,8 +65,7 @@ def append_to_parquet(content, file_path, lang, top_level_directory):
|
|
| 66 |
# Remove chapter markers
|
| 67 |
chapter_markers = ['Chapter', 'Halakhah']
|
| 68 |
for marker in chapter_markers:
|
| 69 |
-
content = re.sub(rf'^{marker} \d+$', '', content, flags=re.MULTILINE)
|
| 70 |
-
|
| 71 |
|
| 72 |
metadata = {"file": file_path}
|
| 73 |
meta_json = json.dumps(metadata)
|
|
|
|
| 31 |
append_to_parquet(content, file_path, "he", top_level_directory)
|
| 32 |
|
| 33 |
|
| 34 |
+
def append_to_parquet(content, file_path, lang, top_level_directory, data_dir = "data"):
|
|
|
|
| 35 |
if not os.path.exists(data_dir):
|
| 36 |
os.makedirs(data_dir)
|
| 37 |
|
|
|
|
| 65 |
# Remove chapter markers
|
| 66 |
chapter_markers = ['Chapter', 'Halakhah']
|
| 67 |
for marker in chapter_markers:
|
| 68 |
+
content = re.sub(rf'^{marker} +\d+$', '', content, flags=re.MULTILINE)
|
|
|
|
| 69 |
|
| 70 |
metadata = {"file": file_path}
|
| 71 |
meta_json = json.dumps(metadata)
|
test_scraper.py
ADDED
|
@@ -0,0 +1,54 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import unittest
|
| 2 |
+
import os
|
| 3 |
+
import shutil
|
| 4 |
+
from scrape_script import append_to_parquet
|
| 5 |
+
import pandas as pd
|
| 6 |
+
|
| 7 |
+
class TestAppendToParquet(unittest.TestCase):
|
| 8 |
+
def setUp(self):
|
| 9 |
+
self.temp_dir = "temp_data"
|
| 10 |
+
if not os.path.exists(self.temp_dir):
|
| 11 |
+
os.makedirs(self.temp_dir)
|
| 12 |
+
|
| 13 |
+
def tearDown(self):
|
| 14 |
+
shutil.rmtree(self.temp_dir)
|
| 15 |
+
|
| 16 |
+
def test_cleaning_rules(self):
|
| 17 |
+
content = (
|
| 18 |
+
"<span>Test text with HTML tags.</span> "
|
| 19 |
+
"https://example.com\n"
|
| 20 |
+
"Chapter 1\n"
|
| 21 |
+
"Halakhah 1\n"
|
| 22 |
+
"<b>bold text</b> <strong>strong text</strong> <small>small text</small>"
|
| 23 |
+
)
|
| 24 |
+
file_path = "sample_file.txt"
|
| 25 |
+
lang = "en"
|
| 26 |
+
top_level_directory = "test"
|
| 27 |
+
|
| 28 |
+
append_to_parquet(content, file_path, lang, top_level_directory, data_dir=self.temp_dir)
|
| 29 |
+
|
| 30 |
+
parquet_file = os.path.join(self.temp_dir, f"train_{top_level_directory}_english.parquet")
|
| 31 |
+
self.assertTrue(os.path.exists(parquet_file))
|
| 32 |
+
|
| 33 |
+
df = pd.read_parquet(parquet_file)
|
| 34 |
+
self.assertEqual(len(df), 1)
|
| 35 |
+
|
| 36 |
+
cleaned_content = df.loc[0, "text"]
|
| 37 |
+
expected_cleaned_content = (
|
| 38 |
+
"Test text with HTML tags. "
|
| 39 |
+
"\n"
|
| 40 |
+
"bold text strong text small text"
|
| 41 |
+
)
|
| 42 |
+
cleaned_content_lines = [line.strip() for line in cleaned_content.split("\n") if line.strip()]
|
| 43 |
+
expected_cleaned_content_lines = [line.strip() for line in expected_cleaned_content.split("\n") if line.strip()]
|
| 44 |
+
|
| 45 |
+
self.assertEqual(len(cleaned_content_lines), len(expected_cleaned_content_lines))
|
| 46 |
+
|
| 47 |
+
for clean_line, expected_line in zip(cleaned_content_lines, expected_cleaned_content_lines):
|
| 48 |
+
self.assertEqual(clean_line, expected_line)
|
| 49 |
+
|
| 50 |
+
|
| 51 |
+
|
| 52 |
+
if __name__ == "__main__":
|
| 53 |
+
unittest.main()
|
| 54 |
+
|