-
Notifications
You must be signed in to change notification settings - Fork 0
/
nlp_rock.py
123 lines (100 loc) · 4.77 KB
/
nlp_rock.py
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
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
import six
from google.cloud import language
from google.cloud import translate
from google.cloud.language import enums
from google.cloud.language import types
from google.api_core import exceptions
def _translate(client, text, frm, to):
text = text.strip()
return client.translate(text, source_language=frm, target_language=to)['translatedText']
def translate_en_th(client, text):
return _translate(client, text, 'en', 'th')
def translate_th_en(client, text):
return _translate(client, text, 'th', 'en')
def get_entities(client, text):
if isinstance(text, six.binary_type):
text = text.decode('utf-8')
# Instantiates a plain text document.
document = types.Document(
content=text,
type=enums.Document.Type.PLAIN_TEXT)
# analyze entity and sentiment at the same time
entities = client.analyze_entity_sentiment(
document=document,
encoding_type='UTF32',
).entities
# return list of result
return [
{
'name': entity.name,
'type': enums.Entity.Type(entity.type).name,
'salience': entity.salience,
'mention_type': enums.EntityMention.Type(entity.mentions[0].type).name,
'magnitude': entity.sentiment.magnitude,
'score': entity.sentiment.score,
} for entity in entities
]
def get_entities_th(lang_client, tran_client, text_th):
text_en = translate_th_en(tran_client, text_th)
entities = get_entities(lang_client, text_en)
return [
{
'_name': entity['name'],
'name': translate_en_th(tran_client, entity['name']),
'type': entity['type'],
'salience': entity['salience'],
'mention_type': entity['mention_type'],
'magnitude': entity['magnitude'],
'score': entity['score'],
} for entity in entities
]
def classify(lang_client, text, verbose=False):
try:
document = language.types.Document(
content=text,
type=language.enums.Document.Type.PLAIN_TEXT)
response = lang_client.classify_text(document)
categories = response.categories
except exceptions.InvalidArgument:
# 400 Invalid text content: too few tokens (words) to process.
return {}
result = {}
for category in categories:
# Turn the categories into a dictionary of the form:
# {category.name: category.confidence}, so that they can
# be treated as a sparse vector.
result[category.name] = category.confidence
if verbose:
print(text)
for category in categories:
print(u'=' * 20)
print(u'{:<16}: {}'.format('category', category.name))
print(u'{:<16}: {}'.format('confidence', category.confidence))
return result
def classify_th(lang_client, tran_client, text_th, verbose=False):
text_en = translate_th_en(tran_client, text_th)
return classify(lang_client, text_en, verbose), text_en
if __name__== "__main__":
# TODO change your text here
text = """
จากกรณี ครูอ้อม นางสาวอ้อมอารีย์ แข็งฤทธิ์ อายุ 32 ปี ครูสอนนักเรียนชั้นอนุบาล 1 และอนุบาล 2 โรงเรียนวัดวงเดือน ต.สามง่ามท่าโบสถ์ อ.หันคา จ.ชัยนาท โพสต์ข้อความหลังถูกมือดีปาเลือดสดกระจายเต็มหน้าห้องเรียนที่สอนประจำชั้นกลัวว่าจะถูกปองร้าย จากที่ทนไม่ไหวเกี่ยวกับคุณภาพชีวิตของเด็กๆ ที่ได้กินอาหารกลางวัน มีสารอาหารไม่ครบ ออกมาเปิดเผยว่าเด็กกินมาม่า ไม่ได้กินผลไม้ หรือของมีประโยชน์เหมือนโรงเรียนอื่นๆ
"""
from time import time
from pprint import pprint as pp
# start time
start_time = time()
# prepare clients
lang_client = language.LanguageServiceClient()
tran_client = translate.Client()
# classify
classify_result, _ = classify_th(lang_client, tran_client, text)
# extract entities
result = get_entities_th(lang_client, tran_client, text)
# show result
print(text)
pp(classify_result)
print('')
for r in result:
print("{:<20} | {} <== {}".format(r['type'], r['name'], r['_name']))
# show usage time
print('\nTotal time : %.2f seconds' % (time() - start_time))