目前,我的代码正在从 PDF 中提取数据并计算词频。我一直在尝试按频率顺序排列它,但一直没能做到。我查看了多个类似的答案,但找不到可以开始工作的答案。有人可以指出我需要做什么吗?
import PyPDF2
import re
pdfFileObj = open('ch8.pdf', 'rb') #Open the File
pdfReader = PyPDF2.PdfFileReader(pdfFileObj) #Read the file
frequency = {} #Create dict
print "Number of Pages %s " % pdfReader.numPages #Print Num Pages
pageObj = pdfReader.getPage(0) # Get the first page
match_pattern = re.findall(r'\b[a-z]{3,15}\b', pageObj.extractText()) #Find the text
for word in match_pattern: #Start counting the frequency
word = word.lower()
count = frequency.get(word,0)
frequency[word] = count + 1
frequency_list = frequency.keys()
for words in frequency_list:
print words, frequency[words]
提前致谢。