Skip to content

Commit

Permalink
Merge pull request #9 from MiyabiTane/add_syntax_google_nlp
Browse files Browse the repository at this point in the history
[ros_google_cloud_language] add syntax result
  • Loading branch information
k-okada committed Oct 31, 2022
2 parents 0f249a1 + 1b61e48 commit fd0fe8b
Show file tree
Hide file tree
Showing 7 changed files with 150 additions and 11 deletions.
3 changes: 3 additions & 0 deletions ros_google_cloud_language/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,9 @@ find_package(
add_message_files(
FILES
TextEntity.msg
TextSyntax.msg
PartOfSpeechTag.msg
DependencyEdge.msg
)

add_action_files(
Expand Down
1 change: 1 addition & 0 deletions ros_google_cloud_language/action/AnalyzeText.action
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ string text
---
# Define the result
ros_google_cloud_language/TextEntity[] entities
ros_google_cloud_language/TextSyntax[] syntaxes
float32 sentiment_score
float32 sentiment_magnitude
---
Expand Down
84 changes: 84 additions & 0 deletions ros_google_cloud_language/msg/DependencyEdge.msg
Original file line number Diff line number Diff line change
@@ -0,0 +1,84 @@
# https://googleapis.dev/python/language/latest/language_v1/types.html#google.cloud.language_v1.types.DependencyEdge
uint32 ABBREV = 1
uint32 ACOMP = 2
uint32 ADVCL = 3
uint32 ADVMOD = 4
uint32 ADVPHMOD = 64
uint32 AMOD = 5
uint32 APPOS = 6
uint32 ASP = 77
uint32 ATTR = 7
uint32 AUX = 8
uint32 AUXCAUS = 65
uint32 AUXPASS = 9
uint32 AUXVV = 66
uint32 CC = 10
uint32 CCOMP = 11
uint32 CONJ = 12
uint32 COP = 75
uint32 CSUBJ = 13
uint32 CSUBJPASS = 14
uint32 DEP = 15
uint32 DET = 16
uint32 DISCOURSE = 17
uint32 DISLOCATED = 76
uint32 DOBJ = 18
uint32 DTMOD = 67
uint32 EXPL = 19
uint32 FOREIGN = 68
uint32 GMOD = 78
uint32 GOBJ = 79
uint32 GOESWITH = 20
uint32 INFMOD = 80
uint32 IOBJ = 21
uint32 KW = 69
uint32 LIST = 70
uint32 MARK = 22
uint32 MES = 81
uint32 MWE = 23
uint32 MWV = 24
uint32 NCOMP = 82
uint32 NEG = 25
uint32 NN = 26
uint32 NOMC = 71
uint32 NOMCSUBJ = 72
uint32 NOMCSUBJPASS = 73
uint32 NPADVMOD = 27
uint32 NSUBJ = 28
uint32 NSUBJPASS = 29
uint32 NUM = 30
uint32 NUMBER = 31
uint32 NUMC = 74
uint32 P = 32
uint32 PARATAXIS = 33
uint32 PARTMOD = 34
uint32 PCOMP = 35
uint32 POBJ = 36
uint32 POSS = 37
uint32 POSTNEG = 38
uint32 PRECOMP = 39
uint32 PRECONJ = 40
uint32 PREDET = 41
uint32 PREF = 42
uint32 PREP = 43
uint32 PRONL = 44
uint32 PRT = 45
uint32 PS = 46
uint32 QUANTMOD = 47
uint32 RCMOD = 48
uint32 RCMODREL = 49
uint32 RDROP = 50
uint32 REF = 51
uint32 REMNANT = 52
uint32 REPARANDUM = 53
uint32 ROOT = 54
uint32 SNUM = 55
uint32 SUFF = 56
uint32 SUFFIX = 62
uint32 TITLE = 63
uint32 TMOD = 57
uint32 TOPIC = 58
uint32 UNKNOWN = 0
uint32 VMOD = 59
uint32 VOCATIVE = 60
uint32 XCOMP = 61
15 changes: 15 additions & 0 deletions ros_google_cloud_language/msg/PartOfSpeechTag.msg
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
# https://googleapis.dev/python/language/latest/language_v1/types.html#google.cloud.language_v1.types.PartOfSpeech.Tag
uint8 ADJ = 1
uint8 ADP = 2
uint8 ADV = 3
uint8 AFFIX = 13
uint8 CONJ = 4
uint8 DET = 5
uint8 NOUN = 6
uint8 NUM = 7
uint8 PRON = 8
uint8 PRT = 9
uint8 PUNCT = 10
uint8 UNKNOWN = 0
uint8 VERB = 11
uint8 X = 12
6 changes: 6 additions & 0 deletions ros_google_cloud_language/msg/TextSyntax.msg
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
# correspond to demo syntax result in https://cloud.google.com/natural-language
string name
string lemma
int32 dependency_edge
int32 part_of_speech
int32 parse_label
20 changes: 14 additions & 6 deletions ros_google_cloud_language/node_scripts/analyze_text.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@
from ros_google_cloud_language.msg import AnalyzeTextResult
from ros_google_cloud_language.msg import AnalyzeTextFeedback
from ros_google_cloud_language.msg import TextEntity
from ros_google_cloud_language.msg import TextSyntax
from diagnostic_msgs.msg import KeyValue

# Imports the Google Cloud client library
Expand Down Expand Up @@ -66,13 +67,20 @@ def execute_cb(self, goal):
result.sentiment_magnitude = sentiment.magnitude

# # Analayze Syntax
# syntax = self._client.analyze_syntax(
# document=document,
# encoding_type='UTF32'
# )
syntax = self._client.analyze_syntax(
document=document,
encoding_type='UTF32'
)

# for token in syntax.tokens:
# print("{} {}".format(token.part_of_speech.tag, token.text.content.encode('utf-8')))
for token in syntax.tokens:
# print("{} {} {}".format(token.part_of_speech.tag, token.text.content.encode('utf-8'), token.dependency_edge.head_token_index)
result.syntaxes.append(TextSyntax(
name=token.text.content.encode('utf-8'),
lemma=token.lemma.encode('utf-8'),
dependency_edge=token.dependency_edge.head_token_index,
part_of_speech=token.part_of_speech.tag,
parse_label=token.dependency_edge.label
))

except Exception as e:
rospy.logerr("Fail to analyze syntax ... {}".format(str(e)))
Expand Down
32 changes: 27 additions & 5 deletions ros_google_cloud_language/samples/client.l
Original file line number Diff line number Diff line change
Expand Up @@ -3,10 +3,22 @@
(ros::roseus-add-msgs "ros_google_cloud_language")
(ros::roseus "cloud_language_client")

(defun type-to-string (id)
(defun type-to-string-entity (id)
(do-symbols (type "ROS_GOOGLE_CLOUD_LANGUAGE::TEXTENTITY")
(if (= (eval type) id)
(return-from type-to-string (symbol-name type))))
(return-from type-to-string-entity (symbol-name type))))
nil)

(defun type-to-string-part-of-speech (id)
(do-symbols (type "ROS_GOOGLE_CLOUD_LANGUAGE::PARTOFSPEECHTAG")
(if (= (eval type) id)
(return-from type-to-string-part-of-speech (symbol-name type))))
nil)

(defun type-to-string-dependency-edge (id)
(do-symbols (type "ROS_GOOGLE_CLOUD_LANGUAGE::DEPENDENCYEDGE")
(if (= (eval type) id)
(return-from type-to-string-dependency-edge (symbol-name type))))
nil)

;; check if this retuns correct answer with https://cloud.google.com/natural-language
Expand All @@ -28,10 +40,20 @@
(ros::ros-info " magnitude : ~A" (send ret :sentiment_magnitude))
(ros::ros-info "entiteis")
(dolist (entity (send ret :entities))
(ros::ros-info " name : ~A" (send entity :name))
(ros::ros-info " type : ~A" (type-to-string (send entity :type)))
(ros::ros-info " salience : ~A" (send entity :salience))
(ros::ros-info " - name : ~A" (send entity :name))
(ros::ros-info " type : ~A" (type-to-string-entity (send entity :type)))
(ros::ros-info " salience : ~A" (send entity :salience))
)
(ros::ros-info "syntaxes")
(dolist (syntax (send ret :syntaxes))
(ros::ros-info " - name : ~A" (send syntax :name))
(ros::ros-info " lemma : ~A" (send syntax :lemma))
(ros::ros-info " dependency_edge : ~A" (send syntax :dependency_edge))
(ros::ros-info " part_of_speech : ~A"
(type-to-string-part-of-speech (send syntax :part_of_speech)))
(ros::ros-info " parse_label : ~A"
(type-to-string-dependency-edge (send syntax :parse_label)))
)
)))

(ros::roseus "client")
Expand Down

0 comments on commit fd0fe8b

Please sign in to comment.