zakerytclarke commited on
Commit
553c3d9
·
verified ·
1 Parent(s): 0c71346

Create transform.py

Browse files
Files changed (1) hide show
  1. transform.py +60 -0
transform.py ADDED
@@ -0,0 +1,60 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ from datasets import load_dataset
2
+ import pandas as pd
3
+ import re
4
+ import json
5
+
6
+ # Load SQuAD dataset from Hugging Face
7
+ dataset = load_dataset('squad')
8
+
9
+ # Define a function to find the previous sentence-ending punctuation before a given index
10
+ def find_previous_punctuation(text, start_index):
11
+ # Regex pattern for sentence-ending punctuation
12
+ punctuation_pattern = r"[.?!;]"
13
+ # Find all matching punctuation positions
14
+ matches = list(re.finditer(punctuation_pattern, text[:start_index]))
15
+ if matches:
16
+ # Return the position just after the last match before start_index
17
+ return matches[-1].end()
18
+ return 0 # Return the start of the string if no punctuation is found
19
+
20
+ # Define a function to find the next sentence-ending punctuation after a given index
21
+ def find_next_punctuation(text, start_index):
22
+ # Regex pattern for sentence-ending punctuation
23
+ punctuation_pattern = r"[.?!;]"
24
+ # Search for the next punctuation after start_index
25
+ match = re.search(punctuation_pattern, text[start_index:])
26
+ if match:
27
+ # Return the index relative to the original string
28
+ return start_index + match.end()
29
+ return len(text) # Return end of the string if no punctuation is found
30
+
31
+ # Extract the specific row's context from the dataset
32
+ def get_row_context(row):
33
+ # Get the starting index of the answer
34
+ answer_idx = row.get('answers').get('answer_start')[0] # Assuming the first answer
35
+ # Find the previous sentence-ending punctuation before the answer
36
+ start_idx = find_previous_punctuation(row['context'], answer_idx)
37
+ # Find the next sentence-ending punctuation after the answer
38
+ end_idx = find_next_punctuation(row['context'], answer_idx + len(row.get('answers').get('text')[0]))
39
+ # Return the substring of context containing the answer and its surrounding context
40
+ return row['context'][start_idx:end_idx].strip()
41
+
42
+ # Function to join and process the dataset into a Pandas DataFrame
43
+ def join_squad_dataset(dataset):
44
+ # Convert the dataset into a Pandas DataFrame
45
+ df = pd.DataFrame(dataset) # Use 'train' split
46
+ # Apply `get_row_context` to generate the context sentence
47
+ df['context'] = df.apply(get_row_context, axis=1)
48
+ df['answer'] = df['answers'].apply(lambda x: x['text'][0])
49
+ # Return the processed DataFrame with required columns
50
+ return df[['context', 'question', 'answer']]
51
+
52
+ # Process the dataset
53
+ train_df = join_squad_dataset(dataset['train'])
54
+ test_df = join_squad_dataset(dataset['validation'])
55
+
56
+ with open("train.json", "w", encoding="utf-8") as f:
57
+ json.dump(train_df.to_dict(orient='records'), f, indent=4, ensure_ascii=False)
58
+
59
+ with open("validation.json", "w", encoding="utf-8") as f:
60
+ json.dump(test_df.to_dict(orient='records'), f, indent=4, ensure_ascii=False)