Как токенизировать предложение с помощью nlp - PullRequest
0 голосов
/ 08 апреля 2019

Я новичок в НЛП. Я пытаюсь токенизировать предложение, используя nlp на python 3.7.Так я использовал следующий код

import nltk
text4="This is the first sentence.A gallon of milk in the U.S. cost 
$2.99.Is this the third sentence?Yes,it is!"
x=nltk.sent_tokenize(text4)
x[0]

Я ожидал, что x [0] вернет первое предложение, но я получил

Out[4]: 'This is the first sentence.A gallon of milk in the U.S. cost $2.99.Is this the third sentence?Yes,it is!'

Я что-то не так делаю?

Ответы [ 2 ]

1 голос
/ 08 апреля 2019

NLTK sent_tokenizer плохо обрабатывает плохо форматированный текст. Если вы предоставите правильные расстояния, то это работает.

import nltk
nltk.download('punkt')
text4="This is the first sentence. A gallon of milk in the U.S. cost $2.99. Is this 
the third sentence? Yes, it is"
x=nltk.sent_tokenize(text4)
x[0]

OR Вы могли бы использовать это.

import re
text4 = "This is the first sentence. A gallon of milk in the U.S. cost 2.99. Is this 
the third sentence? Yes it is"
sentences = re.split(r' *[\.\?!][\'"\)\]]* *', text4)
sentences
1 голос
/ 08 апреля 2019

Чтобы токенайзер вел себя правильно, в предложениях должны быть правильные интервалы и знаки препинания:

import nltk

text4 = "This is a sentence. This is another sentence."
nltk.sent_tokenize(text4)

# ['This is a sentence.', 'This is another sentence.']

## Versus What you had before

nltk.sent_tokenize("This is a sentence.This is another sentence.")

# ['This is a sentence.This is another sentence.']
...