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
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
{
|
2 |
+
"cells": [
|
3 |
+
{
|
4 |
+
"cell_type": "markdown",
|
5 |
+
"metadata": {},
|
6 |
+
"source": [
|
7 |
+
"## Format The dataset"
|
8 |
+
]
|
9 |
+
},
|
10 |
+
{
|
11 |
+
"cell_type": "code",
|
12 |
+
"execution_count": 21,
|
13 |
+
"metadata": {},
|
14 |
+
"outputs": [
|
15 |
+
{
|
16 |
+
"name": "stderr",
|
17 |
+
"output_type": "stream",
|
18 |
+
"text": [
|
19 |
+
"Filtering Content: 100%|██████████| 105218/105218 [00:00<00:00, 125074.19it/s]\n"
|
20 |
+
]
|
21 |
+
}
|
22 |
+
],
|
23 |
+
"source": [
|
24 |
+
"from tqdm import tqdm\n",
|
25 |
+
"import json\n",
|
26 |
+
"import pandas as pd\n",
|
27 |
+
"import random\n",
|
28 |
+
"from datasets import Dataset, DatasetDict\n",
|
29 |
+
"\n",
|
30 |
+
"\n",
|
31 |
+
"with open('/data1/max/telugu_corpus/andhrajyothy_data/andhrajyothy_dataset/all_articles_3months.json', 'r') as f:\n",
|
32 |
+
" data = json.load(f)\n",
|
33 |
+
"\n",
|
34 |
+
"\n",
|
35 |
+
"def analyze_headlines(data):\n",
|
36 |
+
" stories_with_headlines = []\n",
|
37 |
+
" count_with_headlines = 0\n",
|
38 |
+
" count_without_headlines = 0\n",
|
39 |
+
" \n",
|
40 |
+
" for story in tqdm(data, desc='Filtering Content'):\n",
|
41 |
+
" has_headlines = False\n",
|
42 |
+
" story_content = story['content']['StoryContent']\n",
|
43 |
+
" \n",
|
44 |
+
" for content in story_content:\n",
|
45 |
+
" if 'Headlines' in content and content['Headlines']:\n",
|
46 |
+
" has_headlines = True\n",
|
47 |
+
" stories_with_headlines.append({\n",
|
48 |
+
" 'story_id': int(story['story_id']),\n",
|
49 |
+
" 'headlines': content['Headlines'][0].replace('\\u200c', '').replace('\\n', '').replace('</p>', '').replace('<p>', '').replace('<b>', '').replace('</b>', ''),\n",
|
50 |
+
" 'text': content['Body'].replace('\\u200c', '').replace('\\n', '').replace('</p>', '').replace('<p>', '').replace('<b>', '').replace('</b>', ''),\n",
|
51 |
+
" })\n",
|
52 |
+
" \n",
|
53 |
+
" \n",
|
54 |
+
" return stories_with_headlines\n",
|
55 |
+
"filtered_data=analyze_headlines(data)\n"
|
56 |
+
]
|
57 |
+
},
|
58 |
+
{
|
59 |
+
"cell_type": "code",
|
60 |
+
"execution_count": 22,
|
61 |
+
"metadata": {},
|
62 |
+
"outputs": [
|
63 |
+
{
|
64 |
+
"name": "stdout",
|
65 |
+
"output_type": "stream",
|
66 |
+
"text": [
|
67 |
+
"\n",
|
68 |
+
"First example:\n",
|
69 |
+
"{'story_id': 2481002, 'headline': 'బుమ్రాకు విశ్రాంతి!', 'article': 'కుల్దీప్–అక్షర్ మధ్య పోటీ27 నుంచి బంగ్లాతో రెండో టెస్టున్యూఢిల్లీ: వరల్డ్ టెస్టు చాంపియన్షిప్ (డబ్ల్యూటీసీ) 2023–25 అంచెలో భాగంగా టీమిండియా ఆడే పది టెస్టుల్లో మొదటిదాన్ని ఘన విజయంతో ముగించింది. బంగ్లాదేశ్తో జరిగిన ఈ మ్యాచ్లో ఏకంగా 280 పరుగుల తేడాతో నెగ్గి డబ్ల్యూటీసీ టేబుల్లో తమ అగ్రస్థానాన్ని మరింత పదిలపర్చుకుంది. ఇక శుక్రవారం నుంచి కాన్పూర్లో చివరిదైన రెండో టెస్టు జరుగుతుంది. వాస్తవానికి ఈ పది టెస్టుల్లో రోహిత్ సేన సగం మ్యాచ్లను గెలిచినా ఫైనల్లో చోటు ఖాయమే. బంగ్లాతో రెండో టెస్టు తర్వాత స్వదేశంలో కివీస్తో మూడు టెస్టులు ఉంటాయి. ఆ తర్వాత ఐదు టెస్టుల సిరీస్ కోసం భారత జట్టు ఆస్ట్రేలియా టూర్కు వెళ్లాల్సి ఉంటుంది. అయితే అక్కడ ఆత్మవిశ్వాసంతో సిరీస్ను ఆరంభించేందుకు, సొంత గడ్డపై మిగిలిన మూడు టెస్టులను గెలిచి ఒత్తిడి తగ్గించుకోవాలనుకుంటోంది.కాన్పూర్ పిచ్ను అనుసరించి ముగ్గురు స్పిన్నర్లతో వెళ్లాలనుకుంటే జట్టుకు ఓ పేసర్ దూరం కావాల్సిందే. ఒకవేళ బుమ్రాకు రెస్ట్ ఇస్తే ఇబ్బంది లేదు. కానీ తను ఆడితే మరో పేసర్ ఆకాశ్ దీప్పై వేటు పడే చాన్సుంది. ఇక మూడో స్పిన్నర్ కోసం తుది జట్టులో కుల్దీప్, అక్షర్ల మధ్య పోటీ ఏర్పడనుంది. ఇంగ్లండ్త�� జరిగిన సిరీస్లో కుల్దీప్ 19 వికెట్లు తీయడంతో కోచ్ గంభీర్ అతడి వైపే మొగ్గు చూపవచ్చు. మరోవైపు జట్టులో ఇప్పటికే అశ్విన్, జడేజా రూపంలో ఇద్దరు స్పిన్ ఆల్రౌండర్లు ఉన్నారు కాబట్టి అక్షర్ బ్యాటింగ్ సామర్థ్యం పెద్దగా అవసరపడదు.ఎర్ర మట్టితో తయారుచేసిన చెన్నై పిచ్పై బంతి బాగా బౌన్స్ కావడంతో పేసర్లు ఎక్కువగా లాభపడ్డారు. నాలుగో రోజు మాత్రమే స్పిన్కు అనుకూలించింది. అయితే కాన్పూర్లో మాత్రం నల్ల మట్టి పిచ్ సిద్ధంగా ఉండనుంది. ఇక్కడ బంతి తక్కువగా బౌన్స్ అవడంతో పాటు స్పిన్కు అనుకూలిస్తూనే ఫ్లాట్గా ఉండనుంది. దీంతో బ్యాటర్లు, స్పిన్నర్లకు ఇద్దరికీ లాభమే. అలాగే దీనికి తగ్గట్టుగా ఇరు జట్లలోనూ మార్పులు చోటు చేసుకోనున్నాయి. '}\n",
|
70 |
+
"Length of the train dataset 83866\n"
|
71 |
+
]
|
72 |
+
}
|
73 |
+
],
|
74 |
+
"source": [
|
75 |
+
"\n",
|
76 |
+
"\n",
|
77 |
+
"def create_huggingface_dataset(corpus_data):\n",
|
78 |
+
" \"\"\"\n",
|
79 |
+
" Create a HuggingFace Dataset from corpus data with combined headlines and text\n",
|
80 |
+
" \n",
|
81 |
+
" Args:\n",
|
82 |
+
" corpus_data (list): List of dictionaries containing story_id, headlines, and text\n",
|
83 |
+
" \n",
|
84 |
+
" Returns:\n",
|
85 |
+
" datasets.Dataset: HuggingFace Dataset object with combined content\n",
|
86 |
+
" \"\"\"\n",
|
87 |
+
" # Create a new list with combined content\n",
|
88 |
+
" processed_data = []\n",
|
89 |
+
" for item in corpus_data:\n",
|
90 |
+
" processed_data.append({\n",
|
91 |
+
" 'story_id': item['story_id'],\n",
|
92 |
+
" 'headline': item['headlines'],\n",
|
93 |
+
" 'article': item['text']\n",
|
94 |
+
" #'text': f\"{item['headlines']}\\n\\n{item['text']}\" # Combining headlines and text with double newline\n",
|
95 |
+
" })\n",
|
96 |
+
" \n",
|
97 |
+
" # Convert to DataFrame\n",
|
98 |
+
" df = pd.DataFrame(processed_data)\n",
|
99 |
+
" \n",
|
100 |
+
" # Create HuggingFace Dataset\n",
|
101 |
+
" dataset = Dataset.from_pandas(df)\n",
|
102 |
+
" \n",
|
103 |
+
" return dataset\n",
|
104 |
+
"\n",
|
105 |
+
"\n",
|
106 |
+
"\n",
|
107 |
+
"# Create the dataset\n",
|
108 |
+
"train_dataset = create_huggingface_dataset(filtered_data)\n",
|
109 |
+
"# Print first example to show combined content\n",
|
110 |
+
"print(\"\\nFirst example:\")\n",
|
111 |
+
"print(train_dataset[0])\n",
|
112 |
+
"\n",
|
113 |
+
"print('Length of the train dataset',len(train_dataset))\n",
|
114 |
+
"\n",
|
115 |
+
"\n",
|
116 |
+
"# # Save the dataset (optional)\n",
|
117 |
+
"# dataset.save_to_disk(\"telugu_news_dataset\")"
|
118 |
+
]
|
119 |
+
},
|
120 |
+
{
|
121 |
+
"cell_type": "markdown",
|
122 |
+
"metadata": {},
|
123 |
+
"source": [
|
124 |
+
"## Append the TeClass dataset as validation set"
|
125 |
+
]
|
126 |
+
},
|
127 |
+
{
|
128 |
+
"cell_type": "markdown",
|
129 |
+
"metadata": {},
|
130 |
+
"source": [
|
131 |
+
"We are only considering HREL content for validation of current dataset"
|
132 |
+
]
|
133 |
+
},
|
134 |
+
{
|
135 |
+
"cell_type": "code",
|
136 |
+
"execution_count": 23,
|
137 |
+
"metadata": {},
|
138 |
+
"outputs": [],
|
139 |
+
"source": [
|
140 |
+
"\n",
|
141 |
+
"te_df = pd.read_csv('/data1/max/telugu_corpus/TeClass_training_data.csv')\n",
|
142 |
+
"filtered_df = te_df[te_df['label'] == 'HREL']"
|
143 |
+
]
|
144 |
+
},
|
145 |
+
{
|
146 |
+
"cell_type": "code",
|
147 |
+
"execution_count": 27,
|
148 |
+
"metadata": {},
|
149 |
+
"outputs": [
|
150 |
+
{
|
151 |
+
"name": "stdout",
|
152 |
+
"output_type": "stream",
|
153 |
+
"text": [
|
154 |
+
"\n",
|
155 |
+
"First val example:\n",
|
156 |
+
"{'story_id': 920427, 'headline': 'శేఖర్ కమ్ముల తండ్రి శేషయ్య కన్నుమూత', 'article': 'ప్రముఖ దర్శకులు శేఖర్ కమ్ముల ఇంట విషాదం నెలకొంది . ఆయన తండ్రి కమ్ముల శేషయ్య ( 89 ) కన్నుమూశారు . ఆయన గత కొద్దికాలంగా వృద్దాప్య సంబంధింత సమస్యలతో బాధపడుతున్నారు . ప్రస్తుత పరిస్థితుల్లో కొద్దికాలంగా ఇంటి వద్దే చికిత్స అందిస్తున్నారు . ఇటీవల ఆరోగ్యం క్షీణించడంతో స్థానిక ప్రైవేట్ హాస్పిటల్లో చ���ర్పించారు . అక్కడే చికిత్స పొందుతూ శనివారం ఉదయం 6 గంటలకు మరణించారు అని సన్నిహితులు తెలిపారు . శనివారం సాయంత్రం బన్సీలాల్ పేట స్మశాన వాటికలో అంత్యక్రియలు జరుగుతాయని కుటుంబ సభ్యులు తెలిపారు . శేఖర్ కమ్ముల లాక్డౌన్ సమయంలో కరోనా వారియర్స్తో మాట్లాడుతూ ప్లాస్మా దానం చేయాలని అవగాహన కల్పిస్తున్నారు . ఇక సినిమాల విషయానికి వస్తే .. నాగచైతన్య , సాయిపల్లవి హీరోహీరోయిన్లుగా ‘లవ్స్టోరి’ సినిమాను తెరకెక్కిస్తున్నారు శేఖర్ కమ్ముల . ఈ సినిమా షూటింగ్ తుది దశకు చేరుకుంది . సినీ రంగానికి చెందిన పలువురు సోషల్ మీడియా ద్వారా సంతాపం వ్యక్తం చేశారు .'}\n"
|
157 |
+
]
|
158 |
+
}
|
159 |
+
],
|
160 |
+
"source": [
|
161 |
+
"\n",
|
162 |
+
"\n",
|
163 |
+
"def create_huggingface_dataset(filtered_df):\n",
|
164 |
+
" \"\"\"\n",
|
165 |
+
" Create a HuggingFace Dataset from corpus data with combined headlines and text\n",
|
166 |
+
" \n",
|
167 |
+
" Args:\n",
|
168 |
+
" filtered_df (data frame)): dataframe for valset\n",
|
169 |
+
" \n",
|
170 |
+
" Returns:\n",
|
171 |
+
" datasets.Dataset: HuggingFace Dataset object with combined content\n",
|
172 |
+
" \"\"\"\n",
|
173 |
+
" # Create a new list with combined content\n",
|
174 |
+
" processed_data = []\n",
|
175 |
+
" for i,item in filtered_df.iterrows():\n",
|
176 |
+
" processed_data.append({\n",
|
177 |
+
" 'story_id': random.randint(100000,999999),\n",
|
178 |
+
" 'headline': item['headline'],\n",
|
179 |
+
" 'article': item['article'] # Combining headlines and text with double newline\n",
|
180 |
+
" })\n",
|
181 |
+
" \n",
|
182 |
+
" # Convert to DataFrame\n",
|
183 |
+
" df = pd.DataFrame(processed_data)\n",
|
184 |
+
" \n",
|
185 |
+
" # Create HuggingFace Dataset\n",
|
186 |
+
" dataset = Dataset.from_pandas(df)\n",
|
187 |
+
" \n",
|
188 |
+
" return dataset\n",
|
189 |
+
"\n",
|
190 |
+
"\n",
|
191 |
+
"\n",
|
192 |
+
"# Create the dataset\n",
|
193 |
+
"val_dataset = create_huggingface_dataset(filtered_df)\n",
|
194 |
+
"# Print first example to show combined content\n",
|
195 |
+
"print(\"\\nFirst val example:\")\n",
|
196 |
+
"print(val_dataset[0])"
|
197 |
+
]
|
198 |
+
},
|
199 |
+
{
|
200 |
+
"cell_type": "code",
|
201 |
+
"execution_count": 28,
|
202 |
+
"metadata": {},
|
203 |
+
"outputs": [],
|
204 |
+
"source": [
|
205 |
+
"\n",
|
206 |
+
"dataset = DatasetDict({'train': train_dataset, 'validation': val_dataset})"
|
207 |
+
]
|
208 |
+
},
|
209 |
+
{
|
210 |
+
"cell_type": "markdown",
|
211 |
+
"metadata": {},
|
212 |
+
"source": [
|
213 |
+
"Push to the hub for future use"
|
214 |
+
]
|
215 |
+
},
|
216 |
+
{
|
217 |
+
"cell_type": "code",
|
218 |
+
"execution_count": 32,
|
219 |
+
"metadata": {},
|
220 |
+
"outputs": [
|
221 |
+
{
|
222 |
+
"name": "stderr",
|
223 |
+
"output_type": "stream",
|
224 |
+
"text": [
|
225 |
+
"Creating parquet from Arrow format: 100%|██████████| 84/84 [00:00<00:00, 360.53ba/s]\n",
|
226 |
+
"Uploading the dataset shards: 100%|██████████| 1/1 [00:09<00:00, 9.44s/it]\n",
|
227 |
+
"Creating parquet from Arrow format: 100%|██████████| 6/6 [00:00<00:00, 116.88ba/s]\n",
|
228 |
+
"Uploading the dataset shards: 100%|██████████| 1/1 [00:03<00:00, 3.18s/it]\n"
|
229 |
+
]
|
230 |
+
},
|
231 |
+
{
|
232 |
+
"data": {
|
233 |
+
"text/plain": [
|
234 |
+
"CommitInfo(commit_url='https://huggingface.co/datasets/saidines12/telugu_news_dataset/commit/724a92685e7e0a2959cee47d6c7f8b8a74573d59', commit_message='Upload dataset', commit_description='', oid='724a92685e7e0a2959cee47d6c7f8b8a74573d59', pr_url=None, pr_revision=None, pr_num=None)"
|
235 |
+
]
|
236 |
+
},
|
237 |
+
"execution_count": 32,
|
238 |
+
"metadata": {},
|
239 |
+
"output_type": "execute_result"
|
240 |
+
}
|
241 |
+
],
|
242 |
+
"source": [
|
243 |
+
"import os\n",
|
244 |
+
"\n",
|
245 |
+
"\n",
|
246 |
+
"\n",
|
247 |
+
"dataset.push_to_hub(\"saidines12/telugu_news_dataset\")"
|
248 |
+
]
|
249 |
+
}
|
250 |
+
],
|
251 |
+
"metadata": {
|
252 |
+
"kernelspec": {
|
253 |
+
"display_name": "myenv",
|
254 |
+
"language": "python",
|
255 |
+
"name": "python3"
|
256 |
+
},
|
257 |
+
"language_info": {
|
258 |
+
"codemirror_mode": {
|
259 |
+
"name": "ipython",
|
260 |
+
"version": 3
|
261 |
+
},
|
262 |
+
"file_extension": ".py",
|
263 |
+
"mimetype": "text/x-python",
|
264 |
+
"name": "python",
|
265 |
+
"nbconvert_exporter": "python",
|
266 |
+
"pygments_lexer": "ipython3",
|
267 |
+
"version": "3.9.19"
|
268 |
+
}
|
269 |
+
},
|
270 |
+
"nbformat": 4,
|
271 |
+
"nbformat_minor": 2
|
272 |
+
}
|