update README
Browse files
README.md
CHANGED
|
@@ -1,17 +1,18 @@
|
|
| 1 |
-
|
| 2 |
---
|
| 3 |
language: ar
|
| 4 |
datasets:
|
| 5 |
- Marefa-NER
|
|
|
|
|
|
|
| 6 |
---
|
| 7 |
|
| 8 |
# Tebyan تبيـان
|
| 9 |
## Marefa Arabic Named Entity Recognition Model
|
| 10 |
## نموذج المعرفة لتصنيف أجزاء النص
|
| 11 |
---------
|
| 12 |
-
**Version**: 1.
|
| 13 |
|
| 14 |
-
**Last Update:**
|
| 15 |
|
| 16 |
## Model description
|
| 17 |
|
|
@@ -38,152 +39,131 @@ Install the following Python packages
|
|
| 38 |
|
| 39 |
> If you are using `Google Colab`, please restart your runtime after installing the packages.
|
| 40 |
|
| 41 |
-
[**OPTIONAL**]
|
| 42 |
-
Using of an Arabic segmentation tool approved better results in many scenarios. If you want to use `FarasaPy` to segment the texts, please ensure that you have `openjdk-11` installed in your machine, then install the package via:
|
| 43 |
-
```bash
|
| 44 |
-
# install openjdk-11-jdk
|
| 45 |
-
$ apt-get install -y build-essential
|
| 46 |
-
$ apt-get install -y openjdk-11-jdk
|
| 47 |
-
|
| 48 |
-
# instll FarasaPy
|
| 49 |
-
$ pip3 install farasapy==0.0.13
|
| 50 |
-
```
|
| 51 |
-
|
| 52 |
-
*Do not forget to set `USE_FARASAPY` to `True` in the following code*
|
| 53 |
-
|
| 54 |
-
Also, you can set `USE_SENTENCE_TOKENIZER` to `True` for getting better results for long texts.
|
| 55 |
|
| 56 |
-----------
|
| 57 |
|
| 58 |
```python
|
| 59 |
|
| 60 |
# ==== Set configurations
|
| 61 |
-
|
| 62 |
-
|
| 63 |
-
|
| 64 |
-
# do you want to split text into sentences [better for long texts] ?
|
| 65 |
-
USE_SENTENCE_TOKENIZER = False # set to True to use it
|
| 66 |
-
|
| 67 |
-
# ==== Import required modules
|
| 68 |
-
import logging
|
| 69 |
-
import re
|
| 70 |
|
|
|
|
| 71 |
import nltk
|
| 72 |
nltk.download('punkt')
|
| 73 |
-
from nltk.tokenize import word_tokenize
|
| 74 |
|
| 75 |
-
|
| 76 |
|
| 77 |
-
|
| 78 |
-
|
| 79 |
-
|
|
|
|
| 80 |
|
| 81 |
-
|
| 82 |
-
|
| 83 |
-
current_ent = {}
|
| 84 |
|
| 85 |
-
|
| 86 |
-
|
| 87 |
-
|
| 88 |
-
|
| 89 |
-
continue
|
| 90 |
|
| 91 |
-
|
| 92 |
-
|
| 93 |
-
|
| 94 |
-
|
| 95 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 96 |
|
| 97 |
-
|
| 98 |
-
|
| 99 |
-
|
| 100 |
-
|
|
|
|
|
|
|
| 101 |
|
| 102 |
-
|
| 103 |
-
|
| 104 |
-
|
| 105 |
-
|
| 106 |
-
|
| 107 |
-
|
| 108 |
-
|
| 109 |
-
|
| 110 |
-
|
| 111 |
-
|
| 112 |
-
text = text.replace(s, t)
|
| 113 |
-
return text
|
| 114 |
-
|
| 115 |
-
_ = _segment_text("نص تجريبي للتأكد من عمل الأداة", segmenter)
|
| 116 |
-
|
| 117 |
-
custom_labels = ["O", "B-job", "I-job", "B-nationality", "B-person", "I-person", "B-location",
|
| 118 |
-
"B-time", "I-time", "B-event", "I-event", "B-organization", "I-organization",
|
| 119 |
-
"I-location", "I-nationality", "B-product", "I-product", "B-artwork", "I-artwork"]
|
| 120 |
-
|
| 121 |
-
# ==== Import/Download the NER Model
|
| 122 |
-
m_name = "marefa-nlp/marefa-ner"
|
| 123 |
-
tokenizer = AutoTokenizer.from_pretrained(m_name)
|
| 124 |
-
model = AutoModelForTokenClassification.from_pretrained(m_name)
|
| 125 |
-
|
| 126 |
-
ar_ner = pipeline("ner", model=model, tokenizer=tokenizer, grouped_entities=True, aggregation_strategy="simple")
|
| 127 |
-
|
| 128 |
-
# ==== Model Inference
|
| 129 |
samples = [
|
| 130 |
"تلقى تعليمه في الكتاب ثم انضم الى الأزهر عام 1873م. تعلم على يد السيد جمال الدين الأفغاني والشيخ محمد عبده",
|
| 131 |
"بعد عودته إلى القاهرة، التحق نجيب الريحاني فرقة جورج أبيض، الذي كان قد ضمَّ - قُبيل ذلك - فرقته إلى فرقة سلامة حجازي . و منها ذاع صيته",
|
|
|
|
|
|
|
| 132 |
"امبارح اتفرجت على مباراة مانشستر يونايتد مع ريال مدريد في غياب الدون كرستيانو رونالدو",
|
| 133 |
-
"
|
| 134 |
]
|
| 135 |
|
| 136 |
# [optional]
|
| 137 |
samples = [ " ".join(word_tokenize(sample.strip())) for sample in samples if sample.strip() != "" ]
|
| 138 |
|
| 139 |
for sample in samples:
|
| 140 |
-
ents =
|
| 141 |
-
|
| 142 |
-
|
| 143 |
-
sample = _segment_text(sample, segmenter)
|
| 144 |
-
|
| 145 |
-
if USE_SENTENCE_TOKENIZER:
|
| 146 |
-
for sent in sent_tokenize(sample):
|
| 147 |
-
ents += _extract_ner(sent, ar_ner)
|
| 148 |
-
else:
|
| 149 |
-
ents = _extract_ner(sample, ar_ner)
|
| 150 |
-
|
| 151 |
-
# print the results
|
| 152 |
-
print("(", sample, ")")
|
| 153 |
for ent in ents:
|
| 154 |
-
print("\t",
|
| 155 |
-
print("
|
| 156 |
|
| 157 |
```
|
| 158 |
|
| 159 |
Output
|
| 160 |
|
| 161 |
```
|
| 162 |
-
|
| 163 |
-
الأزهر
|
| 164 |
-
عام 1873م
|
| 165 |
-
جمال الدين الأفغاني
|
| 166 |
-
محمد عبده
|
| 167 |
-
|
| 168 |
-
|
| 169 |
-
|
| 170 |
-
|
| 171 |
-
نجيب الريحاني
|
| 172 |
-
فرقة جورج
|
| 173 |
-
فرقة سلامة حجازي
|
| 174 |
-
|
| 175 |
-
|
| 176 |
-
|
| 177 |
-
|
| 178 |
-
|
| 179 |
-
|
| 180 |
-
|
| 181 |
-
|
| 182 |
-
|
| 183 |
-
|
| 184 |
-
|
| 185 |
-
|
| 186 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 187 |
```
|
| 188 |
|
| 189 |
## Fine-Tuning
|
|
|
|
|
|
|
| 1 |
---
|
| 2 |
language: ar
|
| 3 |
datasets:
|
| 4 |
- Marefa-NER
|
| 5 |
+
widget:
|
| 6 |
+
- text: "في استاد القاهرة، بدأ حفل افتتاح بطولة كأس الأمم الأفريقية بحضور رئيس الجمهورية و رئيس الاتحاد الدولي لكرة القدم"
|
| 7 |
---
|
| 8 |
|
| 9 |
# Tebyan تبيـان
|
| 10 |
## Marefa Arabic Named Entity Recognition Model
|
| 11 |
## نموذج المعرفة لتصنيف أجزاء النص
|
| 12 |
---------
|
| 13 |
+
**Version**: 1.3
|
| 14 |
|
| 15 |
+
**Last Update:** 3-12-2021
|
| 16 |
|
| 17 |
## Model description
|
| 18 |
|
|
|
|
| 39 |
|
| 40 |
> If you are using `Google Colab`, please restart your runtime after installing the packages.
|
| 41 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 42 |
|
| 43 |
-----------
|
| 44 |
|
| 45 |
```python
|
| 46 |
|
| 47 |
# ==== Set configurations
|
| 48 |
+
from transformers import AutoTokenizer, AutoModelForTokenClassification
|
| 49 |
+
import torch
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 50 |
|
| 51 |
+
import numpy as np
|
| 52 |
import nltk
|
| 53 |
nltk.download('punkt')
|
| 54 |
+
from nltk.tokenize import word_tokenize
|
| 55 |
|
| 56 |
+
custom_labels = ["O", "B-job", "I-job", "B-nationality", "B-person", "I-person", "B-location","B-time", "I-time", "B-event", "I-event", "B-organization", "I-organization", "I-location", "I-nationality", "B-product", "I-product", "B-artwork", "I-artwork"]
|
| 57 |
|
| 58 |
+
def _extract_ner(text: str, model: AutoModelForTokenClassification,
|
| 59 |
+
tokenizer: AutoTokenizer, start_token: str="▁"):
|
| 60 |
+
tokenized_sentence = tokenizer([text], padding=True, truncation=True, return_tensors="pt")
|
| 61 |
+
tokenized_sentences = tokenized_sentence['input_ids'].numpy()
|
| 62 |
|
| 63 |
+
with torch.no_grad():
|
| 64 |
+
output = model(**tokenized_sentence)
|
|
|
|
| 65 |
|
| 66 |
+
last_hidden_states = output[0].numpy()
|
| 67 |
+
label_indices = np.argmax(last_hidden_states[0], axis=1)
|
| 68 |
+
tokens = tokenizer.convert_ids_to_tokens(tokenized_sentences[0])
|
| 69 |
+
special_tags = set(tokenizer.special_tokens_map.values())
|
|
|
|
| 70 |
|
| 71 |
+
grouped_tokens = []
|
| 72 |
+
for token, label_idx in zip(tokens, label_indices):
|
| 73 |
+
if token not in special_tags:
|
| 74 |
+
if not token.startswith(start_token) and len(token.replace(start_token,"").strip()) > 0:
|
| 75 |
+
grouped_tokens[-1]["token"] += token
|
| 76 |
+
else:
|
| 77 |
+
grouped_tokens.append({"token": token, "label": custom_labels[label_idx]})
|
| 78 |
+
|
| 79 |
+
# extract entities
|
| 80 |
+
ents = []
|
| 81 |
+
prev_label = "O"
|
| 82 |
+
for token in grouped_tokens:
|
| 83 |
+
label = token["label"].replace("I-","").replace("B-","")
|
| 84 |
+
if token["label"] != "O":
|
| 85 |
|
| 86 |
+
if label != prev_label:
|
| 87 |
+
ents.append({"token": [token["token"]], "label": label})
|
| 88 |
+
else:
|
| 89 |
+
ents[-1]["token"].append(token["token"])
|
| 90 |
+
|
| 91 |
+
prev_label = label
|
| 92 |
|
| 93 |
+
# group tokens
|
| 94 |
+
ents = [{"token": "".join(rec["token"]).replace(start_token," ").strip(), "label": rec["label"]} for rec in ents ]
|
| 95 |
+
|
| 96 |
+
return ents
|
| 97 |
+
|
| 98 |
+
model_cp = "marefa-nlp/marefa-ner"
|
| 99 |
+
|
| 100 |
+
tokenizer = AutoTokenizer.from_pretrained(model_cp)
|
| 101 |
+
model = AutoModelForTokenClassification.from_pretrained(model_cp, num_labels=len(custom_labels))
|
| 102 |
+
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 103 |
samples = [
|
| 104 |
"تلقى تعليمه في الكتاب ثم انضم الى الأزهر عام 1873م. تعلم على يد السيد جمال الدين الأفغاني والشيخ محمد عبده",
|
| 105 |
"بعد عودته إلى القاهرة، التحق نجيب الريحاني فرقة جورج أبيض، الذي كان قد ضمَّ - قُبيل ذلك - فرقته إلى فرقة سلامة حجازي . و منها ذاع صيته",
|
| 106 |
+
"في استاد القاهرة، قام حفل افتتاح بطولة كأس الأمم الأفريقية بحضور رئيس الجمهورية و رئيس الاتحاد الدولي لكرة القدم",
|
| 107 |
+
"من فضلك أرسل هذا البريد الى صديقي جلال الدين في تمام الساعة الخامسة صباحا في يوم الثلاثاء القادم",
|
| 108 |
"امبارح اتفرجت على مباراة مانشستر يونايتد مع ريال مدريد في غياب الدون كرستيانو رونالدو",
|
| 109 |
+
"لا تنسى تصحيني الساعة سبعة, و ضيف في الجدول اني احضر مباراة نادي النصر غدا",
|
| 110 |
]
|
| 111 |
|
| 112 |
# [optional]
|
| 113 |
samples = [ " ".join(word_tokenize(sample.strip())) for sample in samples if sample.strip() != "" ]
|
| 114 |
|
| 115 |
for sample in samples:
|
| 116 |
+
ents = _extract_ner(text=sample, model=model, tokenizer=tokenizer, start_token="▁")
|
| 117 |
+
|
| 118 |
+
print(sample)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 119 |
for ent in ents:
|
| 120 |
+
print("\t",ent["token"],"==>",ent["label"])
|
| 121 |
+
print("========\n")
|
| 122 |
|
| 123 |
```
|
| 124 |
|
| 125 |
Output
|
| 126 |
|
| 127 |
```
|
| 128 |
+
تلقى تعليمه في الكتاب ثم انضم الى الأزهر عام 1873م . تعلم على يد السيد جمال الدين الأفغاني والشيخ محمد عبده
|
| 129 |
+
الأزهر ==> organization
|
| 130 |
+
عام 1873م ==> time
|
| 131 |
+
السيد جمال الدين الأفغاني ==> person
|
| 132 |
+
محمد عبده ==> person
|
| 133 |
+
========
|
| 134 |
+
|
| 135 |
+
بعد عودته إلى القاهرة، التحق نجيب الريحاني فرقة جورج أبيض، الذي كان قد ضمَّ - قُبيل ذلك - فرقته إلى فرقة سلامة حجازي . و منها ذاع صيته
|
| 136 |
+
القاهر��، ==> location
|
| 137 |
+
نجيب الريحاني ==> person
|
| 138 |
+
فرقة جورج أبيض، ==> organization
|
| 139 |
+
فرقة سلامة حجازي ==> organization
|
| 140 |
+
========
|
| 141 |
+
|
| 142 |
+
في استاد القاهرة، قام حفل افتتاح بطولة كأس الأمم الأفريقية بحضور رئيس الجمهورية و رئيس الاتحاد الدولي لكرة القدم
|
| 143 |
+
استاد القاهرة، ==> location
|
| 144 |
+
بطولة كأس الأمم الأفريقية ==> event
|
| 145 |
+
رئيس الجمهورية ==> job
|
| 146 |
+
رئيس ==> job
|
| 147 |
+
الاتحاد الدولي لكرة القدم ==> organization
|
| 148 |
+
========
|
| 149 |
+
|
| 150 |
+
من فضلك أرسل هذا البريد الى صديقي جلال الدين في تمام الساعة الخامسة صباحا في يوم الثلاثاء القادم
|
| 151 |
+
جلال الدين ==> person
|
| 152 |
+
الساعة الخامسة صباحا ==> time
|
| 153 |
+
يوم الثلاثاء القادم ==> time
|
| 154 |
+
========
|
| 155 |
+
|
| 156 |
+
امبارح اتفرجت على مباراة مانشستر يونايتد مع ريال مدريد في غياب الدون كرستيانو رونالدو
|
| 157 |
+
مانشستر يونايتد ==> organization
|
| 158 |
+
ريال مدريد ==> organization
|
| 159 |
+
كرستيانو رونالدو ==> person
|
| 160 |
+
========
|
| 161 |
+
|
| 162 |
+
لا تنسى تصحيني الساعة سبعة , و ضيف في الجدول اني احضر مباراة نادي النصر غدا
|
| 163 |
+
الساعة سبعة ==> time
|
| 164 |
+
نادي النصر ==> organization
|
| 165 |
+
غدا ==> time
|
| 166 |
+
========
|
| 167 |
```
|
| 168 |
|
| 169 |
## Fine-Tuning
|