|
| 1 | +{ |
| 2 | + "cells": [ |
| 3 | + { |
| 4 | + "cell_type": "code", |
| 5 | + "execution_count": null, |
| 6 | + "id": "55c5916a-a77f-4497-9123-0f73b4018273", |
| 7 | + "metadata": {}, |
| 8 | + "outputs": [], |
| 9 | + "source": [ |
| 10 | + "from sgnlp.models.coherence_momentum import (\n", |
| 11 | + " CoherenceMomentumModel, \n", |
| 12 | + " CoherenceMomentumConfig, \n", |
| 13 | + " CoherenceMomentumPreprocessor\n", |
| 14 | + ")\n", |
| 15 | + "import pandas as pd\n", |
| 16 | + "\n", |
| 17 | + "# Load CoherenceMomentum model\n", |
| 18 | + "\n", |
| 19 | + "config = CoherenceMomentumConfig.from_pretrained(\n", |
| 20 | + " \"https://storage.googleapis.com/sgnlp-models/models/coherence_momentum/config.json\"\n", |
| 21 | + ")\n", |
| 22 | + "model = CoherenceMomentumModel.from_pretrained(\n", |
| 23 | + " \"https://storage.googleapis.com/sgnlp-models/models/coherence_momentum/pytorch_model.bin\",\n", |
| 24 | + " config=config\n", |
| 25 | + ")\n", |
| 26 | + "preprocessor = CoherenceMomentumPreprocessor(config.model_size, config.max_len)\n", |
| 27 | + "\n", |
| 28 | + "\n", |
| 29 | + "# Function: compute coherence score for a text\n", |
| 30 | + "\n", |
| 31 | + "def get_coherence_score(text):\n", |
| 32 | + " inputs = preprocessor([text])\n", |
| 33 | + " return model.get_main_score(inputs[\"tokenized_texts\"]).item()\n", |
| 34 | + "\n", |
| 35 | + "\n", |
| 36 | + "# Compute full vs segmented coherence for each article\n", |
| 37 | + "\n", |
| 38 | + "results = []\n", |
| 39 | + "for i, (full_text, segments) in enumerate(zip(preprocessed_articles, preprocessed_segments)):\n", |
| 40 | + " reordered_text = \" \".join(segments) # Reconstructed article from segments\n", |
| 41 | + " score_full = get_coherence_score(full_text)\n", |
| 42 | + " score_segments = get_coherence_score(reordered_text)\n", |
| 43 | + " drift = score_full - score_segments \n", |
| 44 | + "\n", |
| 45 | + " results.append({\n", |
| 46 | + " \"Article\": f\"A{i+1}\",\n", |
| 47 | + " \"Coherence Full\": round(score_full, 4),\n", |
| 48 | + " \"Coherence Reordered\": round(score_segments, 4),\n", |
| 49 | + " \"Drift\": round(drift, 4),\n", |
| 50 | + " })\n", |
| 51 | + "\n", |
| 52 | + "\n", |
| 53 | + "# Save per-article results\n", |
| 54 | + "\n", |
| 55 | + "df_coherence = pd.DataFrame(results)\n", |
| 56 | + "df_coherence.to_csv(\"coherence_results.csv\", index=False)\n", |
| 57 | + "\n", |
| 58 | + "\n", |
| 59 | + "# Save summary stats (mean/std)\n", |
| 60 | + "\n", |
| 61 | + "df_summary = df_coherence.drop(\"Article\", axis=1).agg(['mean', 'std']).transpose().reset_index()\n", |
| 62 | + "df_summary.columns = [\"Metric\", \"Mean\", \"Std Dev\"]\n", |
| 63 | + "df_summary.to_csv(\"coherence_summary.csv\", index=False)\n", |
| 64 | + "\n", |
| 65 | + "# Print sample outputs\n", |
| 66 | + "print(df_coherence.head())\n", |
| 67 | + "print(df_summary)\n" |
| 68 | + ] |
| 69 | + } |
| 70 | + ], |
| 71 | + "metadata": { |
| 72 | + "kernelspec": { |
| 73 | + "display_name": "Python 3 (ipykernel)", |
| 74 | + "language": "python", |
| 75 | + "name": "python3" |
| 76 | + }, |
| 77 | + "language_info": { |
| 78 | + "codemirror_mode": { |
| 79 | + "name": "ipython", |
| 80 | + "version": 3 |
| 81 | + }, |
| 82 | + "file_extension": ".py", |
| 83 | + "mimetype": "text/x-python", |
| 84 | + "name": "python", |
| 85 | + "nbconvert_exporter": "python", |
| 86 | + "pygments_lexer": "ipython3", |
| 87 | + "version": "3.10.13" |
| 88 | + } |
| 89 | + }, |
| 90 | + "nbformat": 4, |
| 91 | + "nbformat_minor": 5 |
| 92 | +} |
0 commit comments