Предполагается, что полиглот установлен правильно и в Pycharm выбрана правильная среда. Если нет, установите полиглот в new conda environment
с необходимыми требованиями. Создайте новый проект и выберите существующую среду conda в pycharm. Если модели language embeddings
, ner
не downloaded
, их следует загрузить.
Код:
from polyglot.text import Text
blob = """, which was equalled five days ago by South Africa in the victory over West Indies in Sydney."""
text = Text(blob)
text.language = "en"
## As list all detected entities
print("As list all detected entities")
print(text.entities)
print()
## Separately shown detected entities
print("Separately shown detected entities")
for entity in text.entities:
print(entity.tag, entity)
print()
## Tokenized words of sentence
print("Tokenized words of sentence")
print(text.words)
print()
## For each token try named entity recognition.
## Not very reliable it detects some words as not English and tries other languages.
## If other embeddings are not installed or text.language = "en" is commented then it may give error.
print("For each token try named entity recognition")
for word in text.words:
text = Text(word)
text.language = "en"
## Separately
for entity in text.entities:
print(entity.tag, entity)
Вывод:
As list all detected entities
[I-LOC(['South', 'Africa']), I-ORG(['West', 'Indies']), I-LOC(['Sydney'])]
Separately shown detected entities
I-LOC ['South', 'Africa']
I-ORG ['West', 'Indies']
I-LOC ['Sydney']
Tokenized words of sentence
[',', 'which', 'was', 'equalled', 'five', 'days', 'ago', 'by', 'South', 'Africa', 'in', 'the', 'victory', 'over', 'West', 'Indies', 'in', 'Sydney', '.']
For each token try named entity recognition
I-LOC ['Africa']
I-PER ['Sydney']