-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathcloud.py
More file actions
executable file
·66 lines (56 loc) · 2.39 KB
/
cloud.py
File metadata and controls
executable file
·66 lines (56 loc) · 2.39 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
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
import os
os.environ["GOOGLE_APPLICATION_CREDENTIALS"] = "/home/gabe/depedantify_google_key.json"
# Import the Google Cloud client library
from google.cloud import language
from google.cloud.language import enums
from google.cloud.language import types
import pickle
'''
# Load the text string
with open('test.txt', 'r') as f:
text = f.read()
'''
class CloudConnect:
def __init__(self):
# Instantiate a client
self.client = language.LanguageServiceClient()
def get_entities(self, text):
# Craft a document object
document = types.Document(
content=text,
type=enums.Document.Type.PLAIN_TEXT
)
# Use the client to send it and receive a response
response = self.client.analyze_entities(
document=document,
encoding_type='UTF32'
)
return response
def process_entities_response(self, response):
'''Take a response and return the relevant entities we care about,
their offset, and their wikipedia links.'''
results = []
for entity in response.entities:
name = entity.name
Type = enums.Entity.Type(entity.type).name
metadata = entity.metadata
salience = entity.salience
metadata_entries = set(metadata.keys())
mentions = entity.mentions
if 'wikipedia_url' in metadata_entries:
url = metadata['wikipedia_url']
results.append((name, mentions, url, Type))
return results
def get_syntax(self, text):
# Craft a document object
document = types.Document(
content=text,
type=enums.Document.Type.PLAIN_TEXT
)
# Use the client to send it and receive a response
response = self.client.analyze_syntax(
document=document,
encoding_type='UTF32'
)
return response
cloud = CloudConnect()