Repository navigation
Expand file tree
/
Copy patharg_extract.py
More file actions
126 lines (93 loc) · 3.5 KB
/
Copy patharg_extract.py
File metadata and controls
126 lines (93 loc) · 3.5 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
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
124
125
126
##
# Argument Mining with Tone Analysis 6.0
# 'arg_extract'
# Gide Inc 2019
# SPACY_ARGUING_LEXICON
# From the works of:
# Swapna Somasundaran, Josef Ruppenhofer and Janyce Wiebe (2007) Detecting Arguing and Sentiment in Meetings,
# SIGdial Workshop on Discourse and Dialogue, Antwerp, Belgium, September 2007 (SIGdial Workshop 2007)
##
from __future__ import generator_stop
import spacy
from spacy_arguing_lexicon import ArguingLexiconParser
from ibm_watson import ToneAnalyzerV3
from ibm_watson import ApiException
import json
nlp = spacy.load('en')
nlp.add_pipe(ArguingLexiconParser(lang=nlp.lang))
# argument_analyzer:
# Purpose: Extract argument labels, and sentences containing the argument
# Input: [String] text
# Output: [List] arg_result_list
def argument_analyzer(text):
arg_result_list = []
doc_analysis = nlp(text)
for ext in doc_analysis._.arguments.get_argument_spans():
arg_result = 'Argument: ' + ext.text + ', Label: ' + ext.label_ + ', Sentence: ' + ext.sent.text.strip()
arg_result_list.append(arg_result)
return arg_result_list
# argument_sentence_analyzer (Dumb version):
# Purpose: Extract sentences containing the argument
# Input: [String] text
# Output: [String] arg_result
def argument_sentence_analyzer(text):
arg_result = ''
doc_analysis = nlp(text)
for ext in doc_analysis._.arguments.get_argument_spans():
arg_result = str(ext.sent.text.strip())
return arg_result
# tone_analyzer:
# Purpose: Extract emotional score from texts using IBM Tone Analysis
# Input: [String] text
# Output: [String] doc_result_list (originally a List)
def tone_analyzer(text):
doc_result_list = []
sent_result_list = ''
tone_analysis = {}
if text == '' or text == None or text == ' ':
return ''
try:
tone_init = ToneAnalyzerV3(
version = '2019-07-29',
iam_apikey= 'Gwmh6uTwxSrtgPvWgIiCY1nN5zCjRulxbnS1VU1l-Xti',
url = 'https://gateway-wdc.watsonplatform.net/tone-analyzer/api'
)
tone_analysis = tone_init.tone(
{'text': text},
content_type='application/json'
).get_result()
except ApiException as ex:
print("Method failed with status code " + str(ex.code) + ": " + ex.message)
json_result = json.dumps(tone_analysis, indent=2)
json_result = json.loads(json_result)
doc_tone_result = json_result.get('document_tone').get('tones') #['document_tone']
sent_tone_result = json_result.get('sentences_tone')
if sent_tone_result != None:
for info in sent_tone_result:
tones = info['tones']
text = info['text']
tone_extract_list = tone_extraction(tones=tones)
sent_result_list = str(text) + ': ' + str(tone_extract_list)
else:
pass
if doc_tone_result != []:
for info in doc_tone_result:
tone_name = info['tone_name']
score = info['score']
doc_result_list.append(str(tone_name) + ': ' + str(score))
else:
pass
# if trig == 'd':
return str(doc_result_list)
# tone_extraction:
# Purpose: get score of the tone and tone name from review
# Input: [List] tones (list of tones categories)
# Output: [List] tone_extract_list (tone name and score)
def tone_extraction(tones):
tone_extract_list = []
if tones != []:
for info in tones:
tone_name = info['tone_name']
score = info['score']
tone_extract_list.append([tone_name, score])
return tone_extract_list