-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathPDFTextAnalytics.py
More file actions
43 lines (38 loc) · 1.22 KB
/
PDFTextAnalytics.py
File metadata and controls
43 lines (38 loc) · 1.22 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
import urllib.request
import urllib.response
import sys
import os, glob
import tika
from tika import parser
import http.client, urllib
import json
import re
tika.initVM()
def parsePDF(path):
documents = { 'documents': []}
count = 1
for file in glob.glob(path):
parsedPDF = parser.from_file(file)
text = parsedPDF["content"]
text = text.strip('\n')
text = text.encode('ascii','ignore').decode('ascii')
documents.setdefault('documents').append({"language":"en","id":str(count),"text":text})
count+= 1
return documents
# Replace the accessKey string value with your valid access key.
accessKey = '0bf7acdf299a4d0e8fec402213b6847c'
url = 'westeurope.api.cognitive.microsoft.com'
path = '/text/analytics/v2.0/keyPhrases'
def TextAnalytics(documents):
headers = {'Ocp-Apim-Subscription-Key': accessKey}
conn = http.client.HTTPSConnection(url)
body = json.dumps (documents)
conn.request ("POST", path, body, headers)
response = conn.getresponse ()
return response.read ()
docs = parsePDF("*.pdf")
print(docs)
print()
print ('Please wait a moment for the results to appear.\n')
result = TextAnalytics (docs)
print (json.dumps(json.loads(result), indent=4))