Update README.md
Browse files
README.md
CHANGED
|
@@ -51,15 +51,58 @@ Here’s how you can use the model:
|
|
| 51 |
```python
|
| 52 |
from transformers import AutoTokenizer, AutoModelForTokenClassification, pipeline
|
| 53 |
|
| 54 |
-
|
| 55 |
-
|
| 56 |
-
model = AutoModelForTokenClassification.from_pretrained(model_name)
|
| 57 |
|
| 58 |
nlp = pipeline("ner", model=model, tokenizer=tokenizer)
|
|
|
|
|
|
|
| 59 |
example = '''
|
| 60 |
"در سال ۱۴۰۱، شرکت علیبابا اعلام کرد که با همکاری بانک ملت، یک پروژه بزرگ برای توسعه زیرساختهای تجارت الکترونیک در ایران آغاز خواهد کرد.
|
| 61 |
این پروژه در تهران و اصفهان اجرا میشود و پیشبینی میشود تا پایان سال ۱۴۰۲ تکمیل شود."
|
| 62 |
'''
|
|
|
|
| 63 |
ner_results = nlp(example)
|
| 64 |
|
| 65 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 51 |
```python
|
| 52 |
from transformers import AutoTokenizer, AutoModelForTokenClassification, pipeline
|
| 53 |
|
| 54 |
+
tokenizer = AutoTokenizer.from_pretrained("Behpouyan/Behpouyan-NER")
|
| 55 |
+
model = AutoModelForTokenClassification.from_pretrained("Behpouyan/Behpouyan-NER")
|
|
|
|
| 56 |
|
| 57 |
nlp = pipeline("ner", model=model, tokenizer=tokenizer)
|
| 58 |
+
|
| 59 |
+
# Input example
|
| 60 |
example = '''
|
| 61 |
"در سال ۱۴۰۱، شرکت علیبابا اعلام کرد که با همکاری بانک ملت، یک پروژه بزرگ برای توسعه زیرساختهای تجارت الکترونیک در ایران آغاز خواهد کرد.
|
| 62 |
این پروژه در تهران و اصفهان اجرا میشود و پیشبینی میشود تا پایان سال ۱۴۰۲ تکمیل شود."
|
| 63 |
'''
|
| 64 |
+
# Get NER results
|
| 65 |
ner_results = nlp(example)
|
| 66 |
|
| 67 |
+
# Function to merge subword entities
|
| 68 |
+
def merge_entities(entities):
|
| 69 |
+
merged_results = []
|
| 70 |
+
current_entity = None
|
| 71 |
+
|
| 72 |
+
for entity in entities:
|
| 73 |
+
if entity['entity'].startswith("B-") or current_entity is None:
|
| 74 |
+
# Start a new entity
|
| 75 |
+
if current_entity:
|
| 76 |
+
merged_results.append(current_entity)
|
| 77 |
+
current_entity = {
|
| 78 |
+
"word": entity['word'].strip(),
|
| 79 |
+
"entity": entity['entity'][2:], # Remove "B-" prefix
|
| 80 |
+
"score": entity['score'],
|
| 81 |
+
"start": entity['start'],
|
| 82 |
+
"end": entity['end'],
|
| 83 |
+
}
|
| 84 |
+
elif entity['entity'].startswith("I-") and current_entity:
|
| 85 |
+
# Continue the current entity
|
| 86 |
+
current_entity['word'] += entity['word'].strip()
|
| 87 |
+
current_entity['score'] = min(current_entity['score'], entity['score']) # Use the lowest score
|
| 88 |
+
current_entity['end'] = entity['end']
|
| 89 |
+
|
| 90 |
+
# Add the last entity if any
|
| 91 |
+
if current_entity:
|
| 92 |
+
merged_results.append(current_entity)
|
| 93 |
+
|
| 94 |
+
return merged_results
|
| 95 |
+
|
| 96 |
+
# Merge the entities
|
| 97 |
+
merged_results = merge_entities(ner_results)
|
| 98 |
+
|
| 99 |
+
# Display the merged results
|
| 100 |
+
print("Named Entity Recognition Results:")
|
| 101 |
+
for entity in merged_results:
|
| 102 |
+
print(f"- Entity: {entity['word']}")
|
| 103 |
+
print(f" Type: {entity['entity']}")
|
| 104 |
+
print(f" Score: {entity['score']:.2f}")
|
| 105 |
+
print(f" Start: {entity['start']}, End: {entity['end']}")
|
| 106 |
+
print("-" * 40)
|
| 107 |
+
|
| 108 |
+
|