Files
VKR_2026/NER.py
2026-04-23 15:35:34 +05:00

28 lines
1.2 KiB
Python
Raw Blame History

This file contains ambiguous Unicode characters
This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
# 1 модуль - Распознавание именованных сущностей (NER)
import spacy
class NER:
"""
Класс для выделения именованных сущностей из текста с помощью библиотеки spaCy.
"""
def __init__(self):
self.nlp = spacy.load("ru_core_news_lg")
def extract_entities(self, text):
"""
Выделение именованных сущностей из текста
Использование: text (<текст>)
Возвращает: List[Dict[text, type]] - список словарей, каждый из которых содержит информацию об одной сущности
"""
doc = self.nlp(text)
entities = []
for ent in doc.ents:
entities.append({
'text': ent.text, # Текст сущности
'type': ent.label_, # Тип сущности (PER, LOC, ORG, DATE и т.д.)
'start': ent.start_char, # Начальная позиция в тексте
'end': ent.end_char # Конечная позиция в тексте
})
return entities