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
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
"""
|
| 2 |
+
Scraper for kawmuthu.blogspot.com β Sinhala poems by Kalani Ruwanpathirana
|
| 3 |
+
Run on your local machine or in Kaggle (internet must be ON)
|
| 4 |
+
|
| 5 |
+
Usage:
|
| 6 |
+
pip install requests beautifulsoup4
|
| 7 |
+
python scrape_kawmuthu.py
|
| 8 |
+
|
| 9 |
+
Output:
|
| 10 |
+
kawmuthu_poems_raw.jsonl β raw scraped poems
|
| 11 |
+
kawmuthu_poems_training.jsonl β Alpaca format ready for training
|
| 12 |
+
"""
|
| 13 |
+
|
| 14 |
+
import requests
|
| 15 |
+
from bs4 import BeautifulSoup
|
| 16 |
+
import json
|
| 17 |
+
import re
|
| 18 |
+
import time
|
| 19 |
+
|
| 20 |
+
# ββ Helpers βββββββββββββββββββββββββββββββββββββββββββββββββ
|
| 21 |
+
|
| 22 |
+
def is_sinhala(text):
|
| 23 |
+
sinhala_chars = sum(1 for c in text if '\u0D80' <= c <= '\u0DFF')
|
| 24 |
+
return sinhala_chars > 30
|
| 25 |
+
|
| 26 |
+
def clean_poem(text):
|
| 27 |
+
text = re.sub(r'@[A-Za-z\s\.]+', '', text) # remove @Author
|
| 28 |
+
text = re.sub(r'Β©[A-Za-z\s\.]+', '', text) # remove Β©Author
|
| 29 |
+
text = re.sub(r'-\d{4}\.\d{2}\.\d{2}', '', text) # remove date
|
| 30 |
+
text = re.sub(r'\n{3,}', '\n\n', text) # reduce blank lines
|
| 31 |
+
text = re.sub(r'[ \t]+\n', '\n', text) # trailing spaces
|
| 32 |
+
return text.strip()
|
| 33 |
+
|
| 34 |
+
def get_all_post_urls(base_url):
|
| 35 |
+
"""Crawl all pages and collect post URLs."""
|
| 36 |
+
urls = set()
|
| 37 |
+
page_url = base_url
|
| 38 |
+
page_num = 1
|
| 39 |
+
|
| 40 |
+
while page_url:
|
| 41 |
+
print(f' Page {page_num}: {page_url}')
|
| 42 |
+
try:
|
| 43 |
+
resp = requests.get(page_url, timeout=15)
|
| 44 |
+
soup = BeautifulSoup(resp.text, 'html.parser')
|
| 45 |
+
|
| 46 |
+
for a in soup.find_all('a', href=True):
|
| 47 |
+
href = a['href']
|
| 48 |
+
if 'kawmuthu.blogspot.com/20' in href:
|
| 49 |
+
# strip query params
|
| 50 |
+
href = href.split('?')[0]
|
| 51 |
+
urls.add(href)
|
| 52 |
+
|
| 53 |
+
older = soup.find('a', id='Blog1_blog-pager-older-link')
|
| 54 |
+
page_url = older['href'] if older else None
|
| 55 |
+
page_num += 1
|
| 56 |
+
time.sleep(1)
|
| 57 |
+
|
| 58 |
+
except Exception as e:
|
| 59 |
+
print(f' Error on page {page_num}: {e}')
|
| 60 |
+
break
|
| 61 |
+
|
| 62 |
+
return list(urls)
|
| 63 |
+
|
| 64 |
+
def scrape_post(url):
|
| 65 |
+
"""Scrape a single blog post and return poem dict or None."""
|
| 66 |
+
try:
|
| 67 |
+
resp = requests.get(url, timeout=15)
|
| 68 |
+
soup = BeautifulSoup(resp.text, 'html.parser')
|
| 69 |
+
|
| 70 |
+
# Title
|
| 71 |
+
title_tag = (soup.find('h3', class_='post-title') or
|
| 72 |
+
soup.find('h1', class_='post-title') or
|
| 73 |
+
soup.find('h3', class_='entry-title'))
|
| 74 |
+
title = title_tag.get_text(strip=True) if title_tag else ''
|
| 75 |
+
|
| 76 |
+
# Labels
|
| 77 |
+
labels = [a.get_text(strip=True) for a in soup.find_all('a', rel='tag')]
|
| 78 |
+
|
| 79 |
+
# Body
|
| 80 |
+
body = (soup.find('div', class_='post-body') or
|
| 81 |
+
soup.find('div', class_='entry-content'))
|
| 82 |
+
if not body:
|
| 83 |
+
return None
|
| 84 |
+
|
| 85 |
+
for tag in body.find_all(['img', 'script', 'style']):
|
| 86 |
+
tag.decompose()
|
| 87 |
+
|
| 88 |
+
text = body.get_text('\n')
|
| 89 |
+
text = clean_poem(text)
|
| 90 |
+
|
| 91 |
+
if not is_sinhala(text):
|
| 92 |
+
return None
|
| 93 |
+
if len(text) < 60:
|
| 94 |
+
return None
|
| 95 |
+
|
| 96 |
+
return {
|
| 97 |
+
'title': title,
|
| 98 |
+
'content': text,
|
| 99 |
+
'labels': labels,
|
| 100 |
+
'url': url,
|
| 101 |
+
'author': 'Kalani Ruwanpathirana',
|
| 102 |
+
'source': 'kawmuthu.blogspot.com',
|
| 103 |
+
'type': 'poem',
|
| 104 |
+
}
|
| 105 |
+
except Exception as e:
|
| 106 |
+
print(f' Error: {url} β {e}')
|
| 107 |
+
return None
|
| 108 |
+
|
| 109 |
+
# ββ Training format ββββββββββββββββββββββββββββββββββββββββββ
|
| 110 |
+
|
| 111 |
+
def make_training_examples(poem):
|
| 112 |
+
"""Generate 5 Alpaca training examples per poem."""
|
| 113 |
+
content = poem['content']
|
| 114 |
+
title = poem['title']
|
| 115 |
+
author = poem['author']
|
| 116 |
+
labels = poem['labels']
|
| 117 |
+
|
| 118 |
+
# Detect theme from labels
|
| 119 |
+
theme = labels[0] if labels else 'ΰ·ΰ·ΰΆΰ·ΰΆ½'
|
| 120 |
+
theme_si = {
|
| 121 |
+
'love': 'ΰΆΰΆ―ΰΆ»', 'life': 'ΰΆ’ΰ·ΰ·ΰ·ΰΆ', 'nature': 'ΰ·ΰ·ΰ·ΰΆ·ΰ·ΰ·',
|
| 122 |
+
'baby': 'ΰ·
ΰΆ―ΰΆ»ΰ·', 'night': 'ΰΆ»ΰ·ΰΆΰ·βΰΆ»ΰ·', 'moon': 'ΰ·ΰΆ³',
|
| 123 |
+
'beauty': 'ΰ·ΰ·ΰΆ±ΰ·ΰΆ―ΰΆ»ΰ·ΰΆΊ', 'sadness': 'ΰΆ―ΰ·ΰΆ',
|
| 124 |
+
}.get(theme.lower(), 'ΰ·ΰ·ΰΆΰ·ΰΆ½')
|
| 125 |
+
|
| 126 |
+
TEMPLATE = (
|
| 127 |
+
'Below is an instruction that describes a task. '
|
| 128 |
+
'Write a response that appropriately completes the request.\n\n'
|
| 129 |
+
'### Instruction:\n{instruction}\n\n'
|
| 130 |
+
'### Response:\n{response}'
|
| 131 |
+
)
|
| 132 |
+
|
| 133 |
+
instructions = [
|
| 134 |
+
f'ΰ·ΰ·ΰΆΰ·ΰΆ½ ΰΆΰ·ΰ·ΰΆΊΰΆΰ· ΰΆ½ΰ·ΰΆΊΰΆ±ΰ·ΰΆ±',
|
| 135 |
+
f'Write a Sinhala poem',
|
| 136 |
+
f'{theme_si} ΰΆΰ·ΰΆ± ΰ·ΰ·ΰΆΰ·ΰΆ½ ΰΆΰ·ΰ·ΰΆΊΰΆΰ· ΰΆ½ΰ·ΰΆΊΰΆ±ΰ·ΰΆ±',
|
| 137 |
+
f"'{title}' ΰ·ΰ·ΰΆ±ΰ· ΰ·ΰ·ΰΆΰ·ΰΆ½ ΰΆΰ·ΰ·ΰΆΊΰΆΰ· ΰΆ½ΰ·ΰΆΊΰΆ±ΰ·ΰΆ±",
|
| 138 |
+
f'{author} ΰΆΰ· ΰ·ΰ·ΰΆ½ΰ·ΰΆΊΰ·ΰΆ±ΰ· ΰ·ΰ·ΰΆΰ·ΰΆ½ ΰΆΰ·ΰ·ΰΆΊΰΆΰ· ΰΆ½ΰ·ΰΆΊΰΆ±ΰ·ΰΆ±',
|
| 139 |
+
]
|
| 140 |
+
|
| 141 |
+
examples = []
|
| 142 |
+
for inst in instructions:
|
| 143 |
+
examples.append({
|
| 144 |
+
'text': TEMPLATE.format(instruction=inst, response=content),
|
| 145 |
+
'instruction': inst,
|
| 146 |
+
'output': content,
|
| 147 |
+
'source': poem['source'],
|
| 148 |
+
'type': 'poem',
|
| 149 |
+
'title': title,
|
| 150 |
+
})
|
| 151 |
+
return examples
|
| 152 |
+
|
| 153 |
+
# ββ Main βββββββββββββββββββββββββββββββββββββββββββββββββββββ
|
| 154 |
+
|
| 155 |
+
if __name__ == '__main__':
|
| 156 |
+
BASE = 'https://kawmuthu.blogspot.com/'
|
| 157 |
+
|
| 158 |
+
print('Step 1: Collecting post URLs...')
|
| 159 |
+
urls = get_all_post_urls(BASE)
|
| 160 |
+
print(f'Found {len(urls)} post URLs\n')
|
| 161 |
+
|
| 162 |
+
print('Step 2: Scraping poems...')
|
| 163 |
+
poems = []
|
| 164 |
+
for i, url in enumerate(urls):
|
| 165 |
+
poem = scrape_post(url)
|
| 166 |
+
if poem:
|
| 167 |
+
poems.append(poem)
|
| 168 |
+
print(f' [{i+1}/{len(urls)}] β {poem["title"][:50]}')
|
| 169 |
+
else:
|
| 170 |
+
print(f' [{i+1}/{len(urls)}] - skipped')
|
| 171 |
+
time.sleep(0.5)
|
| 172 |
+
|
| 173 |
+
print(f'\nScraped {len(poems)} valid poems')
|
| 174 |
+
|
| 175 |
+
# Save raw
|
| 176 |
+
with open('kawmuthu_poems_raw.jsonl', 'w', encoding='utf-8') as f:
|
| 177 |
+
for p in poems:
|
| 178 |
+
f.write(json.dumps(p, ensure_ascii=False) + '\n')
|
| 179 |
+
print('Saved kawmuthu_poems_raw.jsonl')
|
| 180 |
+
|
| 181 |
+
# Save training format
|
| 182 |
+
all_examples = []
|
| 183 |
+
for poem in poems:
|
| 184 |
+
all_examples.extend(make_training_examples(poem))
|
| 185 |
+
|
| 186 |
+
with open('kawmuthu_poems_training.jsonl', 'w', encoding='utf-8') as f:
|
| 187 |
+
for ex in all_examples:
|
| 188 |
+
f.write(json.dumps(ex, ensure_ascii=False) + '\n')
|
| 189 |
+
|
| 190 |
+
print(f'Saved kawmuthu_poems_training.jsonl ({len(all_examples)} training examples)')
|
| 191 |
+
print(f'\nDone! Combine with your existing data:')
|
| 192 |
+
print(' cat combined_training.jsonl kawmuthu_poems_training.jsonl > final_training.jsonl')
|