Как удалить \ n с экрана вывода при использовании sent_tokenize с помощью nltk - PullRequest
2 голосов
/ 01 июля 2019

Я использую токенайзер предложений, но как я могу удалить ненужные / n из вывода

from nltk.tokenize import word_tokenize, sent_tokenize
from nltk.corpus import stopwords
import PyPDF2 as p2
pdf_file = open("Muhammad_CV.pdf", 'rb')
pdf_read = p2.PdfFileReader(pdf_file)
count = pdf_read.numPages

for i in range(count):
    page = pdf_read.getPage(i)
    text = page.extractText()                               #Extract text
    tokenized = sent_tokenize(text)                 #Token
    all_words = []
    for w in tokenized:
    all_words.append(w.lower())                  #Lower case
# ///////////////// Stop Words ///////////////////////////
    stop_words = set(stopwords.words('english'))
    filtered = []
    for w in all_words:
        if w not in stop_words:
        filtered.append(w)
    print(filtered)

Вывод, который я получаю:

{'the specialization includes:\n \n\n \nintroduction\n \nto\n \ndata\n \nscience\n \n\n \nbig\n \ndata\n \n&\n \ncloud\n \ncomputing\n \n\n \ndata\n \nmining\n \n\n \nmachine\n \nlearn\ning'}

Желаемый вывод:

{'the specialization includes: introduction to data science big data cloud\n computing data mining machine learning'}

Ответы [ 2 ]

0 голосов
/ 01 июля 2019
 text = '''\n Apple has quietly  hired Dr. Rajiv B. Kumar, a pediatric endocrinologist \n. He will continue working at the hospital part time \n '''

 tokenized_sent_before_remove_n = nltk.sent_tokenize(text)
 #op 
 ['\n Apple has quietly  hired Dr. Rajiv B. Kumar, a pediatric endocrinologist \n.',
'He will continue working at the hospital part time']


 tokenized_sent_after_remove_n = [x.replace('\n','') for x in tokenized_sent]
 #o/p
 [' Apple has quietly  hired Dr. Rajiv B. Kumar, a pediatric endocrinologist .',
 'He will continue working at the hospital part time']
0 голосов
/ 01 июля 2019

Вам просто нужно вызвать метод строки strip(), чтобы удалить окружающие пробелы.

Вот пример (также использующий понимания, потому что это питонский путь :))

from nltk.tokenize import word_tokenize, sent_tokenize
from nltk.corpus import stopwords
import PyPDF2 as p2
pdf_file = open("Muhammad_CV.pdf", 'rb')
pdf_read = p2.PdfFileReader(pdf_file)
count = pdf_read.numPages

for i in range(count):
    page = pdf_read.getPage(i)
    text = page.extractText()
    tokenized = sent_tokenize(text)
    all_words = [w.strip().lower() for w in tokenized]
    stop_words = set(stopwords.words('english'))
    filtered = [w for w in all_words if w not in stop_words]
    print(filtered)

РЕДАКТИРОВАТЬ: исправлено trim в strip:)

Добро пожаловать на сайт PullRequest, где вы можете задавать вопросы и получать ответы от других членов сообщества.
...