anytp commited on
Commit
e6d8570
·
verified ·
1 Parent(s): adf5fad

Upload 3 files

Browse files
Files changed (3) hide show
  1. Fine_Tuning_NER.ipynb +0 -0
  2. Flair_BERT.ipynb +1351 -0
  3. Prueba_P_Flair.ipynb +1660 -0
Fine_Tuning_NER.ipynb ADDED
The diff for this file is too large to render. See raw diff
 
Flair_BERT.ipynb ADDED
@@ -0,0 +1,1351 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ {
2
+ "nbformat": 4,
3
+ "nbformat_minor": 0,
4
+ "metadata": {
5
+ "colab": {
6
+ "provenance": [],
7
+ "gpuType": "T4"
8
+ },
9
+ "kernelspec": {
10
+ "name": "python3",
11
+ "display_name": "Python 3"
12
+ },
13
+ "language_info": {
14
+ "name": "python"
15
+ },
16
+ "accelerator": "GPU"
17
+ },
18
+ "cells": [
19
+ {
20
+ "cell_type": "code",
21
+ "source": [
22
+ "!pip install flair"
23
+ ],
24
+ "metadata": {
25
+ "colab": {
26
+ "base_uri": "https://localhost:8080/"
27
+ },
28
+ "id": "80-pc-7cM_ZK",
29
+ "outputId": "4317872a-09e6-4a17-8fc1-34981da99bdc"
30
+ },
31
+ "execution_count": null,
32
+ "outputs": [
33
+ {
34
+ "output_type": "stream",
35
+ "name": "stdout",
36
+ "text": [
37
+ "Requirement already satisfied: flair in /usr/local/lib/python3.10/dist-packages (0.12.2)\n",
38
+ "Requirement already satisfied: python-dateutil>=2.6.1 in /usr/local/lib/python3.10/dist-packages (from flair) (2.8.2)\n",
39
+ "Requirement already satisfied: torch!=1.8,>=1.5.0 in /usr/local/lib/python3.10/dist-packages (from flair) (2.0.0)\n",
40
+ "Requirement already satisfied: gensim>=3.8.0 in /usr/local/lib/python3.10/dist-packages (from flair) (4.3.2)\n",
41
+ "Requirement already satisfied: tqdm>=4.26.0 in /usr/local/lib/python3.10/dist-packages (from flair) (4.66.1)\n",
42
+ "Requirement already satisfied: segtok>=1.5.7 in /usr/local/lib/python3.10/dist-packages (from flair) (1.5.11)\n",
43
+ "Requirement already satisfied: matplotlib>=2.2.3 in /usr/local/lib/python3.10/dist-packages (from flair) (3.7.1)\n",
44
+ "Requirement already satisfied: mpld3==0.3 in /usr/local/lib/python3.10/dist-packages (from flair) (0.3)\n",
45
+ "Requirement already satisfied: scikit-learn>=0.21.3 in /usr/local/lib/python3.10/dist-packages (from flair) (1.2.2)\n",
46
+ "Requirement already satisfied: sqlitedict>=1.6.0 in /usr/local/lib/python3.10/dist-packages (from flair) (2.1.0)\n",
47
+ "Requirement already satisfied: deprecated>=1.2.4 in /usr/local/lib/python3.10/dist-packages (from flair) (1.2.14)\n",
48
+ "Requirement already satisfied: hyperopt>=0.2.7 in /usr/local/lib/python3.10/dist-packages (from flair) (0.2.7)\n",
49
+ "Requirement already satisfied: boto3 in /usr/local/lib/python3.10/dist-packages (from flair) (1.28.51)\n",
50
+ "Requirement already satisfied: transformers[sentencepiece]>=4.18.0 in /usr/local/lib/python3.10/dist-packages (from flair) (4.33.2)\n",
51
+ "Requirement already satisfied: bpemb>=0.3.2 in /usr/local/lib/python3.10/dist-packages (from flair) (0.3.4)\n",
52
+ "Requirement already satisfied: regex in /usr/local/lib/python3.10/dist-packages (from flair) (2023.6.3)\n",
53
+ "Requirement already satisfied: tabulate in /usr/local/lib/python3.10/dist-packages (from flair) (0.9.0)\n",
54
+ "Requirement already satisfied: langdetect in /usr/local/lib/python3.10/dist-packages (from flair) (1.0.9)\n",
55
+ "Requirement already satisfied: lxml in /usr/local/lib/python3.10/dist-packages (from flair) (4.9.3)\n",
56
+ "Requirement already satisfied: ftfy in /usr/local/lib/python3.10/dist-packages (from flair) (6.1.1)\n",
57
+ "Requirement already satisfied: janome in /usr/local/lib/python3.10/dist-packages (from flair) (0.5.0)\n",
58
+ "Requirement already satisfied: gdown==4.4.0 in /usr/local/lib/python3.10/dist-packages (from flair) (4.4.0)\n",
59
+ "Requirement already satisfied: huggingface-hub>=0.10.0 in /usr/local/lib/python3.10/dist-packages (from flair) (0.17.2)\n",
60
+ "Requirement already satisfied: conllu>=4.0 in /usr/local/lib/python3.10/dist-packages (from flair) (4.5.3)\n",
61
+ "Requirement already satisfied: more-itertools in /usr/local/lib/python3.10/dist-packages (from flair) (10.1.0)\n",
62
+ "Requirement already satisfied: wikipedia-api in /usr/local/lib/python3.10/dist-packages (from flair) (0.6.0)\n",
63
+ "Requirement already satisfied: pptree in /usr/local/lib/python3.10/dist-packages (from flair) (3.1)\n",
64
+ "Requirement already satisfied: pytorch-revgrad in /usr/local/lib/python3.10/dist-packages (from flair) (0.2.0)\n",
65
+ "Requirement already satisfied: transformer-smaller-training-vocab>=0.2.1 in /usr/local/lib/python3.10/dist-packages (from flair) (0.3.2)\n",
66
+ "Requirement already satisfied: filelock in /usr/local/lib/python3.10/dist-packages (from gdown==4.4.0->flair) (3.12.2)\n",
67
+ "Requirement already satisfied: requests[socks] in /usr/local/lib/python3.10/dist-packages (from gdown==4.4.0->flair) (2.31.0)\n",
68
+ "Requirement already satisfied: six in /usr/local/lib/python3.10/dist-packages (from gdown==4.4.0->flair) (1.16.0)\n",
69
+ "Requirement already satisfied: beautifulsoup4 in /usr/local/lib/python3.10/dist-packages (from gdown==4.4.0->flair) (4.11.2)\n",
70
+ "Requirement already satisfied: numpy in /usr/local/lib/python3.10/dist-packages (from bpemb>=0.3.2->flair) (1.23.5)\n",
71
+ "Requirement already satisfied: sentencepiece in /usr/local/lib/python3.10/dist-packages (from bpemb>=0.3.2->flair) (0.1.99)\n",
72
+ "Requirement already satisfied: wrapt<2,>=1.10 in /usr/local/lib/python3.10/dist-packages (from deprecated>=1.2.4->flair) (1.15.0)\n",
73
+ "Requirement already satisfied: scipy>=1.7.0 in /usr/local/lib/python3.10/dist-packages (from gensim>=3.8.0->flair) (1.11.2)\n",
74
+ "Requirement already satisfied: smart-open>=1.8.1 in /usr/local/lib/python3.10/dist-packages (from gensim>=3.8.0->flair) (6.4.0)\n",
75
+ "Requirement already satisfied: fsspec in /usr/local/lib/python3.10/dist-packages (from huggingface-hub>=0.10.0->flair) (2023.6.0)\n",
76
+ "Requirement already satisfied: pyyaml>=5.1 in /usr/local/lib/python3.10/dist-packages (from huggingface-hub>=0.10.0->flair) (6.0.1)\n",
77
+ "Requirement already satisfied: typing-extensions>=3.7.4.3 in /usr/local/lib/python3.10/dist-packages (from huggingface-hub>=0.10.0->flair) (4.5.0)\n",
78
+ "Requirement already satisfied: packaging>=20.9 in /usr/local/lib/python3.10/dist-packages (from huggingface-hub>=0.10.0->flair) (23.1)\n",
79
+ "Requirement already satisfied: networkx>=2.2 in /usr/local/lib/python3.10/dist-packages (from hyperopt>=0.2.7->flair) (3.1)\n",
80
+ "Requirement already satisfied: future in /usr/local/lib/python3.10/dist-packages (from hyperopt>=0.2.7->flair) (0.18.3)\n",
81
+ "Requirement already satisfied: cloudpickle in /usr/local/lib/python3.10/dist-packages (from hyperopt>=0.2.7->flair) (2.2.1)\n",
82
+ "Requirement already satisfied: py4j in /usr/local/lib/python3.10/dist-packages (from hyperopt>=0.2.7->flair) (0.10.9.7)\n",
83
+ "Requirement already satisfied: contourpy>=1.0.1 in /usr/local/lib/python3.10/dist-packages (from matplotlib>=2.2.3->flair) (1.1.0)\n",
84
+ "Requirement already satisfied: cycler>=0.10 in /usr/local/lib/python3.10/dist-packages (from matplotlib>=2.2.3->flair) (0.11.0)\n",
85
+ "Requirement already satisfied: fonttools>=4.22.0 in /usr/local/lib/python3.10/dist-packages (from matplotlib>=2.2.3->flair) (4.42.1)\n",
86
+ "Requirement already satisfied: kiwisolver>=1.0.1 in /usr/local/lib/python3.10/dist-packages (from matplotlib>=2.2.3->flair) (1.4.5)\n",
87
+ "Requirement already satisfied: pillow>=6.2.0 in /usr/local/lib/python3.10/dist-packages (from matplotlib>=2.2.3->flair) (9.4.0)\n",
88
+ "Requirement already satisfied: pyparsing>=2.3.1 in /usr/local/lib/python3.10/dist-packages (from matplotlib>=2.2.3->flair) (3.1.1)\n",
89
+ "Requirement already satisfied: joblib>=1.1.1 in /usr/local/lib/python3.10/dist-packages (from scikit-learn>=0.21.3->flair) (1.3.2)\n",
90
+ "Requirement already satisfied: threadpoolctl>=2.0.0 in /usr/local/lib/python3.10/dist-packages (from scikit-learn>=0.21.3->flair) (3.2.0)\n",
91
+ "Requirement already satisfied: sympy in /usr/local/lib/python3.10/dist-packages (from torch!=1.8,>=1.5.0->flair) (1.12)\n",
92
+ "Requirement already satisfied: jinja2 in /usr/local/lib/python3.10/dist-packages (from torch!=1.8,>=1.5.0->flair) (3.1.2)\n",
93
+ "Requirement already satisfied: nvidia-cuda-nvrtc-cu11==11.7.99 in /usr/local/lib/python3.10/dist-packages (from torch!=1.8,>=1.5.0->flair) (11.7.99)\n",
94
+ "Requirement already satisfied: nvidia-cuda-runtime-cu11==11.7.99 in /usr/local/lib/python3.10/dist-packages (from torch!=1.8,>=1.5.0->flair) (11.7.99)\n",
95
+ "Requirement already satisfied: nvidia-cuda-cupti-cu11==11.7.101 in /usr/local/lib/python3.10/dist-packages (from torch!=1.8,>=1.5.0->flair) (11.7.101)\n",
96
+ "Requirement already satisfied: nvidia-cudnn-cu11==8.5.0.96 in /usr/local/lib/python3.10/dist-packages (from torch!=1.8,>=1.5.0->flair) (8.5.0.96)\n",
97
+ "Requirement already satisfied: nvidia-cublas-cu11==11.10.3.66 in /usr/local/lib/python3.10/dist-packages (from torch!=1.8,>=1.5.0->flair) (11.10.3.66)\n",
98
+ "Requirement already satisfied: nvidia-cufft-cu11==10.9.0.58 in /usr/local/lib/python3.10/dist-packages (from torch!=1.8,>=1.5.0->flair) (10.9.0.58)\n",
99
+ "Requirement already satisfied: nvidia-curand-cu11==10.2.10.91 in /usr/local/lib/python3.10/dist-packages (from torch!=1.8,>=1.5.0->flair) (10.2.10.91)\n",
100
+ "Requirement already satisfied: nvidia-cusolver-cu11==11.4.0.1 in /usr/local/lib/python3.10/dist-packages (from torch!=1.8,>=1.5.0->flair) (11.4.0.1)\n",
101
+ "Requirement already satisfied: nvidia-cusparse-cu11==11.7.4.91 in /usr/local/lib/python3.10/dist-packages (from torch!=1.8,>=1.5.0->flair) (11.7.4.91)\n",
102
+ "Requirement already satisfied: nvidia-nccl-cu11==2.14.3 in /usr/local/lib/python3.10/dist-packages (from torch!=1.8,>=1.5.0->flair) (2.14.3)\n",
103
+ "Requirement already satisfied: nvidia-nvtx-cu11==11.7.91 in /usr/local/lib/python3.10/dist-packages (from torch!=1.8,>=1.5.0->flair) (11.7.91)\n",
104
+ "Requirement already satisfied: triton==2.0.0 in /usr/local/lib/python3.10/dist-packages (from torch!=1.8,>=1.5.0->flair) (2.0.0)\n",
105
+ "Requirement already satisfied: setuptools in /usr/local/lib/python3.10/dist-packages (from nvidia-cublas-cu11==11.10.3.66->torch!=1.8,>=1.5.0->flair) (67.7.2)\n",
106
+ "Requirement already satisfied: wheel in /usr/local/lib/python3.10/dist-packages (from nvidia-cublas-cu11==11.10.3.66->torch!=1.8,>=1.5.0->flair) (0.41.2)\n",
107
+ "Requirement already satisfied: cmake in /usr/local/lib/python3.10/dist-packages (from triton==2.0.0->torch!=1.8,>=1.5.0->flair) (3.27.4.1)\n",
108
+ "Requirement already satisfied: lit in /usr/local/lib/python3.10/dist-packages (from triton==2.0.0->torch!=1.8,>=1.5.0->flair) (16.0.6)\n",
109
+ "Requirement already satisfied: tokenizers!=0.11.3,<0.14,>=0.11.1 in /usr/local/lib/python3.10/dist-packages (from transformers[sentencepiece]>=4.18.0->flair) (0.13.3)\n",
110
+ "Requirement already satisfied: safetensors>=0.3.1 in /usr/local/lib/python3.10/dist-packages (from transformers[sentencepiece]>=4.18.0->flair) (0.3.3)\n",
111
+ "Requirement already satisfied: protobuf in /usr/local/lib/python3.10/dist-packages (from transformers[sentencepiece]>=4.18.0->flair) (3.20.3)\n",
112
+ "Requirement already satisfied: botocore<1.32.0,>=1.31.51 in /usr/local/lib/python3.10/dist-packages (from boto3->flair) (1.31.51)\n",
113
+ "Requirement already satisfied: jmespath<2.0.0,>=0.7.1 in /usr/local/lib/python3.10/dist-packages (from boto3->flair) (1.0.1)\n",
114
+ "Requirement already satisfied: s3transfer<0.7.0,>=0.6.0 in /usr/local/lib/python3.10/dist-packages (from boto3->flair) (0.6.2)\n",
115
+ "Requirement already satisfied: wcwidth>=0.2.5 in /usr/local/lib/python3.10/dist-packages (from ftfy->flair) (0.2.6)\n",
116
+ "Requirement already satisfied: urllib3<1.27,>=1.25.4 in /usr/local/lib/python3.10/dist-packages (from botocore<1.32.0,>=1.31.51->boto3->flair) (1.26.16)\n",
117
+ "Requirement already satisfied: accelerate>=0.20.3 in /usr/local/lib/python3.10/dist-packages (from transformers[sentencepiece]>=4.18.0->flair) (0.23.0)\n",
118
+ "Requirement already satisfied: soupsieve>1.2 in /usr/local/lib/python3.10/dist-packages (from beautifulsoup4->gdown==4.4.0->flair) (2.5)\n",
119
+ "Requirement already satisfied: MarkupSafe>=2.0 in /usr/local/lib/python3.10/dist-packages (from jinja2->torch!=1.8,>=1.5.0->flair) (2.1.3)\n",
120
+ "Requirement already satisfied: charset-normalizer<4,>=2 in /usr/local/lib/python3.10/dist-packages (from requests[socks]->gdown==4.4.0->flair) (3.2.0)\n",
121
+ "Requirement already satisfied: idna<4,>=2.5 in /usr/local/lib/python3.10/dist-packages (from requests[socks]->gdown==4.4.0->flair) (3.4)\n",
122
+ "Requirement already satisfied: certifi>=2017.4.17 in /usr/local/lib/python3.10/dist-packages (from requests[socks]->gdown==4.4.0->flair) (2023.7.22)\n",
123
+ "Requirement already satisfied: PySocks!=1.5.7,>=1.5.6 in /usr/local/lib/python3.10/dist-packages (from requests[socks]->gdown==4.4.0->flair) (1.7.1)\n",
124
+ "Requirement already satisfied: mpmath>=0.19 in /usr/local/lib/python3.10/dist-packages (from sympy->torch!=1.8,>=1.5.0->flair) (1.3.0)\n",
125
+ "Requirement already satisfied: psutil in /usr/local/lib/python3.10/dist-packages (from accelerate>=0.20.3->transformers[sentencepiece]>=4.18.0->flair) (5.9.5)\n"
126
+ ]
127
+ }
128
+ ]
129
+ },
130
+ {
131
+ "cell_type": "code",
132
+ "source": [
133
+ "from flair.datasets import CONLL_03\n",
134
+ "from flair.data import Corpus, Sentence\n",
135
+ "from flair.datasets import ColumnCorpus\n",
136
+ "from flair.embeddings import TransformerWordEmbeddings\n",
137
+ "from flair.models import SequenceTagger\n",
138
+ "from flair.trainers import ModelTrainer\n",
139
+ "\n",
140
+ "import pandas as pd\n",
141
+ "\n",
142
+ "from sklearn.model_selection import train_test_split"
143
+ ],
144
+ "metadata": {
145
+ "id": "TQNZ0mBwLriQ"
146
+ },
147
+ "execution_count": null,
148
+ "outputs": []
149
+ },
150
+ {
151
+ "cell_type": "code",
152
+ "source": [
153
+ "in_path = '/content/drive/MyDrive/Proyecto_ConflictoArmado/NER/output.txt'\n",
154
+ "\n",
155
+ "# Abre el archivo de texto en modo lectura\n",
156
+ "with open(in_path, 'r', encoding='utf-8') as file:\n",
157
+ " # Lee las líneas del archivo\n",
158
+ " lines = file.readlines()\n",
159
+ "\n",
160
+ "# Inicializa el DataFrame\n",
161
+ "data = {'sentence_id': [], 'text': [], 'tag': []}\n",
162
+ "\n",
163
+ "# Inicializa el identificador de oración\n",
164
+ "sentence_id = 1\n",
165
+ "\n",
166
+ "# Procesa cada línea del archivo\n",
167
+ "for line in lines:\n",
168
+ " line = line.strip() # Elimina espacios en blanco al principio y al final de la línea\n",
169
+ " if line: # Si la línea no está en blanco\n",
170
+ " parts = line.split() # Divide la línea en palabras y etiquetas\n",
171
+ " if len(parts) == 2:\n",
172
+ " word, tag = parts\n",
173
+ " data['sentence_id'].append(sentence_id)\n",
174
+ " data['text'].append(word)\n",
175
+ " data['tag'].append(tag)\n",
176
+ " else:\n",
177
+ " # Handle empty lines\n",
178
+ " data['sentence_id'].append(None)\n",
179
+ " data['text'].append(None)\n",
180
+ " data['tag'].append(None)\n",
181
+ " else:\n",
182
+ " # Incrementa el identificador de oración al encontrar una línea en blanco\n",
183
+ " sentence_id += 1\n",
184
+ "\n",
185
+ "# Crea el DataFrame\n",
186
+ "df = pd.DataFrame(data)\n",
187
+ "\n",
188
+ "umbral_longitud = 50\n",
189
+ "\n",
190
+ "# Filtrar las oraciones por longitud dentro de cada grupo de sentence_id\n",
191
+ "df_filtrado = df.groupby('sentence_id').filter(lambda group: len(group) < umbral_longitud)\n",
192
+ "\n",
193
+ "# Imprimir el DataFrame resultante\n",
194
+ "df_filtrado"
195
+ ],
196
+ "metadata": {
197
+ "colab": {
198
+ "base_uri": "https://localhost:8080/",
199
+ "height": 424
200
+ },
201
+ "id": "eIxuR6GwOwiU",
202
+ "outputId": "54951a94-56d6-492d-9704-0cecdcd49bf9"
203
+ },
204
+ "execution_count": null,
205
+ "outputs": [
206
+ {
207
+ "output_type": "execute_result",
208
+ "data": {
209
+ "text/plain": [
210
+ " sentence_id text tag\n",
211
+ "0 1.0 Naciones B-ORG\n",
212
+ "1 1.0 Unidas I-ORG\n",
213
+ "2 1.0 Asamblea O\n",
214
+ "3 1.0 General O\n",
215
+ "4 1.0 23 B-DATE\n",
216
+ "... ... ... ...\n",
217
+ "88927 658.0 los O\n",
218
+ "88928 658.0 niños B-PER\n",
219
+ "88929 658.0 niñas B-PER\n",
220
+ "88930 658.0 y O\n",
221
+ "88931 658.0 adolescentes B-PER\n",
222
+ "\n",
223
+ "[14485 rows x 3 columns]"
224
+ ],
225
+ "text/html": [
226
+ "\n",
227
+ " <div id=\"df-041c5cb9-f1a1-49d9-a970-35554cf7f2fd\" class=\"colab-df-container\">\n",
228
+ " <div>\n",
229
+ "<style scoped>\n",
230
+ " .dataframe tbody tr th:only-of-type {\n",
231
+ " vertical-align: middle;\n",
232
+ " }\n",
233
+ "\n",
234
+ " .dataframe tbody tr th {\n",
235
+ " vertical-align: top;\n",
236
+ " }\n",
237
+ "\n",
238
+ " .dataframe thead th {\n",
239
+ " text-align: right;\n",
240
+ " }\n",
241
+ "</style>\n",
242
+ "<table border=\"1\" class=\"dataframe\">\n",
243
+ " <thead>\n",
244
+ " <tr style=\"text-align: right;\">\n",
245
+ " <th></th>\n",
246
+ " <th>sentence_id</th>\n",
247
+ " <th>text</th>\n",
248
+ " <th>tag</th>\n",
249
+ " </tr>\n",
250
+ " </thead>\n",
251
+ " <tbody>\n",
252
+ " <tr>\n",
253
+ " <th>0</th>\n",
254
+ " <td>1.0</td>\n",
255
+ " <td>Naciones</td>\n",
256
+ " <td>B-ORG</td>\n",
257
+ " </tr>\n",
258
+ " <tr>\n",
259
+ " <th>1</th>\n",
260
+ " <td>1.0</td>\n",
261
+ " <td>Unidas</td>\n",
262
+ " <td>I-ORG</td>\n",
263
+ " </tr>\n",
264
+ " <tr>\n",
265
+ " <th>2</th>\n",
266
+ " <td>1.0</td>\n",
267
+ " <td>Asamblea</td>\n",
268
+ " <td>O</td>\n",
269
+ " </tr>\n",
270
+ " <tr>\n",
271
+ " <th>3</th>\n",
272
+ " <td>1.0</td>\n",
273
+ " <td>General</td>\n",
274
+ " <td>O</td>\n",
275
+ " </tr>\n",
276
+ " <tr>\n",
277
+ " <th>4</th>\n",
278
+ " <td>1.0</td>\n",
279
+ " <td>23</td>\n",
280
+ " <td>B-DATE</td>\n",
281
+ " </tr>\n",
282
+ " <tr>\n",
283
+ " <th>...</th>\n",
284
+ " <td>...</td>\n",
285
+ " <td>...</td>\n",
286
+ " <td>...</td>\n",
287
+ " </tr>\n",
288
+ " <tr>\n",
289
+ " <th>88927</th>\n",
290
+ " <td>658.0</td>\n",
291
+ " <td>los</td>\n",
292
+ " <td>O</td>\n",
293
+ " </tr>\n",
294
+ " <tr>\n",
295
+ " <th>88928</th>\n",
296
+ " <td>658.0</td>\n",
297
+ " <td>niños</td>\n",
298
+ " <td>B-PER</td>\n",
299
+ " </tr>\n",
300
+ " <tr>\n",
301
+ " <th>88929</th>\n",
302
+ " <td>658.0</td>\n",
303
+ " <td>niñas</td>\n",
304
+ " <td>B-PER</td>\n",
305
+ " </tr>\n",
306
+ " <tr>\n",
307
+ " <th>88930</th>\n",
308
+ " <td>658.0</td>\n",
309
+ " <td>y</td>\n",
310
+ " <td>O</td>\n",
311
+ " </tr>\n",
312
+ " <tr>\n",
313
+ " <th>88931</th>\n",
314
+ " <td>658.0</td>\n",
315
+ " <td>adolescentes</td>\n",
316
+ " <td>B-PER</td>\n",
317
+ " </tr>\n",
318
+ " </tbody>\n",
319
+ "</table>\n",
320
+ "<p>14485 rows × 3 columns</p>\n",
321
+ "</div>\n",
322
+ " <div class=\"colab-df-buttons\">\n",
323
+ "\n",
324
+ " <div class=\"colab-df-container\">\n",
325
+ " <button class=\"colab-df-convert\" onclick=\"convertToInteractive('df-041c5cb9-f1a1-49d9-a970-35554cf7f2fd')\"\n",
326
+ " title=\"Convert this dataframe to an interactive table.\"\n",
327
+ " style=\"display:none;\">\n",
328
+ "\n",
329
+ " <svg xmlns=\"http://www.w3.org/2000/svg\" height=\"24px\" viewBox=\"0 -960 960 960\">\n",
330
+ " <path d=\"M120-120v-720h720v720H120Zm60-500h600v-160H180v160Zm220 220h160v-160H400v160Zm0 220h160v-160H400v160ZM180-400h160v-160H180v160Zm440 0h160v-160H620v160ZM180-180h160v-160H180v160Zm440 0h160v-160H620v160Z\"/>\n",
331
+ " </svg>\n",
332
+ " </button>\n",
333
+ "\n",
334
+ " <style>\n",
335
+ " .colab-df-container {\n",
336
+ " display:flex;\n",
337
+ " gap: 12px;\n",
338
+ " }\n",
339
+ "\n",
340
+ " .colab-df-convert {\n",
341
+ " background-color: #E8F0FE;\n",
342
+ " border: none;\n",
343
+ " border-radius: 50%;\n",
344
+ " cursor: pointer;\n",
345
+ " display: none;\n",
346
+ " fill: #1967D2;\n",
347
+ " height: 32px;\n",
348
+ " padding: 0 0 0 0;\n",
349
+ " width: 32px;\n",
350
+ " }\n",
351
+ "\n",
352
+ " .colab-df-convert:hover {\n",
353
+ " background-color: #E2EBFA;\n",
354
+ " box-shadow: 0px 1px 2px rgba(60, 64, 67, 0.3), 0px 1px 3px 1px rgba(60, 64, 67, 0.15);\n",
355
+ " fill: #174EA6;\n",
356
+ " }\n",
357
+ "\n",
358
+ " .colab-df-buttons div {\n",
359
+ " margin-bottom: 4px;\n",
360
+ " }\n",
361
+ "\n",
362
+ " [theme=dark] .colab-df-convert {\n",
363
+ " background-color: #3B4455;\n",
364
+ " fill: #D2E3FC;\n",
365
+ " }\n",
366
+ "\n",
367
+ " [theme=dark] .colab-df-convert:hover {\n",
368
+ " background-color: #434B5C;\n",
369
+ " box-shadow: 0px 1px 3px 1px rgba(0, 0, 0, 0.15);\n",
370
+ " filter: drop-shadow(0px 1px 2px rgba(0, 0, 0, 0.3));\n",
371
+ " fill: #FFFFFF;\n",
372
+ " }\n",
373
+ " </style>\n",
374
+ "\n",
375
+ " <script>\n",
376
+ " const buttonEl =\n",
377
+ " document.querySelector('#df-041c5cb9-f1a1-49d9-a970-35554cf7f2fd button.colab-df-convert');\n",
378
+ " buttonEl.style.display =\n",
379
+ " google.colab.kernel.accessAllowed ? 'block' : 'none';\n",
380
+ "\n",
381
+ " async function convertToInteractive(key) {\n",
382
+ " const element = document.querySelector('#df-041c5cb9-f1a1-49d9-a970-35554cf7f2fd');\n",
383
+ " const dataTable =\n",
384
+ " await google.colab.kernel.invokeFunction('convertToInteractive',\n",
385
+ " [key], {});\n",
386
+ " if (!dataTable) return;\n",
387
+ "\n",
388
+ " const docLinkHtml = 'Like what you see? Visit the ' +\n",
389
+ " '<a target=\"_blank\" href=https://colab.research.google.com/notebooks/data_table.ipynb>data table notebook</a>'\n",
390
+ " + ' to learn more about interactive tables.';\n",
391
+ " element.innerHTML = '';\n",
392
+ " dataTable['output_type'] = 'display_data';\n",
393
+ " await google.colab.output.renderOutput(dataTable, element);\n",
394
+ " const docLink = document.createElement('div');\n",
395
+ " docLink.innerHTML = docLinkHtml;\n",
396
+ " element.appendChild(docLink);\n",
397
+ " }\n",
398
+ " </script>\n",
399
+ " </div>\n",
400
+ "\n",
401
+ "\n",
402
+ "<div id=\"df-f6b40e7a-7999-47d9-a5e0-d4cb48cfe301\">\n",
403
+ " <button class=\"colab-df-quickchart\" onclick=\"quickchart('df-f6b40e7a-7999-47d9-a5e0-d4cb48cfe301')\"\n",
404
+ " title=\"Suggest charts.\"\n",
405
+ " style=\"display:none;\">\n",
406
+ "\n",
407
+ "<svg xmlns=\"http://www.w3.org/2000/svg\" height=\"24px\"viewBox=\"0 0 24 24\"\n",
408
+ " width=\"24px\">\n",
409
+ " <g>\n",
410
+ " <path d=\"M19 3H5c-1.1 0-2 .9-2 2v14c0 1.1.9 2 2 2h14c1.1 0 2-.9 2-2V5c0-1.1-.9-2-2-2zM9 17H7v-7h2v7zm4 0h-2V7h2v10zm4 0h-2v-4h2v4z\"/>\n",
411
+ " </g>\n",
412
+ "</svg>\n",
413
+ " </button>\n",
414
+ "\n",
415
+ "<style>\n",
416
+ " .colab-df-quickchart {\n",
417
+ " --bg-color: #E8F0FE;\n",
418
+ " --fill-color: #1967D2;\n",
419
+ " --hover-bg-color: #E2EBFA;\n",
420
+ " --hover-fill-color: #174EA6;\n",
421
+ " --disabled-fill-color: #AAA;\n",
422
+ " --disabled-bg-color: #DDD;\n",
423
+ " }\n",
424
+ "\n",
425
+ " [theme=dark] .colab-df-quickchart {\n",
426
+ " --bg-color: #3B4455;\n",
427
+ " --fill-color: #D2E3FC;\n",
428
+ " --hover-bg-color: #434B5C;\n",
429
+ " --hover-fill-color: #FFFFFF;\n",
430
+ " --disabled-bg-color: #3B4455;\n",
431
+ " --disabled-fill-color: #666;\n",
432
+ " }\n",
433
+ "\n",
434
+ " .colab-df-quickchart {\n",
435
+ " background-color: var(--bg-color);\n",
436
+ " border: none;\n",
437
+ " border-radius: 50%;\n",
438
+ " cursor: pointer;\n",
439
+ " display: none;\n",
440
+ " fill: var(--fill-color);\n",
441
+ " height: 32px;\n",
442
+ " padding: 0;\n",
443
+ " width: 32px;\n",
444
+ " }\n",
445
+ "\n",
446
+ " .colab-df-quickchart:hover {\n",
447
+ " background-color: var(--hover-bg-color);\n",
448
+ " box-shadow: 0 1px 2px rgba(60, 64, 67, 0.3), 0 1px 3px 1px rgba(60, 64, 67, 0.15);\n",
449
+ " fill: var(--button-hover-fill-color);\n",
450
+ " }\n",
451
+ "\n",
452
+ " .colab-df-quickchart-complete:disabled,\n",
453
+ " .colab-df-quickchart-complete:disabled:hover {\n",
454
+ " background-color: var(--disabled-bg-color);\n",
455
+ " fill: var(--disabled-fill-color);\n",
456
+ " box-shadow: none;\n",
457
+ " }\n",
458
+ "\n",
459
+ " .colab-df-spinner {\n",
460
+ " border: 2px solid var(--fill-color);\n",
461
+ " border-color: transparent;\n",
462
+ " border-bottom-color: var(--fill-color);\n",
463
+ " animation:\n",
464
+ " spin 1s steps(1) infinite;\n",
465
+ " }\n",
466
+ "\n",
467
+ " @keyframes spin {\n",
468
+ " 0% {\n",
469
+ " border-color: transparent;\n",
470
+ " border-bottom-color: var(--fill-color);\n",
471
+ " border-left-color: var(--fill-color);\n",
472
+ " }\n",
473
+ " 20% {\n",
474
+ " border-color: transparent;\n",
475
+ " border-left-color: var(--fill-color);\n",
476
+ " border-top-color: var(--fill-color);\n",
477
+ " }\n",
478
+ " 30% {\n",
479
+ " border-color: transparent;\n",
480
+ " border-left-color: var(--fill-color);\n",
481
+ " border-top-color: var(--fill-color);\n",
482
+ " border-right-color: var(--fill-color);\n",
483
+ " }\n",
484
+ " 40% {\n",
485
+ " border-color: transparent;\n",
486
+ " border-right-color: var(--fill-color);\n",
487
+ " border-top-color: var(--fill-color);\n",
488
+ " }\n",
489
+ " 60% {\n",
490
+ " border-color: transparent;\n",
491
+ " border-right-color: var(--fill-color);\n",
492
+ " }\n",
493
+ " 80% {\n",
494
+ " border-color: transparent;\n",
495
+ " border-right-color: var(--fill-color);\n",
496
+ " border-bottom-color: var(--fill-color);\n",
497
+ " }\n",
498
+ " 90% {\n",
499
+ " border-color: transparent;\n",
500
+ " border-bottom-color: var(--fill-color);\n",
501
+ " }\n",
502
+ " }\n",
503
+ "</style>\n",
504
+ "\n",
505
+ " <script>\n",
506
+ " async function quickchart(key) {\n",
507
+ " const quickchartButtonEl =\n",
508
+ " document.querySelector('#' + key + ' button');\n",
509
+ " quickchartButtonEl.disabled = true; // To prevent multiple clicks.\n",
510
+ " quickchartButtonEl.classList.add('colab-df-spinner');\n",
511
+ " try {\n",
512
+ " const charts = await google.colab.kernel.invokeFunction(\n",
513
+ " 'suggestCharts', [key], {});\n",
514
+ " } catch (error) {\n",
515
+ " console.error('Error during call to suggestCharts:', error);\n",
516
+ " }\n",
517
+ " quickchartButtonEl.classList.remove('colab-df-spinner');\n",
518
+ " quickchartButtonEl.classList.add('colab-df-quickchart-complete');\n",
519
+ " }\n",
520
+ " (() => {\n",
521
+ " let quickchartButtonEl =\n",
522
+ " document.querySelector('#df-f6b40e7a-7999-47d9-a5e0-d4cb48cfe301 button');\n",
523
+ " quickchartButtonEl.style.display =\n",
524
+ " google.colab.kernel.accessAllowed ? 'block' : 'none';\n",
525
+ " })();\n",
526
+ " </script>\n",
527
+ "</div>\n",
528
+ " </div>\n",
529
+ " </div>\n"
530
+ ]
531
+ },
532
+ "metadata": {},
533
+ "execution_count": 3
534
+ }
535
+ ]
536
+ },
537
+ {
538
+ "cell_type": "code",
539
+ "source": [
540
+ "to_replace = ['B-'+tag for tag in ['CON', 'ATE', 'MEM', 'LUC', 'EVE', 'DER', 'LEY']]\n",
541
+ "to_replace.extend(['I-'+tag for tag in ['CON', 'ATE', 'MEM', 'LUC', 'EVE', 'DER', 'LEY']])\n",
542
+ "df_filtrado['new_tag'] = df_filtrado['tag'].replace(to_replace, 'O')"
543
+ ],
544
+ "metadata": {
545
+ "id": "VH8GZ35KYeYP"
546
+ },
547
+ "execution_count": null,
548
+ "outputs": []
549
+ },
550
+ {
551
+ "cell_type": "code",
552
+ "source": [
553
+ "# Split the DataFrame into train, test, and dev sets based on sentence_ids\n",
554
+ "train_ids, test_ids = train_test_split(df_filtrado['sentence_id'].unique(), test_size=0.2, random_state=42)\n",
555
+ "train_ids, dev_ids = train_test_split(train_ids, test_size=0.1, random_state=42)\n",
556
+ "\n",
557
+ "# Define a function to format rows as \"word label\"\n",
558
+ "def format_row(word, label):\n",
559
+ " return f\"{word} {label}\"\n",
560
+ "\n",
561
+ "# Save data into separate files based on sentence_ids\n",
562
+ "for split_name, split_ids in [('train', train_ids), ('test', test_ids), ('dev', dev_ids)]:\n",
563
+ " split_data = df_filtrado[df_filtrado['sentence_id'].isin(split_ids)]\n",
564
+ "\n",
565
+ " formatted_rows = []\n",
566
+ " for _, row in split_data.groupby('sentence_id'):\n",
567
+ " formatted_rows.extend([format_row(word, label) for word, label in zip(row['text'], row['new_tag'])])\n",
568
+ " formatted_rows.append(\"\") # Append a blank line after each sentence\n",
569
+ "\n",
570
+ " with open(f'{split_name}.txt', 'w') as file:\n",
571
+ " file.write('\\n'.join(formatted_rows))"
572
+ ],
573
+ "metadata": {
574
+ "id": "svsuyhfWPbn7"
575
+ },
576
+ "execution_count": null,
577
+ "outputs": []
578
+ },
579
+ {
580
+ "cell_type": "code",
581
+ "source": [
582
+ "# 1. get the corpus\n",
583
+ "# define columns\n",
584
+ "columns = {0 : 'text', 1 : 'ner'}\n",
585
+ "\n",
586
+ "# directory where the data resides\n",
587
+ "data_folder = '/content/'\n",
588
+ "\n",
589
+ "# initializing the corpus\n",
590
+ "corpus: Corpus = ColumnCorpus(data_folder, columns,\n",
591
+ " train_file = 'train.txt',\n",
592
+ " test_file = 'test.txt',\n",
593
+ " dev_file = 'dev.txt')"
594
+ ],
595
+ "metadata": {
596
+ "colab": {
597
+ "base_uri": "https://localhost:8080/"
598
+ },
599
+ "id": "gpJbWNZUOiOo",
600
+ "outputId": "c3f21610-2344-4491-99a8-a076fde637a5"
601
+ },
602
+ "execution_count": null,
603
+ "outputs": [
604
+ {
605
+ "output_type": "stream",
606
+ "name": "stdout",
607
+ "text": [
608
+ "2023-09-20 16:39:04,538 Reading data from /content\n",
609
+ "2023-09-20 16:39:04,541 Train: /content/train.txt\n",
610
+ "2023-09-20 16:39:04,542 Dev: /content/dev.txt\n",
611
+ "2023-09-20 16:39:04,544 Test: /content/test.txt\n"
612
+ ]
613
+ }
614
+ ]
615
+ },
616
+ {
617
+ "cell_type": "code",
618
+ "source": [
619
+ "# 2. what label do we want to predict?\n",
620
+ "label_type = 'ner'"
621
+ ],
622
+ "metadata": {
623
+ "id": "Sng99Yx8Oc6d"
624
+ },
625
+ "execution_count": null,
626
+ "outputs": []
627
+ },
628
+ {
629
+ "cell_type": "code",
630
+ "source": [
631
+ "# 3. make the label dictionary from the corpus\n",
632
+ "label_dict = corpus.make_label_dictionary(label_type=label_type, add_unk=False)\n",
633
+ "print(label_dict)"
634
+ ],
635
+ "metadata": {
636
+ "colab": {
637
+ "base_uri": "https://localhost:8080/"
638
+ },
639
+ "id": "F9FDbMdxOdPV",
640
+ "outputId": "5108697c-0a4c-454b-b423-8c98fac4212a"
641
+ },
642
+ "execution_count": null,
643
+ "outputs": [
644
+ {
645
+ "output_type": "stream",
646
+ "name": "stdout",
647
+ "text": [
648
+ "2023-09-20 16:39:05,130 Computing label dictionary. Progress:\n"
649
+ ]
650
+ },
651
+ {
652
+ "output_type": "stream",
653
+ "name": "stderr",
654
+ "text": [
655
+ "378it [00:00, 12320.75it/s]"
656
+ ]
657
+ },
658
+ {
659
+ "output_type": "stream",
660
+ "name": "stdout",
661
+ "text": [
662
+ "2023-09-20 16:39:05,181 Dictionary created for label 'ner' with 8 values: PER (seen 164 times), GEO (seen 116 times), VIO (seen 113 times), ORG (seen 102 times), ARM (seen 65 times), PAZ (seen 42 times), DATE (seen 40 times), AFE (seen 33 times)\n",
663
+ "Dictionary with 8 tags: PER, GEO, VIO, ORG, ARM, PAZ, DATE, AFE\n"
664
+ ]
665
+ },
666
+ {
667
+ "output_type": "stream",
668
+ "name": "stderr",
669
+ "text": [
670
+ "\n"
671
+ ]
672
+ }
673
+ ]
674
+ },
675
+ {
676
+ "cell_type": "code",
677
+ "source": [
678
+ "# 4. initialize fine-tuneable transformer embeddings WITH document context\n",
679
+ "embeddings = TransformerWordEmbeddings(model='xlm-roberta-large',\n",
680
+ " layers=\"-1\",\n",
681
+ " subtoken_pooling=\"first\",\n",
682
+ " fine_tune=True,\n",
683
+ " use_context=True,\n",
684
+ " )"
685
+ ],
686
+ "metadata": {
687
+ "id": "5_HyQ1R3QVXE"
688
+ },
689
+ "execution_count": null,
690
+ "outputs": []
691
+ },
692
+ {
693
+ "cell_type": "code",
694
+ "source": [
695
+ "# 5. initialize bare-bones sequence tagger (no CRF, no RNN, no reprojection)\n",
696
+ "tagger = SequenceTagger(hidden_size=256,\n",
697
+ " embeddings=embeddings,\n",
698
+ " tag_dictionary=label_dict,\n",
699
+ " tag_type='ner',\n",
700
+ " use_crf=False,\n",
701
+ " use_rnn=False,\n",
702
+ " reproject_embeddings=False,\n",
703
+ " tag_format='BIO')"
704
+ ],
705
+ "metadata": {
706
+ "colab": {
707
+ "base_uri": "https://localhost:8080/"
708
+ },
709
+ "id": "ycL2ky6DQq_B",
710
+ "outputId": "2f742b39-39b3-4e81-c616-016b3fc09233"
711
+ },
712
+ "execution_count": null,
713
+ "outputs": [
714
+ {
715
+ "output_type": "stream",
716
+ "name": "stdout",
717
+ "text": [
718
+ "2023-09-20 16:39:27,403 SequenceTagger predicts: Dictionary with 17 tags: O, B-PER, I-PER, B-GEO, I-GEO, B-VIO, I-VIO, B-ORG, I-ORG, B-ARM, I-ARM, B-PAZ, I-PAZ, B-DATE, I-DATE, B-AFE, I-AFE\n"
719
+ ]
720
+ }
721
+ ]
722
+ },
723
+ {
724
+ "cell_type": "code",
725
+ "source": [
726
+ "# 6. initialize trainer\n",
727
+ "trainer = ModelTrainer(tagger, corpus)"
728
+ ],
729
+ "metadata": {
730
+ "id": "ZscNZ3F1Q2II"
731
+ },
732
+ "execution_count": null,
733
+ "outputs": []
734
+ },
735
+ {
736
+ "cell_type": "code",
737
+ "source": [
738
+ "# 7. run fine-tuning\n",
739
+ "trainer.fine_tune('ner-bertSpanish-fineTuning2',\n",
740
+ " learning_rate=5.0e-6,\n",
741
+ " mini_batch_size=4,\n",
742
+ " mini_batch_chunk_size=1, # remove this parameter to speed up computation if you have a big GPU\n",
743
+ " )"
744
+ ],
745
+ "metadata": {
746
+ "colab": {
747
+ "base_uri": "https://localhost:8080/"
748
+ },
749
+ "id": "qbyyDWDyQ4QZ",
750
+ "outputId": "a71a4ee2-2078-4267-d4e3-7782e0c387ee"
751
+ },
752
+ "execution_count": null,
753
+ "outputs": [
754
+ {
755
+ "output_type": "stream",
756
+ "name": "stdout",
757
+ "text": [
758
+ "2023-09-20 16:39:27,431 ----------------------------------------------------------------------------------------------------\n",
759
+ "2023-09-20 16:39:27,447 Model: \"SequenceTagger(\n",
760
+ " (embeddings): TransformerWordEmbeddings(\n",
761
+ " (model): XLMRobertaModel(\n",
762
+ " (embeddings): XLMRobertaEmbeddings(\n",
763
+ " (word_embeddings): Embedding(250003, 1024)\n",
764
+ " (position_embeddings): Embedding(514, 1024, padding_idx=1)\n",
765
+ " (token_type_embeddings): Embedding(1, 1024)\n",
766
+ " (LayerNorm): LayerNorm((1024,), eps=1e-05, elementwise_affine=True)\n",
767
+ " (dropout): Dropout(p=0.1, inplace=False)\n",
768
+ " )\n",
769
+ " (encoder): XLMRobertaEncoder(\n",
770
+ " (layer): ModuleList(\n",
771
+ " (0-23): 24 x XLMRobertaLayer(\n",
772
+ " (attention): XLMRobertaAttention(\n",
773
+ " (self): XLMRobertaSelfAttention(\n",
774
+ " (query): Linear(in_features=1024, out_features=1024, bias=True)\n",
775
+ " (key): Linear(in_features=1024, out_features=1024, bias=True)\n",
776
+ " (value): Linear(in_features=1024, out_features=1024, bias=True)\n",
777
+ " (dropout): Dropout(p=0.1, inplace=False)\n",
778
+ " )\n",
779
+ " (output): XLMRobertaSelfOutput(\n",
780
+ " (dense): Linear(in_features=1024, out_features=1024, bias=True)\n",
781
+ " (LayerNorm): LayerNorm((1024,), eps=1e-05, elementwise_affine=True)\n",
782
+ " (dropout): Dropout(p=0.1, inplace=False)\n",
783
+ " )\n",
784
+ " )\n",
785
+ " (intermediate): XLMRobertaIntermediate(\n",
786
+ " (dense): Linear(in_features=1024, out_features=4096, bias=True)\n",
787
+ " (intermediate_act_fn): GELUActivation()\n",
788
+ " )\n",
789
+ " (output): XLMRobertaOutput(\n",
790
+ " (dense): Linear(in_features=4096, out_features=1024, bias=True)\n",
791
+ " (LayerNorm): LayerNorm((1024,), eps=1e-05, elementwise_affine=True)\n",
792
+ " (dropout): Dropout(p=0.1, inplace=False)\n",
793
+ " )\n",
794
+ " )\n",
795
+ " )\n",
796
+ " )\n",
797
+ " (pooler): XLMRobertaPooler(\n",
798
+ " (dense): Linear(in_features=1024, out_features=1024, bias=True)\n",
799
+ " (activation): Tanh()\n",
800
+ " )\n",
801
+ " )\n",
802
+ " )\n",
803
+ " (locked_dropout): LockedDropout(p=0.5)\n",
804
+ " (linear): Linear(in_features=1024, out_features=17, bias=True)\n",
805
+ " (loss_function): CrossEntropyLoss()\n",
806
+ ")\"\n",
807
+ "2023-09-20 16:39:27,450 ----------------------------------------------------------------------------------------------------\n",
808
+ "2023-09-20 16:39:27,454 Corpus: \"Corpus: 378 train + 42 dev + 106 test sentences\"\n",
809
+ "2023-09-20 16:39:27,455 ----------------------------------------------------------------------------------------------------\n",
810
+ "2023-09-20 16:39:27,458 Parameters:\n",
811
+ "2023-09-20 16:39:27,461 - learning_rate: \"0.000005\"\n",
812
+ "2023-09-20 16:39:27,471 - mini_batch_size: \"4\"\n",
813
+ "2023-09-20 16:39:27,481 - patience: \"3\"\n",
814
+ "2023-09-20 16:39:27,483 - anneal_factor: \"0.5\"\n",
815
+ "2023-09-20 16:39:27,488 - max_epochs: \"10\"\n",
816
+ "2023-09-20 16:39:27,492 - shuffle: \"True\"\n",
817
+ "2023-09-20 16:39:27,499 - train_with_dev: \"False\"\n",
818
+ "2023-09-20 16:39:27,501 - batch_growth_annealing: \"False\"\n",
819
+ "2023-09-20 16:39:27,504 ----------------------------------------------------------------------------------------------------\n",
820
+ "2023-09-20 16:39:27,506 Model training base path: \"ner-bertSpanish-fineTuning2\"\n",
821
+ "2023-09-20 16:39:27,509 ----------------------------------------------------------------------------------------------------\n",
822
+ "2023-09-20 16:39:27,512 Device: cuda:0\n",
823
+ "2023-09-20 16:39:27,515 ----------------------------------------------------------------------------------------------------\n",
824
+ "2023-09-20 16:39:27,517 Embeddings storage mode: none\n",
825
+ "2023-09-20 16:39:27,521 ----------------------------------------------------------------------------------------------------\n",
826
+ "2023-09-20 16:39:36,397 epoch 1 - iter 9/95 - loss 3.34492128 - time (sec): 8.87 - samples/sec: 103.44 - lr: 0.000000\n",
827
+ "2023-09-20 16:39:46,525 epoch 1 - iter 18/95 - loss 3.28965828 - time (sec): 19.00 - samples/sec: 92.20 - lr: 0.000001\n",
828
+ "2023-09-20 16:39:53,173 epoch 1 - iter 27/95 - loss 3.24176297 - time (sec): 25.65 - samples/sec: 102.41 - lr: 0.000001\n",
829
+ "2023-09-20 16:40:00,001 epoch 1 - iter 36/95 - loss 3.24931554 - time (sec): 32.48 - samples/sec: 110.47 - lr: 0.000002\n",
830
+ "2023-09-20 16:40:07,278 epoch 1 - iter 45/95 - loss 3.21247192 - time (sec): 39.76 - samples/sec: 113.74 - lr: 0.000002\n",
831
+ "2023-09-20 16:40:13,757 epoch 1 - iter 54/95 - loss 3.13623468 - time (sec): 46.23 - samples/sec: 122.48 - lr: 0.000003\n",
832
+ "2023-09-20 16:40:20,791 epoch 1 - iter 63/95 - loss 2.98242809 - time (sec): 53.27 - samples/sec: 127.69 - lr: 0.000003\n",
833
+ "2023-09-20 16:40:27,323 epoch 1 - iter 72/95 - loss 2.71884308 - time (sec): 59.80 - samples/sec: 131.47 - lr: 0.000004\n",
834
+ "2023-09-20 16:40:33,896 epoch 1 - iter 81/95 - loss 2.45957049 - time (sec): 66.37 - samples/sec: 133.97 - lr: 0.000004\n",
835
+ "2023-09-20 16:40:40,825 epoch 1 - iter 90/95 - loss 2.29787318 - time (sec): 73.30 - samples/sec: 134.89 - lr: 0.000005\n",
836
+ "2023-09-20 16:40:44,103 ----------------------------------------------------------------------------------------------------\n",
837
+ "2023-09-20 16:40:44,105 EPOCH 1 done: loss 2.2083 - lr 0.000005\n"
838
+ ]
839
+ },
840
+ {
841
+ "output_type": "stream",
842
+ "name": "stderr",
843
+ "text": [
844
+ "100%|██████████| 11/11 [00:02<00:00, 4.94it/s]"
845
+ ]
846
+ },
847
+ {
848
+ "output_type": "stream",
849
+ "name": "stdout",
850
+ "text": [
851
+ "2023-09-20 16:40:46,340 Evaluating as a multi-label problem: False\n",
852
+ "2023-09-20 16:40:46,359 DEV : loss 0.6564602851867676 - f1-score (micro avg) 0.0\n",
853
+ "2023-09-20 16:40:46,363 ----------------------------------------------------------------------------------------------------\n"
854
+ ]
855
+ },
856
+ {
857
+ "output_type": "stream",
858
+ "name": "stderr",
859
+ "text": [
860
+ "\n"
861
+ ]
862
+ },
863
+ {
864
+ "output_type": "stream",
865
+ "name": "stdout",
866
+ "text": [
867
+ "2023-09-20 16:40:52,692 epoch 2 - iter 9/95 - loss 0.68008748 - time (sec): 6.33 - samples/sec: 164.03 - lr: 0.000005\n",
868
+ "2023-09-20 16:40:59,736 epoch 2 - iter 18/95 - loss 0.82214551 - time (sec): 13.37 - samples/sec: 150.47 - lr: 0.000005\n",
869
+ "2023-09-20 16:41:06,257 epoch 2 - iter 27/95 - loss 0.84081419 - time (sec): 19.89 - samples/sec: 151.82 - lr: 0.000005\n",
870
+ "2023-09-20 16:41:13,054 epoch 2 - iter 36/95 - loss 0.82311766 - time (sec): 26.69 - samples/sec: 149.50 - lr: 0.000005\n",
871
+ "2023-09-20 16:41:21,216 epoch 2 - iter 45/95 - loss 0.85481312 - time (sec): 34.85 - samples/sec: 144.73 - lr: 0.000005\n",
872
+ "2023-09-20 16:41:27,552 epoch 2 - iter 54/95 - loss 0.83370049 - time (sec): 41.19 - samples/sec: 143.63 - lr: 0.000005\n",
873
+ "2023-09-20 16:41:34,547 epoch 2 - iter 63/95 - loss 0.81702753 - time (sec): 48.18 - samples/sec: 141.57 - lr: 0.000005\n",
874
+ "2023-09-20 16:41:40,865 epoch 2 - iter 72/95 - loss 0.83400647 - time (sec): 54.50 - samples/sec: 142.35 - lr: 0.000005\n",
875
+ "2023-09-20 16:41:47,553 epoch 2 - iter 81/95 - loss 0.81831942 - time (sec): 61.19 - samples/sec: 143.43 - lr: 0.000005\n",
876
+ "2023-09-20 16:41:54,534 epoch 2 - iter 90/95 - loss 0.80080903 - time (sec): 68.17 - samples/sec: 144.46 - lr: 0.000004\n",
877
+ "2023-09-20 16:41:58,032 ----------------------------------------------------------------------------------------------------\n",
878
+ "2023-09-20 16:41:58,033 EPOCH 2 done: loss 0.7905 - lr 0.000004\n"
879
+ ]
880
+ },
881
+ {
882
+ "output_type": "stream",
883
+ "name": "stderr",
884
+ "text": [
885
+ "100%|██████████| 11/11 [00:02<00:00, 4.83it/s]"
886
+ ]
887
+ },
888
+ {
889
+ "output_type": "stream",
890
+ "name": "stdout",
891
+ "text": [
892
+ "2023-09-20 16:42:00,319 Evaluating as a multi-label problem: False\n",
893
+ "2023-09-20 16:42:00,334 DEV : loss 0.5198074579238892 - f1-score (micro avg) 0.0\n",
894
+ "2023-09-20 16:42:00,337 ----------------------------------------------------------------------------------------------------\n"
895
+ ]
896
+ },
897
+ {
898
+ "output_type": "stream",
899
+ "name": "stderr",
900
+ "text": [
901
+ "\n"
902
+ ]
903
+ },
904
+ {
905
+ "output_type": "stream",
906
+ "name": "stdout",
907
+ "text": [
908
+ "2023-09-20 16:42:06,878 epoch 3 - iter 9/95 - loss 0.57110353 - time (sec): 6.54 - samples/sec: 160.02 - lr: 0.000004\n",
909
+ "2023-09-20 16:42:13,524 epoch 3 - iter 18/95 - loss 0.62175665 - time (sec): 13.18 - samples/sec: 147.39 - lr: 0.000004\n",
910
+ "2023-09-20 16:42:20,138 epoch 3 - iter 27/95 - loss 0.57933822 - time (sec): 19.80 - samples/sec: 149.56 - lr: 0.000004\n",
911
+ "2023-09-20 16:42:27,222 epoch 3 - iter 36/95 - loss 0.59631046 - time (sec): 26.88 - samples/sec: 150.18 - lr: 0.000004\n",
912
+ "2023-09-20 16:42:33,797 epoch 3 - iter 45/95 - loss 0.58896606 - time (sec): 33.46 - samples/sec: 149.93 - lr: 0.000004\n",
913
+ "2023-09-20 16:42:40,231 epoch 3 - iter 54/95 - loss 0.60859906 - time (sec): 39.89 - samples/sec: 150.41 - lr: 0.000004\n",
914
+ "2023-09-20 16:42:47,408 epoch 3 - iter 63/95 - loss 0.63216711 - time (sec): 47.07 - samples/sec: 147.00 - lr: 0.000004\n",
915
+ "2023-09-20 16:42:53,559 epoch 3 - iter 72/95 - loss 0.63824288 - time (sec): 53.22 - samples/sec: 148.41 - lr: 0.000004\n",
916
+ "2023-09-20 16:43:00,419 epoch 3 - iter 81/95 - loss 0.67740427 - time (sec): 60.08 - samples/sec: 149.91 - lr: 0.000004\n",
917
+ "2023-09-20 16:43:07,400 epoch 3 - iter 90/95 - loss 0.68385038 - time (sec): 67.06 - samples/sec: 148.69 - lr: 0.000004\n",
918
+ "2023-09-20 16:43:10,660 ----------------------------------------------------------------------------------------------------\n",
919
+ "2023-09-20 16:43:10,662 EPOCH 3 done: loss 0.6759 - lr 0.000004\n"
920
+ ]
921
+ },
922
+ {
923
+ "output_type": "stream",
924
+ "name": "stderr",
925
+ "text": [
926
+ "100%|██████████| 11/11 [00:02<00:00, 4.86it/s]"
927
+ ]
928
+ },
929
+ {
930
+ "output_type": "stream",
931
+ "name": "stdout",
932
+ "text": [
933
+ "2023-09-20 16:43:12,936 Evaluating as a multi-label problem: False\n",
934
+ "2023-09-20 16:43:12,953 DEV : loss 0.4649089276790619 - f1-score (micro avg) 0.0\n",
935
+ "2023-09-20 16:43:12,958 ----------------------------------------------------------------------------------------------------\n"
936
+ ]
937
+ },
938
+ {
939
+ "output_type": "stream",
940
+ "name": "stderr",
941
+ "text": [
942
+ "\n"
943
+ ]
944
+ },
945
+ {
946
+ "output_type": "stream",
947
+ "name": "stdout",
948
+ "text": [
949
+ "2023-09-20 16:43:19,654 epoch 4 - iter 9/95 - loss 0.68437438 - time (sec): 6.69 - samples/sec: 153.56 - lr: 0.000004\n",
950
+ "2023-09-20 16:43:26,266 epoch 4 - iter 18/95 - loss 0.61813965 - time (sec): 13.31 - samples/sec: 153.23 - lr: 0.000004\n",
951
+ "2023-09-20 16:43:32,808 epoch 4 - iter 27/95 - loss 0.61204996 - time (sec): 19.85 - samples/sec: 157.44 - lr: 0.000004\n",
952
+ "2023-09-20 16:43:40,029 epoch 4 - iter 36/95 - loss 0.61660487 - time (sec): 27.07 - samples/sec: 151.61 - lr: 0.000004\n",
953
+ "2023-09-20 16:43:46,462 epoch 4 - iter 45/95 - loss 0.60674645 - time (sec): 33.50 - samples/sec: 149.72 - lr: 0.000004\n",
954
+ "2023-09-20 16:43:53,219 epoch 4 - iter 54/95 - loss 0.62906709 - time (sec): 40.26 - samples/sec: 148.66 - lr: 0.000004\n",
955
+ "2023-09-20 16:44:00,505 epoch 4 - iter 63/95 - loss 0.61740208 - time (sec): 47.55 - samples/sec: 148.32 - lr: 0.000004\n",
956
+ "2023-09-20 16:44:06,796 epoch 4 - iter 72/95 - loss 0.62528363 - time (sec): 53.84 - samples/sec: 147.19 - lr: 0.000003\n",
957
+ "2023-09-20 16:44:13,495 epoch 4 - iter 81/95 - loss 0.62469252 - time (sec): 60.54 - samples/sec: 149.07 - lr: 0.000003\n",
958
+ "2023-09-20 16:44:20,347 epoch 4 - iter 90/95 - loss 0.62439226 - time (sec): 67.39 - samples/sec: 148.01 - lr: 0.000003\n",
959
+ "2023-09-20 16:44:23,654 ----------------------------------------------------------------------------------------------------\n",
960
+ "2023-09-20 16:44:23,656 EPOCH 4 done: loss 0.6175 - lr 0.000003\n"
961
+ ]
962
+ },
963
+ {
964
+ "output_type": "stream",
965
+ "name": "stderr",
966
+ "text": [
967
+ "100%|██████████| 11/11 [00:02<00:00, 4.87it/s]"
968
+ ]
969
+ },
970
+ {
971
+ "output_type": "stream",
972
+ "name": "stdout",
973
+ "text": [
974
+ "2023-09-20 16:44:25,928 Evaluating as a multi-label problem: False\n",
975
+ "2023-09-20 16:44:25,946 DEV : loss 0.4446188509464264 - f1-score (micro avg) 0.0\n",
976
+ "2023-09-20 16:44:25,949 ----------------------------------------------------------------------------------------------------\n"
977
+ ]
978
+ },
979
+ {
980
+ "output_type": "stream",
981
+ "name": "stderr",
982
+ "text": [
983
+ "\n"
984
+ ]
985
+ },
986
+ {
987
+ "output_type": "stream",
988
+ "name": "stdout",
989
+ "text": [
990
+ "2023-09-20 16:44:32,764 epoch 5 - iter 9/95 - loss 0.68330165 - time (sec): 6.81 - samples/sec: 149.71 - lr: 0.000003\n",
991
+ "2023-09-20 16:44:39,414 epoch 5 - iter 18/95 - loss 0.55349558 - time (sec): 13.46 - samples/sec: 145.36 - lr: 0.000003\n",
992
+ "2023-09-20 16:44:45,683 epoch 5 - iter 27/95 - loss 0.55279158 - time (sec): 19.73 - samples/sec: 147.63 - lr: 0.000003\n",
993
+ "2023-09-20 16:44:52,989 epoch 5 - iter 36/95 - loss 0.53164896 - time (sec): 27.04 - samples/sec: 144.95 - lr: 0.000003\n",
994
+ "2023-09-20 16:44:59,509 epoch 5 - iter 45/95 - loss 0.57837004 - time (sec): 33.56 - samples/sec: 147.21 - lr: 0.000003\n",
995
+ "2023-09-20 16:45:06,162 epoch 5 - iter 54/95 - loss 0.54499535 - time (sec): 40.21 - samples/sec: 150.13 - lr: 0.000003\n",
996
+ "2023-09-20 16:45:13,305 epoch 5 - iter 63/95 - loss 0.55937756 - time (sec): 47.35 - samples/sec: 148.82 - lr: 0.000003\n",
997
+ "2023-09-20 16:45:19,656 epoch 5 - iter 72/95 - loss 0.56013288 - time (sec): 53.70 - samples/sec: 149.84 - lr: 0.000003\n",
998
+ "2023-09-20 16:45:26,507 epoch 5 - iter 81/95 - loss 0.54816732 - time (sec): 60.56 - samples/sec: 149.07 - lr: 0.000003\n",
999
+ "2023-09-20 16:45:33,154 epoch 5 - iter 90/95 - loss 0.53285600 - time (sec): 67.20 - samples/sec: 148.39 - lr: 0.000003\n",
1000
+ "2023-09-20 16:45:36,568 ----------------------------------------------------------------------------------------------------\n",
1001
+ "2023-09-20 16:45:36,569 EPOCH 5 done: loss 0.5402 - lr 0.000003\n"
1002
+ ]
1003
+ },
1004
+ {
1005
+ "output_type": "stream",
1006
+ "name": "stderr",
1007
+ "text": [
1008
+ "100%|██████████| 11/11 [00:02<00:00, 4.85it/s]"
1009
+ ]
1010
+ },
1011
+ {
1012
+ "output_type": "stream",
1013
+ "name": "stdout",
1014
+ "text": [
1015
+ "2023-09-20 16:45:38,846 Evaluating as a multi-label problem: False\n",
1016
+ "2023-09-20 16:45:38,866 DEV : loss 0.4345150291919708 - f1-score (micro avg) 0.0256\n",
1017
+ "2023-09-20 16:45:38,870 ----------------------------------------------------------------------------------------------------\n"
1018
+ ]
1019
+ },
1020
+ {
1021
+ "output_type": "stream",
1022
+ "name": "stderr",
1023
+ "text": [
1024
+ "\n"
1025
+ ]
1026
+ },
1027
+ {
1028
+ "output_type": "stream",
1029
+ "name": "stdout",
1030
+ "text": [
1031
+ "2023-09-20 16:45:45,695 epoch 6 - iter 9/95 - loss 0.52923392 - time (sec): 6.82 - samples/sec: 145.11 - lr: 0.000003\n",
1032
+ "2023-09-20 16:45:51,985 epoch 6 - iter 18/95 - loss 0.51792781 - time (sec): 13.11 - samples/sec: 156.03 - lr: 0.000003\n",
1033
+ "2023-09-20 16:45:58,393 epoch 6 - iter 27/95 - loss 0.49117250 - time (sec): 19.52 - samples/sec: 151.07 - lr: 0.000003\n",
1034
+ "2023-09-20 16:46:05,614 epoch 6 - iter 36/95 - loss 0.50167473 - time (sec): 26.74 - samples/sec: 147.26 - lr: 0.000003\n",
1035
+ "2023-09-20 16:46:11,980 epoch 6 - iter 45/95 - loss 0.47179564 - time (sec): 33.11 - samples/sec: 149.51 - lr: 0.000003\n",
1036
+ "2023-09-20 16:46:18,861 epoch 6 - iter 54/95 - loss 0.45136159 - time (sec): 39.99 - samples/sec: 152.32 - lr: 0.000002\n",
1037
+ "2023-09-20 16:46:25,844 epoch 6 - iter 63/95 - loss 0.47423503 - time (sec): 46.97 - samples/sec: 149.30 - lr: 0.000002\n",
1038
+ "2023-09-20 16:46:32,126 epoch 6 - iter 72/95 - loss 0.47496461 - time (sec): 53.25 - samples/sec: 150.66 - lr: 0.000002\n",
1039
+ "2023-09-20 16:46:39,170 epoch 6 - iter 81/95 - loss 0.49848349 - time (sec): 60.30 - samples/sec: 148.28 - lr: 0.000002\n",
1040
+ "2023-09-20 16:46:45,744 epoch 6 - iter 90/95 - loss 0.49210164 - time (sec): 66.87 - samples/sec: 148.99 - lr: 0.000002\n",
1041
+ "2023-09-20 16:46:49,149 ----------------------------------------------------------------------------------------------------\n",
1042
+ "2023-09-20 16:46:49,150 EPOCH 6 done: loss 0.4930 - lr 0.000002\n"
1043
+ ]
1044
+ },
1045
+ {
1046
+ "output_type": "stream",
1047
+ "name": "stderr",
1048
+ "text": [
1049
+ "100%|██████████| 11/11 [00:02<00:00, 4.88it/s]"
1050
+ ]
1051
+ },
1052
+ {
1053
+ "output_type": "stream",
1054
+ "name": "stdout",
1055
+ "text": [
1056
+ "2023-09-20 16:46:51,414 Evaluating as a multi-label problem: False\n",
1057
+ "2023-09-20 16:46:51,428 DEV : loss 0.38467857241630554 - f1-score (micro avg) 0.2286\n",
1058
+ "2023-09-20 16:46:51,432 ----------------------------------------------------------------------------------------------------\n"
1059
+ ]
1060
+ },
1061
+ {
1062
+ "output_type": "stream",
1063
+ "name": "stderr",
1064
+ "text": [
1065
+ "\n"
1066
+ ]
1067
+ },
1068
+ {
1069
+ "output_type": "stream",
1070
+ "name": "stdout",
1071
+ "text": [
1072
+ "2023-09-20 16:46:58,494 epoch 7 - iter 9/95 - loss 0.40116530 - time (sec): 7.06 - samples/sec: 149.71 - lr: 0.000002\n",
1073
+ "2023-09-20 16:47:04,965 epoch 7 - iter 18/95 - loss 0.45271521 - time (sec): 13.53 - samples/sec: 153.11 - lr: 0.000002\n",
1074
+ "2023-09-20 16:47:11,575 epoch 7 - iter 27/95 - loss 0.45269619 - time (sec): 20.14 - samples/sec: 153.41 - lr: 0.000002\n",
1075
+ "2023-09-20 16:47:18,725 epoch 7 - iter 36/95 - loss 0.44785464 - time (sec): 27.29 - samples/sec: 150.23 - lr: 0.000002\n",
1076
+ "2023-09-20 16:47:25,036 epoch 7 - iter 45/95 - loss 0.41670820 - time (sec): 33.60 - samples/sec: 149.19 - lr: 0.000002\n",
1077
+ "2023-09-20 16:47:31,759 epoch 7 - iter 54/95 - loss 0.44843613 - time (sec): 40.33 - samples/sec: 151.34 - lr: 0.000002\n",
1078
+ "2023-09-20 16:47:38,802 epoch 7 - iter 63/95 - loss 0.46794764 - time (sec): 47.37 - samples/sec: 150.33 - lr: 0.000002\n",
1079
+ "2023-09-20 16:47:45,076 epoch 7 - iter 72/95 - loss 0.46597196 - time (sec): 53.64 - samples/sec: 149.90 - lr: 0.000002\n",
1080
+ "2023-09-20 16:47:52,339 epoch 7 - iter 81/95 - loss 0.46348741 - time (sec): 60.91 - samples/sec: 149.61 - lr: 0.000002\n",
1081
+ "2023-09-20 16:47:59,025 epoch 7 - iter 90/95 - loss 0.45404421 - time (sec): 67.59 - samples/sec: 147.47 - lr: 0.000002\n",
1082
+ "2023-09-20 16:48:02,689 ----------------------------------------------------------------------------------------------------\n",
1083
+ "2023-09-20 16:48:02,690 EPOCH 7 done: loss 0.4593 - lr 0.000002\n"
1084
+ ]
1085
+ },
1086
+ {
1087
+ "output_type": "stream",
1088
+ "name": "stderr",
1089
+ "text": [
1090
+ "100%|██████████| 11/11 [00:02<00:00, 4.78it/s]"
1091
+ ]
1092
+ },
1093
+ {
1094
+ "output_type": "stream",
1095
+ "name": "stdout",
1096
+ "text": [
1097
+ "2023-09-20 16:48:05,006 Evaluating as a multi-label problem: False\n",
1098
+ "2023-09-20 16:48:05,029 DEV : loss 0.3640497028827667 - f1-score (micro avg) 0.2222\n",
1099
+ "2023-09-20 16:48:05,036 ----------------------------------------------------------------------------------------------------\n"
1100
+ ]
1101
+ },
1102
+ {
1103
+ "output_type": "stream",
1104
+ "name": "stderr",
1105
+ "text": [
1106
+ "\n"
1107
+ ]
1108
+ },
1109
+ {
1110
+ "output_type": "stream",
1111
+ "name": "stdout",
1112
+ "text": [
1113
+ "2023-09-20 16:48:12,140 epoch 8 - iter 9/95 - loss 0.38261461 - time (sec): 7.10 - samples/sec: 153.05 - lr: 0.000002\n",
1114
+ "2023-09-20 16:48:18,934 epoch 8 - iter 18/95 - loss 0.35649288 - time (sec): 13.90 - samples/sec: 151.41 - lr: 0.000002\n",
1115
+ "2023-09-20 16:48:25,579 epoch 8 - iter 27/95 - loss 0.39724913 - time (sec): 20.54 - samples/sec: 152.04 - lr: 0.000002\n",
1116
+ "2023-09-20 16:48:32,424 epoch 8 - iter 36/95 - loss 0.41548796 - time (sec): 27.39 - samples/sec: 149.82 - lr: 0.000001\n",
1117
+ "2023-09-20 16:48:38,844 epoch 8 - iter 45/95 - loss 0.38478886 - time (sec): 33.81 - samples/sec: 149.47 - lr: 0.000001\n",
1118
+ "2023-09-20 16:48:45,816 epoch 8 - iter 54/95 - loss 0.39225607 - time (sec): 40.78 - samples/sec: 148.39 - lr: 0.000001\n",
1119
+ "2023-09-20 16:48:52,568 epoch 8 - iter 63/95 - loss 0.37744603 - time (sec): 47.53 - samples/sec: 148.94 - lr: 0.000001\n",
1120
+ "2023-09-20 16:48:59,082 epoch 8 - iter 72/95 - loss 0.37978610 - time (sec): 54.04 - samples/sec: 146.72 - lr: 0.000001\n",
1121
+ "2023-09-20 16:49:06,514 epoch 8 - iter 81/95 - loss 0.39005255 - time (sec): 61.48 - samples/sec: 144.29 - lr: 0.000001\n",
1122
+ "2023-09-20 16:49:14,188 epoch 8 - iter 90/95 - loss 0.39827420 - time (sec): 69.15 - samples/sec: 143.73 - lr: 0.000001\n",
1123
+ "2023-09-20 16:49:17,638 ----------------------------------------------------------------------------------------------------\n",
1124
+ "2023-09-20 16:49:17,639 EPOCH 8 done: loss 0.4061 - lr 0.000001\n"
1125
+ ]
1126
+ },
1127
+ {
1128
+ "output_type": "stream",
1129
+ "name": "stderr",
1130
+ "text": [
1131
+ "100%|██████████| 11/11 [00:02<00:00, 4.85it/s]"
1132
+ ]
1133
+ },
1134
+ {
1135
+ "output_type": "stream",
1136
+ "name": "stdout",
1137
+ "text": [
1138
+ "2023-09-20 16:49:19,919 Evaluating as a multi-label problem: False\n",
1139
+ "2023-09-20 16:49:19,937 DEV : loss 0.3643392324447632 - f1-score (micro avg) 0.2313\n",
1140
+ "2023-09-20 16:49:19,941 ----------------------------------------------------------------------------------------------------\n"
1141
+ ]
1142
+ },
1143
+ {
1144
+ "output_type": "stream",
1145
+ "name": "stderr",
1146
+ "text": [
1147
+ "\n"
1148
+ ]
1149
+ },
1150
+ {
1151
+ "output_type": "stream",
1152
+ "name": "stdout",
1153
+ "text": [
1154
+ "2023-09-20 16:49:26,803 epoch 9 - iter 9/95 - loss 0.37628733 - time (sec): 6.86 - samples/sec: 157.70 - lr: 0.000001\n",
1155
+ "2023-09-20 16:49:33,861 epoch 9 - iter 18/95 - loss 0.34309120 - time (sec): 13.92 - samples/sec: 146.92 - lr: 0.000001\n",
1156
+ "2023-09-20 16:49:40,349 epoch 9 - iter 27/95 - loss 0.36862108 - time (sec): 20.41 - samples/sec: 149.51 - lr: 0.000001\n",
1157
+ "2023-09-20 16:49:47,393 epoch 9 - iter 36/95 - loss 0.37854449 - time (sec): 27.45 - samples/sec: 146.22 - lr: 0.000001\n",
1158
+ "2023-09-20 16:49:54,443 epoch 9 - iter 45/95 - loss 0.38543222 - time (sec): 34.50 - samples/sec: 142.90 - lr: 0.000001\n",
1159
+ "2023-09-20 16:50:01,292 epoch 9 - iter 54/95 - loss 0.37676979 - time (sec): 41.35 - samples/sec: 143.53 - lr: 0.000001\n",
1160
+ "2023-09-20 16:50:08,679 epoch 9 - iter 63/95 - loss 0.37714998 - time (sec): 48.74 - samples/sec: 141.62 - lr: 0.000001\n",
1161
+ "2023-09-20 16:50:15,118 epoch 9 - iter 72/95 - loss 0.37534464 - time (sec): 55.18 - samples/sec: 143.76 - lr: 0.000001\n",
1162
+ "2023-09-20 16:50:21,880 epoch 9 - iter 81/95 - loss 0.37937109 - time (sec): 61.94 - samples/sec: 143.52 - lr: 0.000001\n",
1163
+ "2023-09-20 16:50:29,147 epoch 9 - iter 90/95 - loss 0.37920452 - time (sec): 69.20 - samples/sec: 143.50 - lr: 0.000001\n",
1164
+ "2023-09-20 16:50:32,552 ----------------------------------------------------------------------------------------------------\n",
1165
+ "2023-09-20 16:50:32,553 EPOCH 9 done: loss 0.3825 - lr 0.000001\n"
1166
+ ]
1167
+ },
1168
+ {
1169
+ "output_type": "stream",
1170
+ "name": "stderr",
1171
+ "text": [
1172
+ "100%|██████████| 11/11 [00:02<00:00, 4.82it/s]"
1173
+ ]
1174
+ },
1175
+ {
1176
+ "output_type": "stream",
1177
+ "name": "stdout",
1178
+ "text": [
1179
+ "2023-09-20 16:50:34,849 Evaluating as a multi-label problem: False\n",
1180
+ "2023-09-20 16:50:34,864 DEV : loss 0.3552079200744629 - f1-score (micro avg) 0.2252\n",
1181
+ "2023-09-20 16:50:34,868 ----------------------------------------------------------------------------------------------------\n"
1182
+ ]
1183
+ },
1184
+ {
1185
+ "output_type": "stream",
1186
+ "name": "stderr",
1187
+ "text": [
1188
+ "\n"
1189
+ ]
1190
+ },
1191
+ {
1192
+ "output_type": "stream",
1193
+ "name": "stdout",
1194
+ "text": [
1195
+ "2023-09-20 16:50:41,507 epoch 10 - iter 9/95 - loss 0.23978793 - time (sec): 6.64 - samples/sec: 164.84 - lr: 0.000001\n",
1196
+ "2023-09-20 16:50:48,591 epoch 10 - iter 18/95 - loss 0.26464288 - time (sec): 13.72 - samples/sec: 146.78 - lr: 0.000000\n",
1197
+ "2023-09-20 16:50:54,870 epoch 10 - iter 27/95 - loss 0.30564030 - time (sec): 20.00 - samples/sec: 154.20 - lr: 0.000000\n",
1198
+ "2023-09-20 16:51:02,035 epoch 10 - iter 36/95 - loss 0.32322471 - time (sec): 27.17 - samples/sec: 146.51 - lr: 0.000000\n",
1199
+ "2023-09-20 16:51:08,927 epoch 10 - iter 45/95 - loss 0.34168939 - time (sec): 34.06 - samples/sec: 144.14 - lr: 0.000000\n",
1200
+ "2023-09-20 16:51:15,553 epoch 10 - iter 54/95 - loss 0.34366191 - time (sec): 40.68 - samples/sec: 146.10 - lr: 0.000000\n",
1201
+ "2023-09-20 16:51:22,584 epoch 10 - iter 63/95 - loss 0.33181748 - time (sec): 47.71 - samples/sec: 144.84 - lr: 0.000000\n",
1202
+ "2023-09-20 16:51:29,148 epoch 10 - iter 72/95 - loss 0.34679468 - time (sec): 54.28 - samples/sec: 145.27 - lr: 0.000000\n",
1203
+ "2023-09-20 16:51:35,756 epoch 10 - iter 81/95 - loss 0.35531872 - time (sec): 60.89 - samples/sec: 145.27 - lr: 0.000000\n",
1204
+ "2023-09-20 16:51:42,815 epoch 10 - iter 90/95 - loss 0.36415594 - time (sec): 67.94 - samples/sec: 145.62 - lr: 0.000000\n",
1205
+ "2023-09-20 16:51:46,067 ----------------------------------------------------------------------------------------------------\n",
1206
+ "2023-09-20 16:51:46,068 EPOCH 10 done: loss 0.3679 - lr 0.000000\n"
1207
+ ]
1208
+ },
1209
+ {
1210
+ "output_type": "stream",
1211
+ "name": "stderr",
1212
+ "text": [
1213
+ "100%|██████████| 11/11 [00:02<00:00, 4.83it/s]"
1214
+ ]
1215
+ },
1216
+ {
1217
+ "output_type": "stream",
1218
+ "name": "stdout",
1219
+ "text": [
1220
+ "2023-09-20 16:51:48,360 Evaluating as a multi-label problem: False\n",
1221
+ "2023-09-20 16:51:48,376 DEV : loss 0.35039085149765015 - f1-score (micro avg) 0.2313\n"
1222
+ ]
1223
+ },
1224
+ {
1225
+ "output_type": "stream",
1226
+ "name": "stderr",
1227
+ "text": [
1228
+ "\n"
1229
+ ]
1230
+ },
1231
+ {
1232
+ "output_type": "stream",
1233
+ "name": "stdout",
1234
+ "text": [
1235
+ "2023-09-20 16:51:58,375 ----------------------------------------------------------------------------------------------------\n",
1236
+ "2023-09-20 16:51:58,380 Testing using last state of model ...\n"
1237
+ ]
1238
+ },
1239
+ {
1240
+ "output_type": "stream",
1241
+ "name": "stderr",
1242
+ "text": [
1243
+ "100%|██████████| 27/27 [00:05<00:00, 5.28it/s]"
1244
+ ]
1245
+ },
1246
+ {
1247
+ "output_type": "stream",
1248
+ "name": "stdout",
1249
+ "text": [
1250
+ "2023-09-20 16:52:03,500 Evaluating as a multi-label problem: False\n",
1251
+ "2023-09-20 16:52:03,518 0.367\t0.3941\t0.38\t0.2424\n",
1252
+ "2023-09-20 16:52:03,519 \n",
1253
+ "Results:\n",
1254
+ "- F-score (micro) 0.38\n",
1255
+ "- F-score (macro) 0.271\n",
1256
+ "- Accuracy 0.2424\n",
1257
+ "\n",
1258
+ "By class:\n",
1259
+ " precision recall f1-score support\n",
1260
+ "\n",
1261
+ " PER 0.2029 0.3256 0.2500 43\n",
1262
+ " VIO 0.4894 0.6216 0.5476 37\n",
1263
+ " ORG 0.4524 0.5429 0.4935 35\n",
1264
+ " GEO 0.4222 0.6333 0.5067 30\n",
1265
+ " ARM 0.5556 0.2778 0.3704 18\n",
1266
+ " PAZ 0.0000 0.0000 0.0000 19\n",
1267
+ " AFE 0.0000 0.0000 0.0000 11\n",
1268
+ " DATE 0.0000 0.0000 0.0000 10\n",
1269
+ "\n",
1270
+ " micro avg 0.3670 0.3941 0.3800 203\n",
1271
+ " macro avg 0.2653 0.3001 0.2710 203\n",
1272
+ "weighted avg 0.3218 0.3941 0.3456 203\n",
1273
+ "\n",
1274
+ "2023-09-20 16:52:03,521 ----------------------------------------------------------------------------------------------------\n"
1275
+ ]
1276
+ },
1277
+ {
1278
+ "output_type": "stream",
1279
+ "name": "stderr",
1280
+ "text": [
1281
+ "\n"
1282
+ ]
1283
+ },
1284
+ {
1285
+ "output_type": "execute_result",
1286
+ "data": {
1287
+ "text/plain": [
1288
+ "{'test_score': 0.3800475059382424,\n",
1289
+ " 'dev_score_history': [0.0,\n",
1290
+ " 0.0,\n",
1291
+ " 0.0,\n",
1292
+ " 0.0,\n",
1293
+ " 0.02564102564102564,\n",
1294
+ " 0.2285714285714286,\n",
1295
+ " 0.22222222222222224,\n",
1296
+ " 0.23129251700680276,\n",
1297
+ " 0.2251655629139073,\n",
1298
+ " 0.23129251700680276],\n",
1299
+ " 'train_loss_history': [2.2083096445195376,\n",
1300
+ " 0.7905150630989667,\n",
1301
+ " 0.6759139882288138,\n",
1302
+ " 0.6174707539115155,\n",
1303
+ " 0.5402019214430303,\n",
1304
+ " 0.49300472509990656,\n",
1305
+ " 0.4592541911305255,\n",
1306
+ " 0.4060959751833173,\n",
1307
+ " 0.3825031868616195,\n",
1308
+ " 0.3679287539225993],\n",
1309
+ " 'dev_loss_history': [0.6564602851867676,\n",
1310
+ " 0.5198074579238892,\n",
1311
+ " 0.4649089276790619,\n",
1312
+ " 0.4446188509464264,\n",
1313
+ " 0.4345150291919708,\n",
1314
+ " 0.38467857241630554,\n",
1315
+ " 0.3640497028827667,\n",
1316
+ " 0.3643392324447632,\n",
1317
+ " 0.3552079200744629,\n",
1318
+ " 0.35039085149765015]}"
1319
+ ]
1320
+ },
1321
+ "metadata": {},
1322
+ "execution_count": 12
1323
+ }
1324
+ ]
1325
+ },
1326
+ {
1327
+ "cell_type": "code",
1328
+ "source": [
1329
+ "# load the trained model\n",
1330
+ "model = SequenceTagger.load('/content/resources/taggers/sota-ner-flert/final-model.pt')\n",
1331
+ "\n",
1332
+ "# create example sentence\n",
1333
+ "sentence = Sentence('Los principales perpetradores de esta violación fueron los paramilitares seguidos de guerrillas como las FARC y el ELN y fuerza pública y otros agentes del Estado como los organismos de inteligencia')# predict the tags\n",
1334
+ "sentence2 = Sentence('Hola. Mi nombre es David Cortés, tengo 27 años de edad y soy de Tumaco, Nariño. Llegué a Bogotá cuando tenía 17 años por temas de seguridad. Mi mamá me envío a esta ciudad porque mi vida estaba en riesgo luego de que, por accidente, fuera testigo de la muerte de un narcotraficante. Todo el barrio estaba lleno de narcos')\n",
1335
+ "sentence3 = Sentence('Mi mamá me envío a esta ciudad porque mi vida estaba en riesgo luego de que, por accidente, fuera testigo de la muerte de un narcotraficante. Todo el barrio estaba lleno de narcos')\n",
1336
+ "model.predict(sentence)\n",
1337
+ "model.predict(sentence2)\n",
1338
+ "model.predict(sentence3)\n",
1339
+ "\n",
1340
+ "print(sentence.to_tagged_string())\n",
1341
+ "print(sentence2.to_tagged_string())\n",
1342
+ "print(sentence3.to_tagged_string())"
1343
+ ],
1344
+ "metadata": {
1345
+ "id": "l0dN82neXCdp"
1346
+ },
1347
+ "execution_count": null,
1348
+ "outputs": []
1349
+ }
1350
+ ]
1351
+ }
Prueba_P_Flair.ipynb ADDED
@@ -0,0 +1,1660 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ {
2
+ "nbformat": 4,
3
+ "nbformat_minor": 0,
4
+ "metadata": {
5
+ "colab": {
6
+ "provenance": [],
7
+ "gpuType": "T4"
8
+ },
9
+ "kernelspec": {
10
+ "name": "python3",
11
+ "display_name": "Python 3"
12
+ },
13
+ "language_info": {
14
+ "name": "python"
15
+ },
16
+ "accelerator": "GPU"
17
+ },
18
+ "cells": [
19
+ {
20
+ "cell_type": "code",
21
+ "execution_count": null,
22
+ "metadata": {
23
+ "colab": {
24
+ "base_uri": "https://localhost:8080/"
25
+ },
26
+ "id": "RoNR4s7BT6Z9",
27
+ "outputId": "38151f0c-7420-403f-edf0-cb7cb9237ba9"
28
+ },
29
+ "outputs": [
30
+ {
31
+ "output_type": "stream",
32
+ "name": "stdout",
33
+ "text": [
34
+ "Collecting flair\n",
35
+ " Downloading flair-0.13.0-py3-none-any.whl (387 kB)\n",
36
+ "\u001b[2K \u001b[90m━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━\u001b[0m \u001b[32m387.2/387.2 kB\u001b[0m \u001b[31m6.5 MB/s\u001b[0m eta \u001b[36m0:00:00\u001b[0m\n",
37
+ "\u001b[?25hCollecting boto3>=1.20.27 (from flair)\n",
38
+ " Downloading boto3-1.28.83-py3-none-any.whl (135 kB)\n",
39
+ "\u001b[2K \u001b[90m━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━\u001b[0m \u001b[32m135.8/135.8 kB\u001b[0m \u001b[31m6.4 MB/s\u001b[0m eta \u001b[36m0:00:00\u001b[0m\n",
40
+ "\u001b[?25hCollecting bpemb>=0.3.2 (from flair)\n",
41
+ " Downloading bpemb-0.3.4-py3-none-any.whl (19 kB)\n",
42
+ "Collecting conllu>=4.0 (from flair)\n",
43
+ " Downloading conllu-4.5.3-py2.py3-none-any.whl (16 kB)\n",
44
+ "Collecting deprecated>=1.2.13 (from flair)\n",
45
+ " Downloading Deprecated-1.2.14-py2.py3-none-any.whl (9.6 kB)\n",
46
+ "Collecting ftfy>=6.1.0 (from flair)\n",
47
+ " Downloading ftfy-6.1.1-py3-none-any.whl (53 kB)\n",
48
+ "\u001b[2K \u001b[90m━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━\u001b[0m \u001b[32m53.1/53.1 kB\u001b[0m \u001b[31m7.7 MB/s\u001b[0m eta \u001b[36m0:00:00\u001b[0m\n",
49
+ "\u001b[?25hRequirement already satisfied: gdown>=4.4.0 in /usr/local/lib/python3.10/dist-packages (from flair) (4.6.6)\n",
50
+ "Requirement already satisfied: gensim>=4.2.0 in /usr/local/lib/python3.10/dist-packages (from flair) (4.3.2)\n",
51
+ "Collecting huggingface-hub>=0.10.0 (from flair)\n",
52
+ " Downloading huggingface_hub-0.19.0-py3-none-any.whl (311 kB)\n",
53
+ "\u001b[2K \u001b[90m━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━\u001b[0m \u001b[32m311.2/311.2 kB\u001b[0m \u001b[31m9.9 MB/s\u001b[0m eta \u001b[36m0:00:00\u001b[0m\n",
54
+ "\u001b[?25hCollecting janome>=0.4.2 (from flair)\n",
55
+ " Downloading Janome-0.5.0-py2.py3-none-any.whl (19.7 MB)\n",
56
+ "\u001b[2K \u001b[90m━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━\u001b[0m \u001b[32m19.7/19.7 MB\u001b[0m \u001b[31m40.2 MB/s\u001b[0m eta \u001b[36m0:00:00\u001b[0m\n",
57
+ "\u001b[?25hCollecting langdetect>=1.0.9 (from flair)\n",
58
+ " Downloading langdetect-1.0.9.tar.gz (981 kB)\n",
59
+ "\u001b[2K \u001b[90m━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━\u001b[0m \u001b[32m981.5/981.5 kB\u001b[0m \u001b[31m68.5 MB/s\u001b[0m eta \u001b[36m0:00:00\u001b[0m\n",
60
+ "\u001b[?25h Preparing metadata (setup.py) ... \u001b[?25l\u001b[?25hdone\n",
61
+ "Requirement already satisfied: lxml>=4.8.0 in /usr/local/lib/python3.10/dist-packages (from flair) (4.9.3)\n",
62
+ "Requirement already satisfied: matplotlib>=2.2.3 in /usr/local/lib/python3.10/dist-packages (from flair) (3.7.1)\n",
63
+ "Requirement already satisfied: more-itertools>=8.13.0 in /usr/local/lib/python3.10/dist-packages (from flair) (10.1.0)\n",
64
+ "Collecting mpld3>=0.3 (from flair)\n",
65
+ " Downloading mpld3-0.5.9-py3-none-any.whl (201 kB)\n",
66
+ "\u001b[2K \u001b[90m━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━\u001b[0m \u001b[32m201.2/201.2 kB\u001b[0m \u001b[31m25.5 MB/s\u001b[0m eta \u001b[36m0:00:00\u001b[0m\n",
67
+ "\u001b[?25hCollecting pptree>=3.1 (from flair)\n",
68
+ " Downloading pptree-3.1.tar.gz (3.0 kB)\n",
69
+ " Preparing metadata (setup.py) ... \u001b[?25l\u001b[?25hdone\n",
70
+ "Requirement already satisfied: python-dateutil>=2.8.2 in /usr/local/lib/python3.10/dist-packages (from flair) (2.8.2)\n",
71
+ "Collecting pytorch-revgrad>=0.2.0 (from flair)\n",
72
+ " Downloading pytorch_revgrad-0.2.0-py3-none-any.whl (4.6 kB)\n",
73
+ "Requirement already satisfied: regex>=2022.1.18 in /usr/local/lib/python3.10/dist-packages (from flair) (2023.6.3)\n",
74
+ "Requirement already satisfied: scikit-learn>=1.0.2 in /usr/local/lib/python3.10/dist-packages (from flair) (1.2.2)\n",
75
+ "Collecting segtok>=1.5.11 (from flair)\n",
76
+ " Downloading segtok-1.5.11-py3-none-any.whl (24 kB)\n",
77
+ "Collecting sqlitedict>=2.0.0 (from flair)\n",
78
+ " Downloading sqlitedict-2.1.0.tar.gz (21 kB)\n",
79
+ " Preparing metadata (setup.py) ... \u001b[?25l\u001b[?25hdone\n",
80
+ "Requirement already satisfied: tabulate>=0.8.10 in /usr/local/lib/python3.10/dist-packages (from flair) (0.9.0)\n",
81
+ "Requirement already satisfied: torch!=1.8,>=1.5.0 in /usr/local/lib/python3.10/dist-packages (from flair) (2.1.0+cu118)\n",
82
+ "Requirement already satisfied: tqdm>=4.63.0 in /usr/local/lib/python3.10/dist-packages (from flair) (4.66.1)\n",
83
+ "Collecting transformer-smaller-training-vocab>=0.2.3 (from flair)\n",
84
+ " Downloading transformer_smaller_training_vocab-0.3.2-py3-none-any.whl (14 kB)\n",
85
+ "Collecting transformers[sentencepiece]<5.0.0,>=4.18.0 (from flair)\n",
86
+ " Downloading transformers-4.35.0-py3-none-any.whl (7.9 MB)\n",
87
+ "\u001b[2K \u001b[90m━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━\u001b[0m \u001b[32m7.9/7.9 MB\u001b[0m \u001b[31m66.3 MB/s\u001b[0m eta \u001b[36m0:00:00\u001b[0m\n",
88
+ "\u001b[?25hCollecting urllib3<2.0.0,>=1.0.0 (from flair)\n",
89
+ " Downloading urllib3-1.26.18-py2.py3-none-any.whl (143 kB)\n",
90
+ "\u001b[2K \u001b[90m━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━\u001b[0m \u001b[32m143.8/143.8 kB\u001b[0m \u001b[31m18.1 MB/s\u001b[0m eta \u001b[36m0:00:00\u001b[0m\n",
91
+ "\u001b[?25hCollecting wikipedia-api>=0.5.7 (from flair)\n",
92
+ " Downloading Wikipedia_API-0.6.0-py3-none-any.whl (14 kB)\n",
93
+ "Collecting semver<4.0.0,>=3.0.0 (from flair)\n",
94
+ " Downloading semver-3.0.2-py3-none-any.whl (17 kB)\n",
95
+ "Collecting botocore<1.32.0,>=1.31.83 (from boto3>=1.20.27->flair)\n",
96
+ " Downloading botocore-1.31.84-py3-none-any.whl (11.3 MB)\n",
97
+ "\u001b[2K \u001b[90m━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━\u001b[0m \u001b[32m11.3/11.3 MB\u001b[0m \u001b[31m49.0 MB/s\u001b[0m eta \u001b[36m0:00:00\u001b[0m\n",
98
+ "\u001b[?25hCollecting jmespath<2.0.0,>=0.7.1 (from boto3>=1.20.27->flair)\n",
99
+ " Downloading jmespath-1.0.1-py3-none-any.whl (20 kB)\n",
100
+ "Collecting s3transfer<0.8.0,>=0.7.0 (from boto3>=1.20.27->flair)\n",
101
+ " Downloading s3transfer-0.7.0-py3-none-any.whl (79 kB)\n",
102
+ "\u001b[2K \u001b[90m━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━\u001b[0m \u001b[32m79.8/79.8 kB\u001b[0m \u001b[31m12.7 MB/s\u001b[0m eta \u001b[36m0:00:00\u001b[0m\n",
103
+ "\u001b[?25hRequirement already satisfied: numpy in /usr/local/lib/python3.10/dist-packages (from bpemb>=0.3.2->flair) (1.23.5)\n",
104
+ "Requirement already satisfied: requests in /usr/local/lib/python3.10/dist-packages (from bpemb>=0.3.2->flair) (2.31.0)\n",
105
+ "Collecting sentencepiece (from bpemb>=0.3.2->flair)\n",
106
+ " Downloading sentencepiece-0.1.99-cp310-cp310-manylinux_2_17_x86_64.manylinux2014_x86_64.whl (1.3 MB)\n",
107
+ "\u001b[2K \u001b[90m━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━\u001b[0m \u001b[32m1.3/1.3 MB\u001b[0m \u001b[31m92.0 MB/s\u001b[0m eta \u001b[36m0:00:00\u001b[0m\n",
108
+ "\u001b[?25hRequirement already satisfied: wrapt<2,>=1.10 in /usr/local/lib/python3.10/dist-packages (from deprecated>=1.2.13->flair) (1.14.1)\n",
109
+ "Requirement already satisfied: wcwidth>=0.2.5 in /usr/local/lib/python3.10/dist-packages (from ftfy>=6.1.0->flair) (0.2.9)\n",
110
+ "Requirement already satisfied: filelock in /usr/local/lib/python3.10/dist-packages (from gdown>=4.4.0->flair) (3.13.1)\n",
111
+ "Requirement already satisfied: six in /usr/local/lib/python3.10/dist-packages (from gdown>=4.4.0->flair) (1.16.0)\n",
112
+ "Requirement already satisfied: beautifulsoup4 in /usr/local/lib/python3.10/dist-packages (from gdown>=4.4.0->flair) (4.11.2)\n",
113
+ "Requirement already satisfied: scipy>=1.7.0 in /usr/local/lib/python3.10/dist-packages (from gensim>=4.2.0->flair) (1.11.3)\n",
114
+ "Requirement already satisfied: smart-open>=1.8.1 in /usr/local/lib/python3.10/dist-packages (from gensim>=4.2.0->flair) (6.4.0)\n",
115
+ "Requirement already satisfied: fsspec>=2023.5.0 in /usr/local/lib/python3.10/dist-packages (from huggingface-hub>=0.10.0->flair) (2023.6.0)\n",
116
+ "Requirement already satisfied: pyyaml>=5.1 in /usr/local/lib/python3.10/dist-packages (from huggingface-hub>=0.10.0->flair) (6.0.1)\n",
117
+ "Requirement already satisfied: typing-extensions>=3.7.4.3 in /usr/local/lib/python3.10/dist-packages (from huggingface-hub>=0.10.0->flair) (4.5.0)\n",
118
+ "Requirement already satisfied: packaging>=20.9 in /usr/local/lib/python3.10/dist-packages (from huggingface-hub>=0.10.0->flair) (23.2)\n",
119
+ "Requirement already satisfied: contourpy>=1.0.1 in /usr/local/lib/python3.10/dist-packages (from matplotlib>=2.2.3->flair) (1.2.0)\n",
120
+ "Requirement already satisfied: cycler>=0.10 in /usr/local/lib/python3.10/dist-packages (from matplotlib>=2.2.3->flair) (0.12.1)\n",
121
+ "Requirement already satisfied: fonttools>=4.22.0 in /usr/local/lib/python3.10/dist-packages (from matplotlib>=2.2.3->flair) (4.44.0)\n",
122
+ "Requirement already satisfied: kiwisolver>=1.0.1 in /usr/local/lib/python3.10/dist-packages (from matplotlib>=2.2.3->flair) (1.4.5)\n",
123
+ "Requirement already satisfied: pillow>=6.2.0 in /usr/local/lib/python3.10/dist-packages (from matplotlib>=2.2.3->flair) (9.4.0)\n",
124
+ "Requirement already satisfied: pyparsing>=2.3.1 in /usr/local/lib/python3.10/dist-packages (from matplotlib>=2.2.3->flair) (3.1.1)\n",
125
+ "Requirement already satisfied: jinja2 in /usr/local/lib/python3.10/dist-packages (from mpld3>=0.3->flair) (3.1.2)\n",
126
+ "Requirement already satisfied: joblib>=1.1.1 in /usr/local/lib/python3.10/dist-packages (from scikit-learn>=1.0.2->flair) (1.3.2)\n",
127
+ "Requirement already satisfied: threadpoolctl>=2.0.0 in /usr/local/lib/python3.10/dist-packages (from scikit-learn>=1.0.2->flair) (3.2.0)\n",
128
+ "Requirement already satisfied: sympy in /usr/local/lib/python3.10/dist-packages (from torch!=1.8,>=1.5.0->flair) (1.12)\n",
129
+ "Requirement already satisfied: networkx in /usr/local/lib/python3.10/dist-packages (from torch!=1.8,>=1.5.0->flair) (3.2.1)\n",
130
+ "Requirement already satisfied: triton==2.1.0 in /usr/local/lib/python3.10/dist-packages (from torch!=1.8,>=1.5.0->flair) (2.1.0)\n",
131
+ "Collecting tokenizers<0.15,>=0.14 (from transformers[sentencepiece]<5.0.0,>=4.18.0->flair)\n",
132
+ " Downloading tokenizers-0.14.1-cp310-cp310-manylinux_2_17_x86_64.manylinux2014_x86_64.whl (3.8 MB)\n",
133
+ "\u001b[2K \u001b[90m━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━\u001b[0m \u001b[32m3.8/3.8 MB\u001b[0m \u001b[31m115.0 MB/s\u001b[0m eta \u001b[36m0:00:00\u001b[0m\n",
134
+ "\u001b[?25hCollecting safetensors>=0.3.1 (from transformers[sentencepiece]<5.0.0,>=4.18.0->flair)\n",
135
+ " Downloading safetensors-0.4.0-cp310-cp310-manylinux_2_17_x86_64.manylinux2014_x86_64.whl (1.3 MB)\n",
136
+ "\u001b[2K \u001b[90m━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━\u001b[0m \u001b[32m1.3/1.3 MB\u001b[0m \u001b[31m89.2 MB/s\u001b[0m eta \u001b[36m0:00:00\u001b[0m\n",
137
+ "\u001b[?25hRequirement already satisfied: protobuf in /usr/local/lib/python3.10/dist-packages (from transformers[sentencepiece]<5.0.0,>=4.18.0->flair) (3.20.3)\n",
138
+ "Collecting huggingface-hub>=0.10.0 (from flair)\n",
139
+ " Downloading huggingface_hub-0.17.3-py3-none-any.whl (295 kB)\n",
140
+ "\u001b[2K \u001b[90m━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━\u001b[0m \u001b[32m295.0/295.0 kB\u001b[0m \u001b[31m35.4 MB/s\u001b[0m eta \u001b[36m0:00:00\u001b[0m\n",
141
+ "\u001b[?25hCollecting accelerate>=0.20.3 (from transformers[sentencepiece]<5.0.0,>=4.18.0->flair)\n",
142
+ " Downloading accelerate-0.24.1-py3-none-any.whl (261 kB)\n",
143
+ "\u001b[2K \u001b[90m━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━\u001b[0m \u001b[32m261.4/261.4 kB\u001b[0m \u001b[31m31.1 MB/s\u001b[0m eta \u001b[36m0:00:00\u001b[0m\n",
144
+ "\u001b[?25hRequirement already satisfied: soupsieve>1.2 in /usr/local/lib/python3.10/dist-packages (from beautifulsoup4->gdown>=4.4.0->flair) (2.5)\n",
145
+ "Requirement already satisfied: MarkupSafe>=2.0 in /usr/local/lib/python3.10/dist-packages (from jinja2->mpld3>=0.3->flair) (2.1.3)\n",
146
+ "Requirement already satisfied: charset-normalizer<4,>=2 in /usr/local/lib/python3.10/dist-packages (from requests->bpemb>=0.3.2->flair) (3.3.2)\n",
147
+ "Requirement already satisfied: idna<4,>=2.5 in /usr/local/lib/python3.10/dist-packages (from requests->bpemb>=0.3.2->flair) (3.4)\n",
148
+ "Requirement already satisfied: certifi>=2017.4.17 in /usr/local/lib/python3.10/dist-packages (from requests->bpemb>=0.3.2->flair) (2023.7.22)\n",
149
+ "Requirement already satisfied: PySocks!=1.5.7,>=1.5.6 in /usr/local/lib/python3.10/dist-packages (from requests->bpemb>=0.3.2->flair) (1.7.1)\n",
150
+ "Requirement already satisfied: mpmath>=0.19 in /usr/local/lib/python3.10/dist-packages (from sympy->torch!=1.8,>=1.5.0->flair) (1.3.0)\n",
151
+ "Requirement already satisfied: psutil in /usr/local/lib/python3.10/dist-packages (from accelerate>=0.20.3->transformers[sentencepiece]<5.0.0,>=4.18.0->flair) (5.9.5)\n",
152
+ "Building wheels for collected packages: langdetect, pptree, sqlitedict\n",
153
+ " Building wheel for langdetect (setup.py) ... \u001b[?25l\u001b[?25hdone\n",
154
+ " Created wheel for langdetect: filename=langdetect-1.0.9-py3-none-any.whl size=993224 sha256=c1fdf2da43cd28e93dd14524a66cc9aad946f45a274627627b8f92c3562ff31a\n",
155
+ " Stored in directory: /root/.cache/pip/wheels/95/03/7d/59ea870c70ce4e5a370638b5462a7711ab78fba2f655d05106\n",
156
+ " Building wheel for pptree (setup.py) ... \u001b[?25l\u001b[?25hdone\n",
157
+ " Created wheel for pptree: filename=pptree-3.1-py3-none-any.whl size=4607 sha256=9d983536826cfe539a4252c0906794af572cb1cc3e93afa20bc51ddd02e6b859\n",
158
+ " Stored in directory: /root/.cache/pip/wheels/9f/b6/0e/6f26eb9e6eb53ff2107a7888d72b5a6a597593956113037828\n",
159
+ " Building wheel for sqlitedict (setup.py) ... \u001b[?25l\u001b[?25hdone\n",
160
+ " Created wheel for sqlitedict: filename=sqlitedict-2.1.0-py3-none-any.whl size=16863 sha256=b2057820ab5f87e156c67eaf350698fa195e576320012ead0b37117ffee1ed5d\n",
161
+ " Stored in directory: /root/.cache/pip/wheels/79/d6/e7/304e0e6cb2221022c26d8161f7c23cd4f259a9e41e8bbcfabd\n",
162
+ "Successfully built langdetect pptree sqlitedict\n",
163
+ "Installing collected packages: sqlitedict, sentencepiece, pptree, janome, urllib3, semver, segtok, safetensors, langdetect, jmespath, ftfy, deprecated, conllu, botocore, wikipedia-api, s3transfer, pytorch-revgrad, mpld3, huggingface-hub, bpemb, tokenizers, boto3, accelerate, transformers, transformer-smaller-training-vocab, flair\n",
164
+ " Attempting uninstall: urllib3\n",
165
+ " Found existing installation: urllib3 2.0.7\n",
166
+ " Uninstalling urllib3-2.0.7:\n",
167
+ " Successfully uninstalled urllib3-2.0.7\n",
168
+ "Successfully installed accelerate-0.24.1 boto3-1.28.83 botocore-1.31.84 bpemb-0.3.4 conllu-4.5.3 deprecated-1.2.14 flair-0.13.0 ftfy-6.1.1 huggingface-hub-0.17.3 janome-0.5.0 jmespath-1.0.1 langdetect-1.0.9 mpld3-0.5.9 pptree-3.1 pytorch-revgrad-0.2.0 s3transfer-0.7.0 safetensors-0.4.0 segtok-1.5.11 semver-3.0.2 sentencepiece-0.1.99 sqlitedict-2.1.0 tokenizers-0.14.1 transformer-smaller-training-vocab-0.3.2 transformers-4.35.0 urllib3-1.26.18 wikipedia-api-0.6.0\n"
169
+ ]
170
+ }
171
+ ],
172
+ "source": [
173
+ "!pip install flair"
174
+ ]
175
+ },
176
+ {
177
+ "cell_type": "code",
178
+ "source": [
179
+ "import pandas as pd\n",
180
+ "import matplotlib.pyplot as plt\n",
181
+ "\n",
182
+ "from flair.data import Corpus, Sentence\n",
183
+ "from flair.datasets import ColumnCorpus\n",
184
+ "from flair.models import SequenceTagger\n",
185
+ "from flair.embeddings import WordEmbeddings, StackedEmbeddings, FlairEmbeddings#, BertEmbeddings\n",
186
+ "from flair.trainers import ModelTrainer\n",
187
+ "\n",
188
+ "from typing import List\n",
189
+ "\n",
190
+ "from sklearn.model_selection import train_test_split"
191
+ ],
192
+ "metadata": {
193
+ "id": "njeVg7J5UE8j"
194
+ },
195
+ "execution_count": null,
196
+ "outputs": []
197
+ },
198
+ {
199
+ "cell_type": "code",
200
+ "source": [
201
+ "from google.colab import drive\n",
202
+ "drive.mount('/content/drive')"
203
+ ],
204
+ "metadata": {
205
+ "colab": {
206
+ "base_uri": "https://localhost:8080/"
207
+ },
208
+ "id": "VljbnVFJbsg1",
209
+ "outputId": "7df3f664-d52e-4356-dc47-15884c6baae3"
210
+ },
211
+ "execution_count": null,
212
+ "outputs": [
213
+ {
214
+ "output_type": "stream",
215
+ "name": "stdout",
216
+ "text": [
217
+ "Mounted at /content/drive\n"
218
+ ]
219
+ }
220
+ ]
221
+ },
222
+ {
223
+ "cell_type": "code",
224
+ "source": [
225
+ "in_path = '/content/drive/MyDrive/Proyecto_ConflictoArmado/NER/output.txt'"
226
+ ],
227
+ "metadata": {
228
+ "id": "3euqI4U9V3GY"
229
+ },
230
+ "execution_count": null,
231
+ "outputs": []
232
+ },
233
+ {
234
+ "cell_type": "code",
235
+ "source": [
236
+ "import pandas as pd\n",
237
+ "\n",
238
+ "# Abre el archivo de texto en modo lectura\n",
239
+ "with open(in_path, 'r', encoding='utf-8') as file:\n",
240
+ " # Lee las líneas del archivo\n",
241
+ " lines = file.readlines()\n",
242
+ "\n",
243
+ "# Inicializa el DataFrame\n",
244
+ "data = {'sentence_id': [], 'text': [], 'tag': []}\n",
245
+ "\n",
246
+ "# Inicializa el identificador de oración\n",
247
+ "sentence_id = 1\n",
248
+ "\n",
249
+ "# Procesa cada línea del archivo\n",
250
+ "for line in lines:\n",
251
+ " line = line.strip() # Elimina espacios en blanco al principio y al final de la línea\n",
252
+ " if line: # Si la línea no está en blanco\n",
253
+ " parts = line.split() # Divide la línea en palabras y etiquetas\n",
254
+ " if len(parts) == 2:\n",
255
+ " word, tag = parts\n",
256
+ " data['sentence_id'].append(sentence_id)\n",
257
+ " data['text'].append(word)\n",
258
+ " data['tag'].append(tag)\n",
259
+ " else:\n",
260
+ " # Handle empty lines\n",
261
+ " data['sentence_id'].append(None)\n",
262
+ " data['text'].append(None)\n",
263
+ " data['tag'].append(None)\n",
264
+ " else:\n",
265
+ " # Incrementa el identificador de oración al encontrar una línea en blanco\n",
266
+ " sentence_id += 1\n",
267
+ "\n",
268
+ "# Crea el DataFrame\n",
269
+ "df = pd.DataFrame(data)\n",
270
+ "\n",
271
+ "# Imprime el DataFrame\n",
272
+ "df"
273
+ ],
274
+ "metadata": {
275
+ "colab": {
276
+ "base_uri": "https://localhost:8080/",
277
+ "height": 424
278
+ },
279
+ "id": "tFD-UpFRWfat",
280
+ "outputId": "6918253e-e70f-4571-dcdc-800434dc243e"
281
+ },
282
+ "execution_count": null,
283
+ "outputs": [
284
+ {
285
+ "output_type": "execute_result",
286
+ "data": {
287
+ "text/plain": [
288
+ " sentence_id text tag\n",
289
+ "0 1.0 Naciones B-ORG\n",
290
+ "1 1.0 Unidas I-ORG\n",
291
+ "2 1.0 Asamblea O\n",
292
+ "3 1.0 General O\n",
293
+ "4 1.0 23 B-DATE\n",
294
+ "... ... ... ...\n",
295
+ "88927 658.0 los O\n",
296
+ "88928 658.0 niños B-PER\n",
297
+ "88929 658.0 niñas B-PER\n",
298
+ "88930 658.0 y O\n",
299
+ "88931 658.0 adolescentes B-PER\n",
300
+ "\n",
301
+ "[88932 rows x 3 columns]"
302
+ ],
303
+ "text/html": [
304
+ "\n",
305
+ " <div id=\"df-7f679e41-4744-436e-919a-37835cbd9393\" class=\"colab-df-container\">\n",
306
+ " <div>\n",
307
+ "<style scoped>\n",
308
+ " .dataframe tbody tr th:only-of-type {\n",
309
+ " vertical-align: middle;\n",
310
+ " }\n",
311
+ "\n",
312
+ " .dataframe tbody tr th {\n",
313
+ " vertical-align: top;\n",
314
+ " }\n",
315
+ "\n",
316
+ " .dataframe thead th {\n",
317
+ " text-align: right;\n",
318
+ " }\n",
319
+ "</style>\n",
320
+ "<table border=\"1\" class=\"dataframe\">\n",
321
+ " <thead>\n",
322
+ " <tr style=\"text-align: right;\">\n",
323
+ " <th></th>\n",
324
+ " <th>sentence_id</th>\n",
325
+ " <th>text</th>\n",
326
+ " <th>tag</th>\n",
327
+ " </tr>\n",
328
+ " </thead>\n",
329
+ " <tbody>\n",
330
+ " <tr>\n",
331
+ " <th>0</th>\n",
332
+ " <td>1.0</td>\n",
333
+ " <td>Naciones</td>\n",
334
+ " <td>B-ORG</td>\n",
335
+ " </tr>\n",
336
+ " <tr>\n",
337
+ " <th>1</th>\n",
338
+ " <td>1.0</td>\n",
339
+ " <td>Unidas</td>\n",
340
+ " <td>I-ORG</td>\n",
341
+ " </tr>\n",
342
+ " <tr>\n",
343
+ " <th>2</th>\n",
344
+ " <td>1.0</td>\n",
345
+ " <td>Asamblea</td>\n",
346
+ " <td>O</td>\n",
347
+ " </tr>\n",
348
+ " <tr>\n",
349
+ " <th>3</th>\n",
350
+ " <td>1.0</td>\n",
351
+ " <td>General</td>\n",
352
+ " <td>O</td>\n",
353
+ " </tr>\n",
354
+ " <tr>\n",
355
+ " <th>4</th>\n",
356
+ " <td>1.0</td>\n",
357
+ " <td>23</td>\n",
358
+ " <td>B-DATE</td>\n",
359
+ " </tr>\n",
360
+ " <tr>\n",
361
+ " <th>...</th>\n",
362
+ " <td>...</td>\n",
363
+ " <td>...</td>\n",
364
+ " <td>...</td>\n",
365
+ " </tr>\n",
366
+ " <tr>\n",
367
+ " <th>88927</th>\n",
368
+ " <td>658.0</td>\n",
369
+ " <td>los</td>\n",
370
+ " <td>O</td>\n",
371
+ " </tr>\n",
372
+ " <tr>\n",
373
+ " <th>88928</th>\n",
374
+ " <td>658.0</td>\n",
375
+ " <td>niños</td>\n",
376
+ " <td>B-PER</td>\n",
377
+ " </tr>\n",
378
+ " <tr>\n",
379
+ " <th>88929</th>\n",
380
+ " <td>658.0</td>\n",
381
+ " <td>niñas</td>\n",
382
+ " <td>B-PER</td>\n",
383
+ " </tr>\n",
384
+ " <tr>\n",
385
+ " <th>88930</th>\n",
386
+ " <td>658.0</td>\n",
387
+ " <td>y</td>\n",
388
+ " <td>O</td>\n",
389
+ " </tr>\n",
390
+ " <tr>\n",
391
+ " <th>88931</th>\n",
392
+ " <td>658.0</td>\n",
393
+ " <td>adolescentes</td>\n",
394
+ " <td>B-PER</td>\n",
395
+ " </tr>\n",
396
+ " </tbody>\n",
397
+ "</table>\n",
398
+ "<p>88932 rows × 3 columns</p>\n",
399
+ "</div>\n",
400
+ " <div class=\"colab-df-buttons\">\n",
401
+ "\n",
402
+ " <div class=\"colab-df-container\">\n",
403
+ " <button class=\"colab-df-convert\" onclick=\"convertToInteractive('df-7f679e41-4744-436e-919a-37835cbd9393')\"\n",
404
+ " title=\"Convert this dataframe to an interactive table.\"\n",
405
+ " style=\"display:none;\">\n",
406
+ "\n",
407
+ " <svg xmlns=\"http://www.w3.org/2000/svg\" height=\"24px\" viewBox=\"0 -960 960 960\">\n",
408
+ " <path d=\"M120-120v-720h720v720H120Zm60-500h600v-160H180v160Zm220 220h160v-160H400v160Zm0 220h160v-160H400v160ZM180-400h160v-160H180v160Zm440 0h160v-160H620v160ZM180-180h160v-160H180v160Zm440 0h160v-160H620v160Z\"/>\n",
409
+ " </svg>\n",
410
+ " </button>\n",
411
+ "\n",
412
+ " <style>\n",
413
+ " .colab-df-container {\n",
414
+ " display:flex;\n",
415
+ " gap: 12px;\n",
416
+ " }\n",
417
+ "\n",
418
+ " .colab-df-convert {\n",
419
+ " background-color: #E8F0FE;\n",
420
+ " border: none;\n",
421
+ " border-radius: 50%;\n",
422
+ " cursor: pointer;\n",
423
+ " display: none;\n",
424
+ " fill: #1967D2;\n",
425
+ " height: 32px;\n",
426
+ " padding: 0 0 0 0;\n",
427
+ " width: 32px;\n",
428
+ " }\n",
429
+ "\n",
430
+ " .colab-df-convert:hover {\n",
431
+ " background-color: #E2EBFA;\n",
432
+ " box-shadow: 0px 1px 2px rgba(60, 64, 67, 0.3), 0px 1px 3px 1px rgba(60, 64, 67, 0.15);\n",
433
+ " fill: #174EA6;\n",
434
+ " }\n",
435
+ "\n",
436
+ " .colab-df-buttons div {\n",
437
+ " margin-bottom: 4px;\n",
438
+ " }\n",
439
+ "\n",
440
+ " [theme=dark] .colab-df-convert {\n",
441
+ " background-color: #3B4455;\n",
442
+ " fill: #D2E3FC;\n",
443
+ " }\n",
444
+ "\n",
445
+ " [theme=dark] .colab-df-convert:hover {\n",
446
+ " background-color: #434B5C;\n",
447
+ " box-shadow: 0px 1px 3px 1px rgba(0, 0, 0, 0.15);\n",
448
+ " filter: drop-shadow(0px 1px 2px rgba(0, 0, 0, 0.3));\n",
449
+ " fill: #FFFFFF;\n",
450
+ " }\n",
451
+ " </style>\n",
452
+ "\n",
453
+ " <script>\n",
454
+ " const buttonEl =\n",
455
+ " document.querySelector('#df-7f679e41-4744-436e-919a-37835cbd9393 button.colab-df-convert');\n",
456
+ " buttonEl.style.display =\n",
457
+ " google.colab.kernel.accessAllowed ? 'block' : 'none';\n",
458
+ "\n",
459
+ " async function convertToInteractive(key) {\n",
460
+ " const element = document.querySelector('#df-7f679e41-4744-436e-919a-37835cbd9393');\n",
461
+ " const dataTable =\n",
462
+ " await google.colab.kernel.invokeFunction('convertToInteractive',\n",
463
+ " [key], {});\n",
464
+ " if (!dataTable) return;\n",
465
+ "\n",
466
+ " const docLinkHtml = 'Like what you see? Visit the ' +\n",
467
+ " '<a target=\"_blank\" href=https://colab.research.google.com/notebooks/data_table.ipynb>data table notebook</a>'\n",
468
+ " + ' to learn more about interactive tables.';\n",
469
+ " element.innerHTML = '';\n",
470
+ " dataTable['output_type'] = 'display_data';\n",
471
+ " await google.colab.output.renderOutput(dataTable, element);\n",
472
+ " const docLink = document.createElement('div');\n",
473
+ " docLink.innerHTML = docLinkHtml;\n",
474
+ " element.appendChild(docLink);\n",
475
+ " }\n",
476
+ " </script>\n",
477
+ " </div>\n",
478
+ "\n",
479
+ "\n",
480
+ "<div id=\"df-d9a0bf32-634d-4944-8793-55c2f58d1dfd\">\n",
481
+ " <button class=\"colab-df-quickchart\" onclick=\"quickchart('df-d9a0bf32-634d-4944-8793-55c2f58d1dfd')\"\n",
482
+ " title=\"Suggest charts\"\n",
483
+ " style=\"display:none;\">\n",
484
+ "\n",
485
+ "<svg xmlns=\"http://www.w3.org/2000/svg\" height=\"24px\"viewBox=\"0 0 24 24\"\n",
486
+ " width=\"24px\">\n",
487
+ " <g>\n",
488
+ " <path d=\"M19 3H5c-1.1 0-2 .9-2 2v14c0 1.1.9 2 2 2h14c1.1 0 2-.9 2-2V5c0-1.1-.9-2-2-2zM9 17H7v-7h2v7zm4 0h-2V7h2v10zm4 0h-2v-4h2v4z\"/>\n",
489
+ " </g>\n",
490
+ "</svg>\n",
491
+ " </button>\n",
492
+ "\n",
493
+ "<style>\n",
494
+ " .colab-df-quickchart {\n",
495
+ " --bg-color: #E8F0FE;\n",
496
+ " --fill-color: #1967D2;\n",
497
+ " --hover-bg-color: #E2EBFA;\n",
498
+ " --hover-fill-color: #174EA6;\n",
499
+ " --disabled-fill-color: #AAA;\n",
500
+ " --disabled-bg-color: #DDD;\n",
501
+ " }\n",
502
+ "\n",
503
+ " [theme=dark] .colab-df-quickchart {\n",
504
+ " --bg-color: #3B4455;\n",
505
+ " --fill-color: #D2E3FC;\n",
506
+ " --hover-bg-color: #434B5C;\n",
507
+ " --hover-fill-color: #FFFFFF;\n",
508
+ " --disabled-bg-color: #3B4455;\n",
509
+ " --disabled-fill-color: #666;\n",
510
+ " }\n",
511
+ "\n",
512
+ " .colab-df-quickchart {\n",
513
+ " background-color: var(--bg-color);\n",
514
+ " border: none;\n",
515
+ " border-radius: 50%;\n",
516
+ " cursor: pointer;\n",
517
+ " display: none;\n",
518
+ " fill: var(--fill-color);\n",
519
+ " height: 32px;\n",
520
+ " padding: 0;\n",
521
+ " width: 32px;\n",
522
+ " }\n",
523
+ "\n",
524
+ " .colab-df-quickchart:hover {\n",
525
+ " background-color: var(--hover-bg-color);\n",
526
+ " box-shadow: 0 1px 2px rgba(60, 64, 67, 0.3), 0 1px 3px 1px rgba(60, 64, 67, 0.15);\n",
527
+ " fill: var(--button-hover-fill-color);\n",
528
+ " }\n",
529
+ "\n",
530
+ " .colab-df-quickchart-complete:disabled,\n",
531
+ " .colab-df-quickchart-complete:disabled:hover {\n",
532
+ " background-color: var(--disabled-bg-color);\n",
533
+ " fill: var(--disabled-fill-color);\n",
534
+ " box-shadow: none;\n",
535
+ " }\n",
536
+ "\n",
537
+ " .colab-df-spinner {\n",
538
+ " border: 2px solid var(--fill-color);\n",
539
+ " border-color: transparent;\n",
540
+ " border-bottom-color: var(--fill-color);\n",
541
+ " animation:\n",
542
+ " spin 1s steps(1) infinite;\n",
543
+ " }\n",
544
+ "\n",
545
+ " @keyframes spin {\n",
546
+ " 0% {\n",
547
+ " border-color: transparent;\n",
548
+ " border-bottom-color: var(--fill-color);\n",
549
+ " border-left-color: var(--fill-color);\n",
550
+ " }\n",
551
+ " 20% {\n",
552
+ " border-color: transparent;\n",
553
+ " border-left-color: var(--fill-color);\n",
554
+ " border-top-color: var(--fill-color);\n",
555
+ " }\n",
556
+ " 30% {\n",
557
+ " border-color: transparent;\n",
558
+ " border-left-color: var(--fill-color);\n",
559
+ " border-top-color: var(--fill-color);\n",
560
+ " border-right-color: var(--fill-color);\n",
561
+ " }\n",
562
+ " 40% {\n",
563
+ " border-color: transparent;\n",
564
+ " border-right-color: var(--fill-color);\n",
565
+ " border-top-color: var(--fill-color);\n",
566
+ " }\n",
567
+ " 60% {\n",
568
+ " border-color: transparent;\n",
569
+ " border-right-color: var(--fill-color);\n",
570
+ " }\n",
571
+ " 80% {\n",
572
+ " border-color: transparent;\n",
573
+ " border-right-color: var(--fill-color);\n",
574
+ " border-bottom-color: var(--fill-color);\n",
575
+ " }\n",
576
+ " 90% {\n",
577
+ " border-color: transparent;\n",
578
+ " border-bottom-color: var(--fill-color);\n",
579
+ " }\n",
580
+ " }\n",
581
+ "</style>\n",
582
+ "\n",
583
+ " <script>\n",
584
+ " async function quickchart(key) {\n",
585
+ " const quickchartButtonEl =\n",
586
+ " document.querySelector('#' + key + ' button');\n",
587
+ " quickchartButtonEl.disabled = true; // To prevent multiple clicks.\n",
588
+ " quickchartButtonEl.classList.add('colab-df-spinner');\n",
589
+ " try {\n",
590
+ " const charts = await google.colab.kernel.invokeFunction(\n",
591
+ " 'suggestCharts', [key], {});\n",
592
+ " } catch (error) {\n",
593
+ " console.error('Error during call to suggestCharts:', error);\n",
594
+ " }\n",
595
+ " quickchartButtonEl.classList.remove('colab-df-spinner');\n",
596
+ " quickchartButtonEl.classList.add('colab-df-quickchart-complete');\n",
597
+ " }\n",
598
+ " (() => {\n",
599
+ " let quickchartButtonEl =\n",
600
+ " document.querySelector('#df-d9a0bf32-634d-4944-8793-55c2f58d1dfd button');\n",
601
+ " quickchartButtonEl.style.display =\n",
602
+ " google.colab.kernel.accessAllowed ? 'block' : 'none';\n",
603
+ " })();\n",
604
+ " </script>\n",
605
+ "</div>\n",
606
+ " </div>\n",
607
+ " </div>\n"
608
+ ]
609
+ },
610
+ "metadata": {},
611
+ "execution_count": 7
612
+ }
613
+ ]
614
+ },
615
+ {
616
+ "cell_type": "code",
617
+ "source": [
618
+ "umbral_longitud = 50\n",
619
+ "\n",
620
+ "# Filtrar las oraciones por longitud dentro de cada grupo de sentence_id\n",
621
+ "df_filtrado = df.groupby('sentence_id').filter(lambda group: len(group) < umbral_longitud)\n",
622
+ "\n",
623
+ "# Imprimir el DataFrame resultante\n",
624
+ "df_filtrado"
625
+ ],
626
+ "metadata": {
627
+ "id": "NibPBjotWtBi",
628
+ "colab": {
629
+ "base_uri": "https://localhost:8080/",
630
+ "height": 424
631
+ },
632
+ "outputId": "69a1ffde-2e82-47ab-cbb8-f48d3dc7bef5"
633
+ },
634
+ "execution_count": null,
635
+ "outputs": [
636
+ {
637
+ "output_type": "execute_result",
638
+ "data": {
639
+ "text/plain": [
640
+ " sentence_id text tag\n",
641
+ "0 1.0 Naciones B-ORG\n",
642
+ "1 1.0 Unidas I-ORG\n",
643
+ "2 1.0 Asamblea O\n",
644
+ "3 1.0 General O\n",
645
+ "4 1.0 23 B-DATE\n",
646
+ "... ... ... ...\n",
647
+ "88927 658.0 los O\n",
648
+ "88928 658.0 niños B-PER\n",
649
+ "88929 658.0 niñas B-PER\n",
650
+ "88930 658.0 y O\n",
651
+ "88931 658.0 adolescentes B-PER\n",
652
+ "\n",
653
+ "[14485 rows x 3 columns]"
654
+ ],
655
+ "text/html": [
656
+ "\n",
657
+ " <div id=\"df-8c8700b8-e6af-40d0-b782-17e18ee9a7ab\" class=\"colab-df-container\">\n",
658
+ " <div>\n",
659
+ "<style scoped>\n",
660
+ " .dataframe tbody tr th:only-of-type {\n",
661
+ " vertical-align: middle;\n",
662
+ " }\n",
663
+ "\n",
664
+ " .dataframe tbody tr th {\n",
665
+ " vertical-align: top;\n",
666
+ " }\n",
667
+ "\n",
668
+ " .dataframe thead th {\n",
669
+ " text-align: right;\n",
670
+ " }\n",
671
+ "</style>\n",
672
+ "<table border=\"1\" class=\"dataframe\">\n",
673
+ " <thead>\n",
674
+ " <tr style=\"text-align: right;\">\n",
675
+ " <th></th>\n",
676
+ " <th>sentence_id</th>\n",
677
+ " <th>text</th>\n",
678
+ " <th>tag</th>\n",
679
+ " </tr>\n",
680
+ " </thead>\n",
681
+ " <tbody>\n",
682
+ " <tr>\n",
683
+ " <th>0</th>\n",
684
+ " <td>1.0</td>\n",
685
+ " <td>Naciones</td>\n",
686
+ " <td>B-ORG</td>\n",
687
+ " </tr>\n",
688
+ " <tr>\n",
689
+ " <th>1</th>\n",
690
+ " <td>1.0</td>\n",
691
+ " <td>Unidas</td>\n",
692
+ " <td>I-ORG</td>\n",
693
+ " </tr>\n",
694
+ " <tr>\n",
695
+ " <th>2</th>\n",
696
+ " <td>1.0</td>\n",
697
+ " <td>Asamblea</td>\n",
698
+ " <td>O</td>\n",
699
+ " </tr>\n",
700
+ " <tr>\n",
701
+ " <th>3</th>\n",
702
+ " <td>1.0</td>\n",
703
+ " <td>General</td>\n",
704
+ " <td>O</td>\n",
705
+ " </tr>\n",
706
+ " <tr>\n",
707
+ " <th>4</th>\n",
708
+ " <td>1.0</td>\n",
709
+ " <td>23</td>\n",
710
+ " <td>B-DATE</td>\n",
711
+ " </tr>\n",
712
+ " <tr>\n",
713
+ " <th>...</th>\n",
714
+ " <td>...</td>\n",
715
+ " <td>...</td>\n",
716
+ " <td>...</td>\n",
717
+ " </tr>\n",
718
+ " <tr>\n",
719
+ " <th>88927</th>\n",
720
+ " <td>658.0</td>\n",
721
+ " <td>los</td>\n",
722
+ " <td>O</td>\n",
723
+ " </tr>\n",
724
+ " <tr>\n",
725
+ " <th>88928</th>\n",
726
+ " <td>658.0</td>\n",
727
+ " <td>niños</td>\n",
728
+ " <td>B-PER</td>\n",
729
+ " </tr>\n",
730
+ " <tr>\n",
731
+ " <th>88929</th>\n",
732
+ " <td>658.0</td>\n",
733
+ " <td>niñas</td>\n",
734
+ " <td>B-PER</td>\n",
735
+ " </tr>\n",
736
+ " <tr>\n",
737
+ " <th>88930</th>\n",
738
+ " <td>658.0</td>\n",
739
+ " <td>y</td>\n",
740
+ " <td>O</td>\n",
741
+ " </tr>\n",
742
+ " <tr>\n",
743
+ " <th>88931</th>\n",
744
+ " <td>658.0</td>\n",
745
+ " <td>adolescentes</td>\n",
746
+ " <td>B-PER</td>\n",
747
+ " </tr>\n",
748
+ " </tbody>\n",
749
+ "</table>\n",
750
+ "<p>14485 rows × 3 columns</p>\n",
751
+ "</div>\n",
752
+ " <div class=\"colab-df-buttons\">\n",
753
+ "\n",
754
+ " <div class=\"colab-df-container\">\n",
755
+ " <button class=\"colab-df-convert\" onclick=\"convertToInteractive('df-8c8700b8-e6af-40d0-b782-17e18ee9a7ab')\"\n",
756
+ " title=\"Convert this dataframe to an interactive table.\"\n",
757
+ " style=\"display:none;\">\n",
758
+ "\n",
759
+ " <svg xmlns=\"http://www.w3.org/2000/svg\" height=\"24px\" viewBox=\"0 -960 960 960\">\n",
760
+ " <path d=\"M120-120v-720h720v720H120Zm60-500h600v-160H180v160Zm220 220h160v-160H400v160Zm0 220h160v-160H400v160ZM180-400h160v-160H180v160Zm440 0h160v-160H620v160ZM180-180h160v-160H180v160Zm440 0h160v-160H620v160Z\"/>\n",
761
+ " </svg>\n",
762
+ " </button>\n",
763
+ "\n",
764
+ " <style>\n",
765
+ " .colab-df-container {\n",
766
+ " display:flex;\n",
767
+ " gap: 12px;\n",
768
+ " }\n",
769
+ "\n",
770
+ " .colab-df-convert {\n",
771
+ " background-color: #E8F0FE;\n",
772
+ " border: none;\n",
773
+ " border-radius: 50%;\n",
774
+ " cursor: pointer;\n",
775
+ " display: none;\n",
776
+ " fill: #1967D2;\n",
777
+ " height: 32px;\n",
778
+ " padding: 0 0 0 0;\n",
779
+ " width: 32px;\n",
780
+ " }\n",
781
+ "\n",
782
+ " .colab-df-convert:hover {\n",
783
+ " background-color: #E2EBFA;\n",
784
+ " box-shadow: 0px 1px 2px rgba(60, 64, 67, 0.3), 0px 1px 3px 1px rgba(60, 64, 67, 0.15);\n",
785
+ " fill: #174EA6;\n",
786
+ " }\n",
787
+ "\n",
788
+ " .colab-df-buttons div {\n",
789
+ " margin-bottom: 4px;\n",
790
+ " }\n",
791
+ "\n",
792
+ " [theme=dark] .colab-df-convert {\n",
793
+ " background-color: #3B4455;\n",
794
+ " fill: #D2E3FC;\n",
795
+ " }\n",
796
+ "\n",
797
+ " [theme=dark] .colab-df-convert:hover {\n",
798
+ " background-color: #434B5C;\n",
799
+ " box-shadow: 0px 1px 3px 1px rgba(0, 0, 0, 0.15);\n",
800
+ " filter: drop-shadow(0px 1px 2px rgba(0, 0, 0, 0.3));\n",
801
+ " fill: #FFFFFF;\n",
802
+ " }\n",
803
+ " </style>\n",
804
+ "\n",
805
+ " <script>\n",
806
+ " const buttonEl =\n",
807
+ " document.querySelector('#df-8c8700b8-e6af-40d0-b782-17e18ee9a7ab button.colab-df-convert');\n",
808
+ " buttonEl.style.display =\n",
809
+ " google.colab.kernel.accessAllowed ? 'block' : 'none';\n",
810
+ "\n",
811
+ " async function convertToInteractive(key) {\n",
812
+ " const element = document.querySelector('#df-8c8700b8-e6af-40d0-b782-17e18ee9a7ab');\n",
813
+ " const dataTable =\n",
814
+ " await google.colab.kernel.invokeFunction('convertToInteractive',\n",
815
+ " [key], {});\n",
816
+ " if (!dataTable) return;\n",
817
+ "\n",
818
+ " const docLinkHtml = 'Like what you see? Visit the ' +\n",
819
+ " '<a target=\"_blank\" href=https://colab.research.google.com/notebooks/data_table.ipynb>data table notebook</a>'\n",
820
+ " + ' to learn more about interactive tables.';\n",
821
+ " element.innerHTML = '';\n",
822
+ " dataTable['output_type'] = 'display_data';\n",
823
+ " await google.colab.output.renderOutput(dataTable, element);\n",
824
+ " const docLink = document.createElement('div');\n",
825
+ " docLink.innerHTML = docLinkHtml;\n",
826
+ " element.appendChild(docLink);\n",
827
+ " }\n",
828
+ " </script>\n",
829
+ " </div>\n",
830
+ "\n",
831
+ "\n",
832
+ "<div id=\"df-87a32cf0-c30f-4584-b711-aa2dff9cd004\">\n",
833
+ " <button class=\"colab-df-quickchart\" onclick=\"quickchart('df-87a32cf0-c30f-4584-b711-aa2dff9cd004')\"\n",
834
+ " title=\"Suggest charts\"\n",
835
+ " style=\"display:none;\">\n",
836
+ "\n",
837
+ "<svg xmlns=\"http://www.w3.org/2000/svg\" height=\"24px\"viewBox=\"0 0 24 24\"\n",
838
+ " width=\"24px\">\n",
839
+ " <g>\n",
840
+ " <path d=\"M19 3H5c-1.1 0-2 .9-2 2v14c0 1.1.9 2 2 2h14c1.1 0 2-.9 2-2V5c0-1.1-.9-2-2-2zM9 17H7v-7h2v7zm4 0h-2V7h2v10zm4 0h-2v-4h2v4z\"/>\n",
841
+ " </g>\n",
842
+ "</svg>\n",
843
+ " </button>\n",
844
+ "\n",
845
+ "<style>\n",
846
+ " .colab-df-quickchart {\n",
847
+ " --bg-color: #E8F0FE;\n",
848
+ " --fill-color: #1967D2;\n",
849
+ " --hover-bg-color: #E2EBFA;\n",
850
+ " --hover-fill-color: #174EA6;\n",
851
+ " --disabled-fill-color: #AAA;\n",
852
+ " --disabled-bg-color: #DDD;\n",
853
+ " }\n",
854
+ "\n",
855
+ " [theme=dark] .colab-df-quickchart {\n",
856
+ " --bg-color: #3B4455;\n",
857
+ " --fill-color: #D2E3FC;\n",
858
+ " --hover-bg-color: #434B5C;\n",
859
+ " --hover-fill-color: #FFFFFF;\n",
860
+ " --disabled-bg-color: #3B4455;\n",
861
+ " --disabled-fill-color: #666;\n",
862
+ " }\n",
863
+ "\n",
864
+ " .colab-df-quickchart {\n",
865
+ " background-color: var(--bg-color);\n",
866
+ " border: none;\n",
867
+ " border-radius: 50%;\n",
868
+ " cursor: pointer;\n",
869
+ " display: none;\n",
870
+ " fill: var(--fill-color);\n",
871
+ " height: 32px;\n",
872
+ " padding: 0;\n",
873
+ " width: 32px;\n",
874
+ " }\n",
875
+ "\n",
876
+ " .colab-df-quickchart:hover {\n",
877
+ " background-color: var(--hover-bg-color);\n",
878
+ " box-shadow: 0 1px 2px rgba(60, 64, 67, 0.3), 0 1px 3px 1px rgba(60, 64, 67, 0.15);\n",
879
+ " fill: var(--button-hover-fill-color);\n",
880
+ " }\n",
881
+ "\n",
882
+ " .colab-df-quickchart-complete:disabled,\n",
883
+ " .colab-df-quickchart-complete:disabled:hover {\n",
884
+ " background-color: var(--disabled-bg-color);\n",
885
+ " fill: var(--disabled-fill-color);\n",
886
+ " box-shadow: none;\n",
887
+ " }\n",
888
+ "\n",
889
+ " .colab-df-spinner {\n",
890
+ " border: 2px solid var(--fill-color);\n",
891
+ " border-color: transparent;\n",
892
+ " border-bottom-color: var(--fill-color);\n",
893
+ " animation:\n",
894
+ " spin 1s steps(1) infinite;\n",
895
+ " }\n",
896
+ "\n",
897
+ " @keyframes spin {\n",
898
+ " 0% {\n",
899
+ " border-color: transparent;\n",
900
+ " border-bottom-color: var(--fill-color);\n",
901
+ " border-left-color: var(--fill-color);\n",
902
+ " }\n",
903
+ " 20% {\n",
904
+ " border-color: transparent;\n",
905
+ " border-left-color: var(--fill-color);\n",
906
+ " border-top-color: var(--fill-color);\n",
907
+ " }\n",
908
+ " 30% {\n",
909
+ " border-color: transparent;\n",
910
+ " border-left-color: var(--fill-color);\n",
911
+ " border-top-color: var(--fill-color);\n",
912
+ " border-right-color: var(--fill-color);\n",
913
+ " }\n",
914
+ " 40% {\n",
915
+ " border-color: transparent;\n",
916
+ " border-right-color: var(--fill-color);\n",
917
+ " border-top-color: var(--fill-color);\n",
918
+ " }\n",
919
+ " 60% {\n",
920
+ " border-color: transparent;\n",
921
+ " border-right-color: var(--fill-color);\n",
922
+ " }\n",
923
+ " 80% {\n",
924
+ " border-color: transparent;\n",
925
+ " border-right-color: var(--fill-color);\n",
926
+ " border-bottom-color: var(--fill-color);\n",
927
+ " }\n",
928
+ " 90% {\n",
929
+ " border-color: transparent;\n",
930
+ " border-bottom-color: var(--fill-color);\n",
931
+ " }\n",
932
+ " }\n",
933
+ "</style>\n",
934
+ "\n",
935
+ " <script>\n",
936
+ " async function quickchart(key) {\n",
937
+ " const quickchartButtonEl =\n",
938
+ " document.querySelector('#' + key + ' button');\n",
939
+ " quickchartButtonEl.disabled = true; // To prevent multiple clicks.\n",
940
+ " quickchartButtonEl.classList.add('colab-df-spinner');\n",
941
+ " try {\n",
942
+ " const charts = await google.colab.kernel.invokeFunction(\n",
943
+ " 'suggestCharts', [key], {});\n",
944
+ " } catch (error) {\n",
945
+ " console.error('Error during call to suggestCharts:', error);\n",
946
+ " }\n",
947
+ " quickchartButtonEl.classList.remove('colab-df-spinner');\n",
948
+ " quickchartButtonEl.classList.add('colab-df-quickchart-complete');\n",
949
+ " }\n",
950
+ " (() => {\n",
951
+ " let quickchartButtonEl =\n",
952
+ " document.querySelector('#df-87a32cf0-c30f-4584-b711-aa2dff9cd004 button');\n",
953
+ " quickchartButtonEl.style.display =\n",
954
+ " google.colab.kernel.accessAllowed ? 'block' : 'none';\n",
955
+ " })();\n",
956
+ " </script>\n",
957
+ "</div>\n",
958
+ " </div>\n",
959
+ " </div>\n"
960
+ ]
961
+ },
962
+ "metadata": {},
963
+ "execution_count": 8
964
+ }
965
+ ]
966
+ },
967
+ {
968
+ "cell_type": "markdown",
969
+ "source": [
970
+ "**Para esta prueba se van a mantener las etiquetas `ORG`, `PER`, `GEO`, `VIO`, `ARM`, `PAZ`, `AFE`.**"
971
+ ],
972
+ "metadata": {
973
+ "id": "oVdLoDK-YFZt"
974
+ }
975
+ },
976
+ {
977
+ "cell_type": "code",
978
+ "source": [
979
+ "df_filtrado.tag.unique()"
980
+ ],
981
+ "metadata": {
982
+ "id": "gR-6My-VX8jW",
983
+ "colab": {
984
+ "base_uri": "https://localhost:8080/"
985
+ },
986
+ "outputId": "ff549e71-e4f2-4fad-d4b2-c54cc05f2460"
987
+ },
988
+ "execution_count": null,
989
+ "outputs": [
990
+ {
991
+ "output_type": "execute_result",
992
+ "data": {
993
+ "text/plain": [
994
+ "array(['B-ORG', 'I-ORG', 'O', 'B-DATE', 'I-DATE', 'B-PER', 'I-PER',\n",
995
+ " 'B-VIO', 'I-VIO', 'B-ARM', 'I-ARM', 'B-CON', 'B-ATE', 'I-ATE',\n",
996
+ " 'B-GEO', 'I-CON', 'B-MEM', 'B-AFE', 'B-LUC', 'I-LUC', 'I-AFE',\n",
997
+ " 'I-GEO', 'B-PAZ', 'I-PAZ', 'B-EVE', 'I-EVE', 'B-DER', 'I-DER',\n",
998
+ " 'B-LEY', 'I-LEY', 'I-MEM'], dtype=object)"
999
+ ]
1000
+ },
1001
+ "metadata": {},
1002
+ "execution_count": 9
1003
+ }
1004
+ ]
1005
+ },
1006
+ {
1007
+ "cell_type": "code",
1008
+ "source": [
1009
+ "to_replace = ['B-'+tag for tag in ['CON', 'ATE', 'MEM', 'LUC', 'EVE', 'DER', 'LEY']]\n",
1010
+ "to_replace.extend(['I-'+tag for tag in ['CON', 'ATE', 'MEM', 'LUC', 'EVE', 'DER', 'LEY']])\n",
1011
+ "df_filtrado['new_tag'] = df_filtrado['tag'].replace(to_replace, 'O')"
1012
+ ],
1013
+ "metadata": {
1014
+ "id": "eZlRSM68YCEy"
1015
+ },
1016
+ "execution_count": null,
1017
+ "outputs": []
1018
+ },
1019
+ {
1020
+ "cell_type": "code",
1021
+ "source": [
1022
+ "df_filtrado.head()"
1023
+ ],
1024
+ "metadata": {
1025
+ "id": "qPZ_ZfW-dJ-C",
1026
+ "colab": {
1027
+ "base_uri": "https://localhost:8080/",
1028
+ "height": 206
1029
+ },
1030
+ "outputId": "8c0227b5-7def-4956-f1c4-3803cffd276c"
1031
+ },
1032
+ "execution_count": null,
1033
+ "outputs": [
1034
+ {
1035
+ "output_type": "execute_result",
1036
+ "data": {
1037
+ "text/plain": [
1038
+ " sentence_id text tag new_tag\n",
1039
+ "0 1.0 Naciones B-ORG B-ORG\n",
1040
+ "1 1.0 Unidas I-ORG I-ORG\n",
1041
+ "2 1.0 Asamblea O O\n",
1042
+ "3 1.0 General O O\n",
1043
+ "4 1.0 23 B-DATE B-DATE"
1044
+ ],
1045
+ "text/html": [
1046
+ "\n",
1047
+ " <div id=\"df-29d1fe30-5920-4363-bdb4-c48cc415bb9f\" class=\"colab-df-container\">\n",
1048
+ " <div>\n",
1049
+ "<style scoped>\n",
1050
+ " .dataframe tbody tr th:only-of-type {\n",
1051
+ " vertical-align: middle;\n",
1052
+ " }\n",
1053
+ "\n",
1054
+ " .dataframe tbody tr th {\n",
1055
+ " vertical-align: top;\n",
1056
+ " }\n",
1057
+ "\n",
1058
+ " .dataframe thead th {\n",
1059
+ " text-align: right;\n",
1060
+ " }\n",
1061
+ "</style>\n",
1062
+ "<table border=\"1\" class=\"dataframe\">\n",
1063
+ " <thead>\n",
1064
+ " <tr style=\"text-align: right;\">\n",
1065
+ " <th></th>\n",
1066
+ " <th>sentence_id</th>\n",
1067
+ " <th>text</th>\n",
1068
+ " <th>tag</th>\n",
1069
+ " <th>new_tag</th>\n",
1070
+ " </tr>\n",
1071
+ " </thead>\n",
1072
+ " <tbody>\n",
1073
+ " <tr>\n",
1074
+ " <th>0</th>\n",
1075
+ " <td>1.0</td>\n",
1076
+ " <td>Naciones</td>\n",
1077
+ " <td>B-ORG</td>\n",
1078
+ " <td>B-ORG</td>\n",
1079
+ " </tr>\n",
1080
+ " <tr>\n",
1081
+ " <th>1</th>\n",
1082
+ " <td>1.0</td>\n",
1083
+ " <td>Unidas</td>\n",
1084
+ " <td>I-ORG</td>\n",
1085
+ " <td>I-ORG</td>\n",
1086
+ " </tr>\n",
1087
+ " <tr>\n",
1088
+ " <th>2</th>\n",
1089
+ " <td>1.0</td>\n",
1090
+ " <td>Asamblea</td>\n",
1091
+ " <td>O</td>\n",
1092
+ " <td>O</td>\n",
1093
+ " </tr>\n",
1094
+ " <tr>\n",
1095
+ " <th>3</th>\n",
1096
+ " <td>1.0</td>\n",
1097
+ " <td>General</td>\n",
1098
+ " <td>O</td>\n",
1099
+ " <td>O</td>\n",
1100
+ " </tr>\n",
1101
+ " <tr>\n",
1102
+ " <th>4</th>\n",
1103
+ " <td>1.0</td>\n",
1104
+ " <td>23</td>\n",
1105
+ " <td>B-DATE</td>\n",
1106
+ " <td>B-DATE</td>\n",
1107
+ " </tr>\n",
1108
+ " </tbody>\n",
1109
+ "</table>\n",
1110
+ "</div>\n",
1111
+ " <div class=\"colab-df-buttons\">\n",
1112
+ "\n",
1113
+ " <div class=\"colab-df-container\">\n",
1114
+ " <button class=\"colab-df-convert\" onclick=\"convertToInteractive('df-29d1fe30-5920-4363-bdb4-c48cc415bb9f')\"\n",
1115
+ " title=\"Convert this dataframe to an interactive table.\"\n",
1116
+ " style=\"display:none;\">\n",
1117
+ "\n",
1118
+ " <svg xmlns=\"http://www.w3.org/2000/svg\" height=\"24px\" viewBox=\"0 -960 960 960\">\n",
1119
+ " <path d=\"M120-120v-720h720v720H120Zm60-500h600v-160H180v160Zm220 220h160v-160H400v160Zm0 220h160v-160H400v160ZM180-400h160v-160H180v160Zm440 0h160v-160H620v160ZM180-180h160v-160H180v160Zm440 0h160v-160H620v160Z\"/>\n",
1120
+ " </svg>\n",
1121
+ " </button>\n",
1122
+ "\n",
1123
+ " <style>\n",
1124
+ " .colab-df-container {\n",
1125
+ " display:flex;\n",
1126
+ " gap: 12px;\n",
1127
+ " }\n",
1128
+ "\n",
1129
+ " .colab-df-convert {\n",
1130
+ " background-color: #E8F0FE;\n",
1131
+ " border: none;\n",
1132
+ " border-radius: 50%;\n",
1133
+ " cursor: pointer;\n",
1134
+ " display: none;\n",
1135
+ " fill: #1967D2;\n",
1136
+ " height: 32px;\n",
1137
+ " padding: 0 0 0 0;\n",
1138
+ " width: 32px;\n",
1139
+ " }\n",
1140
+ "\n",
1141
+ " .colab-df-convert:hover {\n",
1142
+ " background-color: #E2EBFA;\n",
1143
+ " box-shadow: 0px 1px 2px rgba(60, 64, 67, 0.3), 0px 1px 3px 1px rgba(60, 64, 67, 0.15);\n",
1144
+ " fill: #174EA6;\n",
1145
+ " }\n",
1146
+ "\n",
1147
+ " .colab-df-buttons div {\n",
1148
+ " margin-bottom: 4px;\n",
1149
+ " }\n",
1150
+ "\n",
1151
+ " [theme=dark] .colab-df-convert {\n",
1152
+ " background-color: #3B4455;\n",
1153
+ " fill: #D2E3FC;\n",
1154
+ " }\n",
1155
+ "\n",
1156
+ " [theme=dark] .colab-df-convert:hover {\n",
1157
+ " background-color: #434B5C;\n",
1158
+ " box-shadow: 0px 1px 3px 1px rgba(0, 0, 0, 0.15);\n",
1159
+ " filter: drop-shadow(0px 1px 2px rgba(0, 0, 0, 0.3));\n",
1160
+ " fill: #FFFFFF;\n",
1161
+ " }\n",
1162
+ " </style>\n",
1163
+ "\n",
1164
+ " <script>\n",
1165
+ " const buttonEl =\n",
1166
+ " document.querySelector('#df-29d1fe30-5920-4363-bdb4-c48cc415bb9f button.colab-df-convert');\n",
1167
+ " buttonEl.style.display =\n",
1168
+ " google.colab.kernel.accessAllowed ? 'block' : 'none';\n",
1169
+ "\n",
1170
+ " async function convertToInteractive(key) {\n",
1171
+ " const element = document.querySelector('#df-29d1fe30-5920-4363-bdb4-c48cc415bb9f');\n",
1172
+ " const dataTable =\n",
1173
+ " await google.colab.kernel.invokeFunction('convertToInteractive',\n",
1174
+ " [key], {});\n",
1175
+ " if (!dataTable) return;\n",
1176
+ "\n",
1177
+ " const docLinkHtml = 'Like what you see? Visit the ' +\n",
1178
+ " '<a target=\"_blank\" href=https://colab.research.google.com/notebooks/data_table.ipynb>data table notebook</a>'\n",
1179
+ " + ' to learn more about interactive tables.';\n",
1180
+ " element.innerHTML = '';\n",
1181
+ " dataTable['output_type'] = 'display_data';\n",
1182
+ " await google.colab.output.renderOutput(dataTable, element);\n",
1183
+ " const docLink = document.createElement('div');\n",
1184
+ " docLink.innerHTML = docLinkHtml;\n",
1185
+ " element.appendChild(docLink);\n",
1186
+ " }\n",
1187
+ " </script>\n",
1188
+ " </div>\n",
1189
+ "\n",
1190
+ "\n",
1191
+ "<div id=\"df-83eb9e5f-bc38-42d4-8788-0e5ce29260c8\">\n",
1192
+ " <button class=\"colab-df-quickchart\" onclick=\"quickchart('df-83eb9e5f-bc38-42d4-8788-0e5ce29260c8')\"\n",
1193
+ " title=\"Suggest charts\"\n",
1194
+ " style=\"display:none;\">\n",
1195
+ "\n",
1196
+ "<svg xmlns=\"http://www.w3.org/2000/svg\" height=\"24px\"viewBox=\"0 0 24 24\"\n",
1197
+ " width=\"24px\">\n",
1198
+ " <g>\n",
1199
+ " <path d=\"M19 3H5c-1.1 0-2 .9-2 2v14c0 1.1.9 2 2 2h14c1.1 0 2-.9 2-2V5c0-1.1-.9-2-2-2zM9 17H7v-7h2v7zm4 0h-2V7h2v10zm4 0h-2v-4h2v4z\"/>\n",
1200
+ " </g>\n",
1201
+ "</svg>\n",
1202
+ " </button>\n",
1203
+ "\n",
1204
+ "<style>\n",
1205
+ " .colab-df-quickchart {\n",
1206
+ " --bg-color: #E8F0FE;\n",
1207
+ " --fill-color: #1967D2;\n",
1208
+ " --hover-bg-color: #E2EBFA;\n",
1209
+ " --hover-fill-color: #174EA6;\n",
1210
+ " --disabled-fill-color: #AAA;\n",
1211
+ " --disabled-bg-color: #DDD;\n",
1212
+ " }\n",
1213
+ "\n",
1214
+ " [theme=dark] .colab-df-quickchart {\n",
1215
+ " --bg-color: #3B4455;\n",
1216
+ " --fill-color: #D2E3FC;\n",
1217
+ " --hover-bg-color: #434B5C;\n",
1218
+ " --hover-fill-color: #FFFFFF;\n",
1219
+ " --disabled-bg-color: #3B4455;\n",
1220
+ " --disabled-fill-color: #666;\n",
1221
+ " }\n",
1222
+ "\n",
1223
+ " .colab-df-quickchart {\n",
1224
+ " background-color: var(--bg-color);\n",
1225
+ " border: none;\n",
1226
+ " border-radius: 50%;\n",
1227
+ " cursor: pointer;\n",
1228
+ " display: none;\n",
1229
+ " fill: var(--fill-color);\n",
1230
+ " height: 32px;\n",
1231
+ " padding: 0;\n",
1232
+ " width: 32px;\n",
1233
+ " }\n",
1234
+ "\n",
1235
+ " .colab-df-quickchart:hover {\n",
1236
+ " background-color: var(--hover-bg-color);\n",
1237
+ " box-shadow: 0 1px 2px rgba(60, 64, 67, 0.3), 0 1px 3px 1px rgba(60, 64, 67, 0.15);\n",
1238
+ " fill: var(--button-hover-fill-color);\n",
1239
+ " }\n",
1240
+ "\n",
1241
+ " .colab-df-quickchart-complete:disabled,\n",
1242
+ " .colab-df-quickchart-complete:disabled:hover {\n",
1243
+ " background-color: var(--disabled-bg-color);\n",
1244
+ " fill: var(--disabled-fill-color);\n",
1245
+ " box-shadow: none;\n",
1246
+ " }\n",
1247
+ "\n",
1248
+ " .colab-df-spinner {\n",
1249
+ " border: 2px solid var(--fill-color);\n",
1250
+ " border-color: transparent;\n",
1251
+ " border-bottom-color: var(--fill-color);\n",
1252
+ " animation:\n",
1253
+ " spin 1s steps(1) infinite;\n",
1254
+ " }\n",
1255
+ "\n",
1256
+ " @keyframes spin {\n",
1257
+ " 0% {\n",
1258
+ " border-color: transparent;\n",
1259
+ " border-bottom-color: var(--fill-color);\n",
1260
+ " border-left-color: var(--fill-color);\n",
1261
+ " }\n",
1262
+ " 20% {\n",
1263
+ " border-color: transparent;\n",
1264
+ " border-left-color: var(--fill-color);\n",
1265
+ " border-top-color: var(--fill-color);\n",
1266
+ " }\n",
1267
+ " 30% {\n",
1268
+ " border-color: transparent;\n",
1269
+ " border-left-color: var(--fill-color);\n",
1270
+ " border-top-color: var(--fill-color);\n",
1271
+ " border-right-color: var(--fill-color);\n",
1272
+ " }\n",
1273
+ " 40% {\n",
1274
+ " border-color: transparent;\n",
1275
+ " border-right-color: var(--fill-color);\n",
1276
+ " border-top-color: var(--fill-color);\n",
1277
+ " }\n",
1278
+ " 60% {\n",
1279
+ " border-color: transparent;\n",
1280
+ " border-right-color: var(--fill-color);\n",
1281
+ " }\n",
1282
+ " 80% {\n",
1283
+ " border-color: transparent;\n",
1284
+ " border-right-color: var(--fill-color);\n",
1285
+ " border-bottom-color: var(--fill-color);\n",
1286
+ " }\n",
1287
+ " 90% {\n",
1288
+ " border-color: transparent;\n",
1289
+ " border-bottom-color: var(--fill-color);\n",
1290
+ " }\n",
1291
+ " }\n",
1292
+ "</style>\n",
1293
+ "\n",
1294
+ " <script>\n",
1295
+ " async function quickchart(key) {\n",
1296
+ " const quickchartButtonEl =\n",
1297
+ " document.querySelector('#' + key + ' button');\n",
1298
+ " quickchartButtonEl.disabled = true; // To prevent multiple clicks.\n",
1299
+ " quickchartButtonEl.classList.add('colab-df-spinner');\n",
1300
+ " try {\n",
1301
+ " const charts = await google.colab.kernel.invokeFunction(\n",
1302
+ " 'suggestCharts', [key], {});\n",
1303
+ " } catch (error) {\n",
1304
+ " console.error('Error during call to suggestCharts:', error);\n",
1305
+ " }\n",
1306
+ " quickchartButtonEl.classList.remove('colab-df-spinner');\n",
1307
+ " quickchartButtonEl.classList.add('colab-df-quickchart-complete');\n",
1308
+ " }\n",
1309
+ " (() => {\n",
1310
+ " let quickchartButtonEl =\n",
1311
+ " document.querySelector('#df-83eb9e5f-bc38-42d4-8788-0e5ce29260c8 button');\n",
1312
+ " quickchartButtonEl.style.display =\n",
1313
+ " google.colab.kernel.accessAllowed ? 'block' : 'none';\n",
1314
+ " })();\n",
1315
+ " </script>\n",
1316
+ "</div>\n",
1317
+ " </div>\n",
1318
+ " </div>\n"
1319
+ ]
1320
+ },
1321
+ "metadata": {},
1322
+ "execution_count": 11
1323
+ }
1324
+ ]
1325
+ },
1326
+ {
1327
+ "cell_type": "code",
1328
+ "source": [
1329
+ "df_filtrado['new_tag'].unique()"
1330
+ ],
1331
+ "metadata": {
1332
+ "id": "tYBgwXRqdXvC",
1333
+ "colab": {
1334
+ "base_uri": "https://localhost:8080/"
1335
+ },
1336
+ "outputId": "77175ff8-d6ff-4323-b524-1154d2a68f0f"
1337
+ },
1338
+ "execution_count": null,
1339
+ "outputs": [
1340
+ {
1341
+ "output_type": "execute_result",
1342
+ "data": {
1343
+ "text/plain": [
1344
+ "array(['B-ORG', 'I-ORG', 'O', 'B-DATE', 'I-DATE', 'B-PER', 'I-PER',\n",
1345
+ " 'B-VIO', 'I-VIO', 'B-ARM', 'I-ARM', 'B-GEO', 'B-AFE', 'I-AFE',\n",
1346
+ " 'I-GEO', 'B-PAZ', 'I-PAZ'], dtype=object)"
1347
+ ]
1348
+ },
1349
+ "metadata": {},
1350
+ "execution_count": 12
1351
+ }
1352
+ ]
1353
+ },
1354
+ {
1355
+ "cell_type": "markdown",
1356
+ "source": [
1357
+ "### Conteo para el dataset."
1358
+ ],
1359
+ "metadata": {
1360
+ "id": "2VzOEn9jZyqg"
1361
+ }
1362
+ },
1363
+ {
1364
+ "cell_type": "code",
1365
+ "source": [
1366
+ "[elemento for elemento in df_filtrado['new_tag'].unique() if elemento.startswith('B-')]"
1367
+ ],
1368
+ "metadata": {
1369
+ "colab": {
1370
+ "base_uri": "https://localhost:8080/"
1371
+ },
1372
+ "id": "vrrTam09cYYr",
1373
+ "outputId": "eed5c6af-1c0b-4267-d367-5374532efa47"
1374
+ },
1375
+ "execution_count": null,
1376
+ "outputs": [
1377
+ {
1378
+ "output_type": "execute_result",
1379
+ "data": {
1380
+ "text/plain": [
1381
+ "['B-ORG', 'B-DATE', 'B-PER', 'B-VIO', 'B-ARM', 'B-GEO', 'B-AFE', 'B-PAZ']"
1382
+ ]
1383
+ },
1384
+ "metadata": {},
1385
+ "execution_count": 32
1386
+ }
1387
+ ]
1388
+ },
1389
+ {
1390
+ "cell_type": "code",
1391
+ "source": [
1392
+ "df_filtrado[df_filtrado['new_tag']!='O'].shape[0]"
1393
+ ],
1394
+ "metadata": {
1395
+ "colab": {
1396
+ "base_uri": "https://localhost:8080/"
1397
+ },
1398
+ "id": "TsEfHktNabJx",
1399
+ "outputId": "24d08843-398d-451f-825b-ad79b594b438"
1400
+ },
1401
+ "execution_count": null,
1402
+ "outputs": [
1403
+ {
1404
+ "output_type": "execute_result",
1405
+ "data": {
1406
+ "text/plain": [
1407
+ "2037"
1408
+ ]
1409
+ },
1410
+ "metadata": {},
1411
+ "execution_count": 27
1412
+ }
1413
+ ]
1414
+ },
1415
+ {
1416
+ "cell_type": "code",
1417
+ "source": [
1418
+ "print(f\"El total de oraciones es: {int(df_filtrado['sentence_id'].max())}\")\n",
1419
+ "print(f\"El total de tokens es: {int(df_filtrado.shape[0])}\")\n",
1420
+ "print(f\"El total de categorías es: {len([elemento for elemento in df_filtrado['new_tag'].unique() if elemento.startswith('B-')])}\")\n",
1421
+ "print(f\"El total de tokens en alguna categoría es: {df_filtrado[df_filtrado['new_tag']!='O'].shape[0]}\")\n",
1422
+ "print(\"Distribución por clases.\")\n",
1423
+ "for tag in [elemento for elemento in df_filtrado['new_tag'].unique() if elemento.startswith('B-')]:\n",
1424
+ " print(f\"\\t{tag[2:]} aparece {df_filtrado[df_filtrado['new_tag']==tag].shape[0]} veces.\")"
1425
+ ],
1426
+ "metadata": {
1427
+ "colab": {
1428
+ "base_uri": "https://localhost:8080/"
1429
+ },
1430
+ "id": "SEMObliXZyLE",
1431
+ "outputId": "5dc48a67-1175-4fa4-f77d-1bee8df78596"
1432
+ },
1433
+ "execution_count": null,
1434
+ "outputs": [
1435
+ {
1436
+ "output_type": "stream",
1437
+ "name": "stdout",
1438
+ "text": [
1439
+ "El total de oraciones es: 658\n",
1440
+ "El total de tokens es: 14485\n",
1441
+ "El total de categorías es: 8\n",
1442
+ "El total de tokens en alguna categoría es: 2037\n",
1443
+ "Distribución por clases.\n",
1444
+ "\tORG aparece 144 veces.\n",
1445
+ "\tDATE aparece 57 veces.\n",
1446
+ "\tPER aparece 216 veces.\n",
1447
+ "\tVIO aparece 158 veces.\n",
1448
+ "\tARM aparece 89 veces.\n",
1449
+ "\tGEO aparece 154 veces.\n",
1450
+ "\tAFE aparece 49 veces.\n",
1451
+ "\tPAZ aparece 62 veces.\n"
1452
+ ]
1453
+ }
1454
+ ]
1455
+ },
1456
+ {
1457
+ "cell_type": "markdown",
1458
+ "source": [
1459
+ "### Procesod e entrenamiento."
1460
+ ],
1461
+ "metadata": {
1462
+ "id": "pWEIM9KoZ2-D"
1463
+ }
1464
+ },
1465
+ {
1466
+ "cell_type": "code",
1467
+ "source": [
1468
+ "# Split the DataFrame into train, test, and dev sets based on sentence_ids\n",
1469
+ "train_ids, test_ids = train_test_split(df_filtrado['sentence_id'].unique(), test_size=0.2, random_state=42)\n",
1470
+ "train_ids, dev_ids = train_test_split(train_ids, test_size=0.1, random_state=42)\n",
1471
+ "\n",
1472
+ "# Define a function to format rows as \"word label\"\n",
1473
+ "def format_row(word, label):\n",
1474
+ " return f\"{word} {label}\"\n",
1475
+ "\n",
1476
+ "# Save data into separate files based on sentence_ids\n",
1477
+ "for split_name, split_ids in [('train', train_ids), ('test', test_ids), ('dev', dev_ids)]:\n",
1478
+ " split_data = df_filtrado[df_filtrado['sentence_id'].isin(split_ids)]\n",
1479
+ "\n",
1480
+ " formatted_rows = []\n",
1481
+ " for _, row in split_data.groupby('sentence_id'):\n",
1482
+ " formatted_rows.extend([format_row(word, label) for word, label in zip(row['text'], row['new_tag'])])\n",
1483
+ " formatted_rows.append(\"\") # Append a blank line after each sentence\n",
1484
+ "\n",
1485
+ " with open(f'{split_name}.txt', 'w') as file:\n",
1486
+ " file.write('\\n'.join(formatted_rows))"
1487
+ ],
1488
+ "metadata": {
1489
+ "id": "rSMvEdUWdrYp"
1490
+ },
1491
+ "execution_count": null,
1492
+ "outputs": []
1493
+ },
1494
+ {
1495
+ "cell_type": "code",
1496
+ "source": [
1497
+ "# define columns\n",
1498
+ "columns = {0 : 'text', 1 : 'ner'}\n",
1499
+ "\n",
1500
+ "# directory where the data resides\n",
1501
+ "data_folder = '/content/'\n",
1502
+ "\n",
1503
+ "# initializing the corpus\n",
1504
+ "corpus: Corpus = ColumnCorpus(data_folder, columns,\n",
1505
+ " train_file = 'train.txt',\n",
1506
+ " test_file = 'test.txt',\n",
1507
+ " dev_file = 'dev.txt')"
1508
+ ],
1509
+ "metadata": {
1510
+ "id": "zVgB1yp6effD",
1511
+ "colab": {
1512
+ "base_uri": "https://localhost:8080/"
1513
+ },
1514
+ "outputId": "1e611d0e-9436-49a7-bcbd-f47aabf7c515"
1515
+ },
1516
+ "execution_count": null,
1517
+ "outputs": [
1518
+ {
1519
+ "output_type": "stream",
1520
+ "name": "stdout",
1521
+ "text": [
1522
+ "2023-11-10 23:10:28,046 Reading data from /content\n",
1523
+ "2023-11-10 23:10:28,048 Train: /content/train.txt\n",
1524
+ "2023-11-10 23:10:28,050 Dev: /content/dev.txt\n",
1525
+ "2023-11-10 23:10:28,051 Test: /content/test.txt\n"
1526
+ ]
1527
+ }
1528
+ ]
1529
+ },
1530
+ {
1531
+ "cell_type": "code",
1532
+ "source": [
1533
+ "# tag to predict\n",
1534
+ "tag_type = 'ner'# make tag dictionary from the corpus\n",
1535
+ "tag_dictionary = corpus.make_label_dictionary(label_type=tag_type)"
1536
+ ],
1537
+ "metadata": {
1538
+ "id": "VeMVuHskefyp",
1539
+ "colab": {
1540
+ "base_uri": "https://localhost:8080/"
1541
+ },
1542
+ "outputId": "6c4051f4-46fe-4298-ab5c-91fc646811ee"
1543
+ },
1544
+ "execution_count": null,
1545
+ "outputs": [
1546
+ {
1547
+ "output_type": "stream",
1548
+ "name": "stdout",
1549
+ "text": [
1550
+ "2023-11-10 23:10:30,743 Computing label dictionary. Progress:\n"
1551
+ ]
1552
+ },
1553
+ {
1554
+ "output_type": "stream",
1555
+ "name": "stderr",
1556
+ "text": [
1557
+ "0it [00:00, ?it/s]\n",
1558
+ "378it [00:00, 16853.55it/s]"
1559
+ ]
1560
+ },
1561
+ {
1562
+ "output_type": "stream",
1563
+ "name": "stdout",
1564
+ "text": [
1565
+ "2023-11-10 23:10:30,819 Dictionary created for label 'ner' with 8 values: PER (seen 164 times), GEO (seen 116 times), VIO (seen 113 times), ORG (seen 102 times), ARM (seen 65 times), PAZ (seen 42 times), DATE (seen 40 times), AFE (seen 33 times)\n"
1566
+ ]
1567
+ },
1568
+ {
1569
+ "output_type": "stream",
1570
+ "name": "stderr",
1571
+ "text": [
1572
+ "\n"
1573
+ ]
1574
+ }
1575
+ ]
1576
+ },
1577
+ {
1578
+ "cell_type": "code",
1579
+ "source": [
1580
+ "embedding_types : List = [\n",
1581
+ " WordEmbeddings('glove'),\n",
1582
+ " FlairEmbeddings('es-forward'),\n",
1583
+ " FlairEmbeddings('es-backward'),\n",
1584
+ " #bert_embedding\n",
1585
+ " ]\n",
1586
+ "\n",
1587
+ "embeddings : StackedEmbeddings = StackedEmbeddings(embeddings=embedding_types)"
1588
+ ],
1589
+ "metadata": {
1590
+ "id": "DMgwBh1aeisC"
1591
+ },
1592
+ "execution_count": null,
1593
+ "outputs": []
1594
+ },
1595
+ {
1596
+ "cell_type": "code",
1597
+ "source": [
1598
+ "tagger : SequenceTagger = SequenceTagger(hidden_size=256,\n",
1599
+ " embeddings=embeddings,\n",
1600
+ " tag_dictionary=tag_dictionary,\n",
1601
+ " tag_type=tag_type,\n",
1602
+ " use_crf=True,\n",
1603
+ " tag_format='BIO')"
1604
+ ],
1605
+ "metadata": {
1606
+ "id": "W9XpEufIeqIB"
1607
+ },
1608
+ "execution_count": null,
1609
+ "outputs": []
1610
+ },
1611
+ {
1612
+ "cell_type": "code",
1613
+ "source": [
1614
+ "trainer : ModelTrainer = ModelTrainer(tagger, corpus)\n",
1615
+ "trainer.train('resources/taggers/example-ner',\n",
1616
+ " learning_rate=0.05,\n",
1617
+ " mini_batch_size=2,\n",
1618
+ " max_epochs=100)"
1619
+ ],
1620
+ "metadata": {
1621
+ "id": "S2tlEOrEe2x0"
1622
+ },
1623
+ "execution_count": null,
1624
+ "outputs": []
1625
+ },
1626
+ {
1627
+ "cell_type": "code",
1628
+ "source": [
1629
+ "# load the trained model\n",
1630
+ "model = SequenceTagger.load('/content/resources/taggers/example-ner/final-model.pt')\n",
1631
+ "\n",
1632
+ "# create example sentence\n",
1633
+ "sentence = Sentence('Los principales perpetradores de esta violación fueron los paramilitares seguidos de guerrillas como las FARC y el ELN y fuerza pública y otros agentes del Estado como los organismos de inteligencia')# predict the tags\n",
1634
+ "sentence2 = Sentence('Hola. Mi nombre es David Cortés, tengo 27 años de edad y soy de Tumaco, Nariño. Llegué a Bogotá cuando tenía 17 años por temas de seguridad. Mi mamá me envío a esta ciudad porque mi vida estaba en riesgo luego de que, por accidente, fuera testigo de la muerte de un narcotraficante. Todo el barrio estaba lleno de narcos')\n",
1635
+ "sentence3 = Sentence('Mi mamá me envío a esta ciudad porque mi vida estaba en riesgo luego de que, por accidente, fuera testigo de la muerte de un narcotraficante. Todo el barrio estaba lleno de narcos')\n",
1636
+ "model.predict(sentence)\n",
1637
+ "model.predict(sentence2)\n",
1638
+ "model.predict(sentence3)\n",
1639
+ "\n",
1640
+ "print(sentence.to_tagged_string())\n",
1641
+ "print(sentence2.to_tagged_string())\n",
1642
+ "print(sentence3.to_tagged_string())"
1643
+ ],
1644
+ "metadata": {
1645
+ "id": "11DieTPge5vb"
1646
+ },
1647
+ "execution_count": null,
1648
+ "outputs": []
1649
+ },
1650
+ {
1651
+ "cell_type": "code",
1652
+ "source": [],
1653
+ "metadata": {
1654
+ "id": "Ci2gJc_Gg7qq"
1655
+ },
1656
+ "execution_count": null,
1657
+ "outputs": []
1658
+ }
1659
+ ]
1660
+ }