| import os |
| import re |
| import pandas as pd |
| import spacy |
|
|
| def clean_text(text): |
| if not isinstance(text, str): |
| return "" |
| |
| text = re.sub(r'http\S+|www\S+|https\S+', '', text, flags=re.MULTILINE) |
| |
| text = re.sub(r'<.*?>', '', text) |
| |
| text = re.sub(r'[^A-Za-z\s]', '', text) |
| |
| text = re.sub(r'\s+', ' ', text).strip() |
| return text.lower() |
|
|
| def preprocess_dataset(input_file, output_clean_file, output_tokens_file, nlp): |
| print(f"Processing {input_file}...") |
| df = pd.read_csv(input_file) |
| |
| |
| cols = [c for c in df.columns if c.lower() in ['text', 'extraversion']] |
| df = df[cols] |
| |
| |
| |
| |
| df['bert_text'] = df['text'].apply(lambda x: re.sub(r'http\S+|www\S+|https\S+', '', str(x), flags=re.MULTILINE).strip()) |
| df['clean_text'] = df['text'].apply(clean_text) |
| |
| |
| df = df[df['clean_text'].str.len() > 0] |
| |
| print("Tokenizing and lemmatizing...") |
| |
| tokens_list = [] |
| for doc in nlp.pipe(df['clean_text'], batch_size=256, disable=['parser', 'ner']): |
| tokens = [token.lemma_ for token in doc if not token.is_stop] |
| tokens_list.append(" ".join(tokens)) |
| |
| df['lemmatized_tokens'] = tokens_list |
| |
| |
| bert_df = df[['bert_text', 'extraversion']] if 'extraversion' in df.columns else df[['bert_text']] |
| bert_df.to_csv(output_clean_file, index=False) |
| |
| |
| tokens_df = df[['lemmatized_tokens', 'extraversion']] if 'extraversion' in df.columns else df[['lemmatized_tokens']] |
| tokens_df.to_csv(output_tokens_file, index=False) |
| print(f"Done. Saved to {output_clean_file} and {output_tokens_file}\n") |
|
|
| def main(): |
| data_dir = os.path.join(os.path.dirname(os.path.dirname(os.path.abspath(__file__))), "data") |
| |
| try: |
| nlp = spacy.load("en_core_web_sm") |
| except OSError: |
| from spacy.cli import download |
| download("en_core_web_sm") |
| nlp = spacy.load("en_core_web_sm") |
| |
| for split in ['train', 'validation', 'test']: |
| in_file = os.path.join(data_dir, f"{split}_set.csv") |
| if os.path.exists(in_file): |
| preprocess_dataset( |
| in_file, |
| os.path.join(data_dir, f"{split}_clean.csv"), |
| os.path.join(data_dir, f"{split}_tokens.csv"), |
| nlp |
| ) |
| else: |
| print(f"Skipping {split}, file not found.") |
|
|
| if __name__ == "__main__": |
| main() |
|
|