У меня есть список соскобленного текста из интернета. сказать "слова1"
Я извлек текст из PDF-файла, преобразовал его в токены, а затем удалил все стоп-слова. сказать "слова2"
Оба типа списка
words1 имеет 98123 элементов
words2 имеет 42752 элементов
Я пытаюсь пересечь слова 1 и слова 2, и с этим я сталкиваюсь
...out = set(words1) & set(words2)
Traceback (most recent call last):
File "<ipython-input-80-57a21dd3f370>", line 1, in <module>
out = set(words1) & set(words2)
File "C:\Users\Sam\Anaconda3\envs\GPU\lib\site-packages\PyPDF2\pdf.py", line 1069, in _showwarning
file.write(formatWarning(message, category, filename, lineno, line))
File "C:\Users\Sam\Anaconda3\envs\GPU\lib\site-packages\PyPDF2\utils.py", line 69, in formatWarning
file = filename.replace("/", "\\").rsplit("\\", 1)[1] # find the file name
IndexError: list index out of range
...
Но когда я конвертирую PDF в текстовый файл онлайн и извлекаю текст: произнесите "words3"
код
out = set (words1) и set (words3)
Я получаю общие слова без ошибок.
Нужна помощь.
полный код:
import PyPDF2
import textract
from nltk.tokenize import word_tokenize
from nltk.corpus import stopwords
fileurl = 'C://apache-ctakes-4.0.0//cpg.pdf'
filename = 'cpg.pdf'
pdfobj = open(filename,'rb')
pdfreader = PyPDF2.PdfFileReader(pdfobj)
num_pages = pdfreader.numPages
count = 0
text = ""
while count < num_pages:
pageobj = pdfreader.getPage(count)
count +=1
text += pageobj.extractText()
if text != "":
text = text
else:
text = textract.process(fileurl, method = 'tesseract', language = 'eng')
tokens = word_tokenize(text)
punctuations = ['(',')',';',':','[',']',',']
stop_words = stopwords.words('english')
words2 = [word for word in tokens if not word in stop_words]
f1 = open("clinicalterms.txt", 'r')
f3 = open("trymatch.txt",'r')
words1 = f1.read().split()
words3 = f3.read().split()
out = set(words1) & set(words2)