Как я могу запустить этот токен / тэг Polyglot в PyCharm? - PullRequest
0 голосов
/ 13 февраля 2020

Я оцениваю различные библиотеки распознавания именованных объектов (NER) и пробую Полиглот .

Кажется, все идет хорошо, но инструкции говорят мне использовать эту строку в командной строке:

!polyglot --lang en tokenize --input testdata/cricket.txt |  polyglot --lang en ner | tail -n 20

..., которая должна выдавать (в примере) этот вывод:

,               O
which           O
was             O
equalled        O
five            O
days            O
ago             O
by              O
South           I-LOC
Africa          I-LOC
in              O
their           O
victory         O
over            O
West            I-ORG
Indies          I-ORG
in              O
Sydney          I-LOC
.               O

Это именно тот вывод, который мне нужен для моего проекта, и он работает точно так же, как он мне нужен для работы; однако мне нужно запустить его в моем интерфейсе PyCharm, а не в командной строке, и сохранить результаты в pandas фрейме данных. Как мне перевести эту команду?

1 Ответ

0 голосов
/ 13 февраля 2020

Предполагается, что полиглот установлен правильно и в 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']
...