From 29fc6195c6cfed811db2bc7578623adbff893573 Mon Sep 17 00:00:00 2001 From: Naoto Tsukamoto Date: Mon, 24 Jul 2023 15:55:36 +0900 Subject: [PATCH 1/4] [ros_google_cloud_language] Fix for python3; Avoid attribute error --- .../node_scripts/analyze_text.py | 30 +++++++++++++++---- 1 file changed, 24 insertions(+), 6 deletions(-) diff --git a/ros_google_cloud_language/node_scripts/analyze_text.py b/ros_google_cloud_language/node_scripts/analyze_text.py index ace3e8c7d..bd1a452ca 100644 --- a/ros_google_cloud_language/node_scripts/analyze_text.py +++ b/ros_google_cloud_language/node_scripts/analyze_text.py @@ -8,6 +8,8 @@ import actionlib import rospy import os +import sys +from unidecode import unidecode from ros_google_cloud_language.msg import AnalyzeTextAction from ros_google_cloud_language.msg import AnalyzeTextResult @@ -50,13 +52,21 @@ def execute_cb(self, goal): encoding_type='UTF32', ) for entity in response.entities: + if sys.version_info.major < 3: + # To avoid AttributeError 'bytes' object has no attribute 'encode' in python3 #NOQA + entity.name = entity.name.encode('utf-8') + else: + # Forcefully changed to ascii string because ros message only supports ascii character #NOQA + entity.name = unidecode(entity.name) result.entities.append(TextEntity( - name=entity.name.encode('utf-8'), + name=entity.name, type=entity.type, - metadata=map(lambda kv: KeyValue(kv[0], kv[1]), - entity.metadata.items()), + # 'map' function is different from python2 and python3 + metadata=[ + KeyValue(kv[0], kv[1]) + for kv in entity.metadata.items() + ], salience=entity.salience)) - # Detects the sentiment of the text sentiment = self._client.analyze_sentiment( document=document, @@ -74,9 +84,17 @@ def execute_cb(self, goal): for token in syntax.tokens: # print("{} {} {}".format(token.part_of_speech.tag, token.text.content.encode('utf-8'), token.dependency_edge.head_token_index) + if sys.version_info.major < 3: + # To avoid AttributeError 'bytes' object has no attribute 'encode' in python3 #NOQA + token.text.content = token.text.content.encode('utf-8') + token.lemma = token.lemma.encode('utf-8') + else: + # Forcefully changed to ascii string because ros message only supports ascii character #NOQA + token.text.content = unidecode(token.text.content) + token.lemma = unidecode(token.lemma) result.syntaxes.append(TextSyntax( - name=token.text.content.encode('utf-8'), - lemma=token.lemma.encode('utf-8'), + name=token.text.content, + lemma=token.lemma, dependency_edge=token.dependency_edge.head_token_index, part_of_speech=token.part_of_speech.tag, parse_label=token.dependency_edge.label From eef8a9330b7d7c757484285bdc12b1360810d02c Mon Sep 17 00:00:00 2001 From: Naoto Tsukamoto Date: Mon, 24 Jul 2023 16:34:44 +0900 Subject: [PATCH 2/4] [ros_google_cloud_language] Add python3-unidecode to dependency --- ros_google_cloud_language/package.xml | 1 + 1 file changed, 1 insertion(+) diff --git a/ros_google_cloud_language/package.xml b/ros_google_cloud_language/package.xml index 706aff74b..b11fd196f 100644 --- a/ros_google_cloud_language/package.xml +++ b/ros_google_cloud_language/package.xml @@ -15,6 +15,7 @@ message_runtime rospy + python3-unidecode actionlib_msgs diagnostic_msgs From 27fd4193df9336288665638d6fc599a49250c8dc Mon Sep 17 00:00:00 2001 From: Naoto Tsukamoto Date: Mon, 24 Jul 2023 17:02:59 +0900 Subject: [PATCH 3/4] [ros_google_cloud_language] Change permission of test_rospy_node.py --- ros_google_cloud_language/test/test_rospy_node.py | 0 1 file changed, 0 insertions(+), 0 deletions(-) mode change 100644 => 100755 ros_google_cloud_language/test/test_rospy_node.py diff --git a/ros_google_cloud_language/test/test_rospy_node.py b/ros_google_cloud_language/test/test_rospy_node.py old mode 100644 new mode 100755 From cc249d29e611fc563119efbab6a1905e5a2307e8 Mon Sep 17 00:00:00 2001 From: Naoto Tsukamoto Date: Mon, 24 Jul 2023 17:40:01 +0900 Subject: [PATCH 4/4] [ros_google_cloud_language] Fix importing unidecode in python2 --- ros_google_cloud_language/node_scripts/analyze_text.py | 5 ++++- 1 file changed, 4 insertions(+), 1 deletion(-) diff --git a/ros_google_cloud_language/node_scripts/analyze_text.py b/ros_google_cloud_language/node_scripts/analyze_text.py index bd1a452ca..d8366a3dd 100644 --- a/ros_google_cloud_language/node_scripts/analyze_text.py +++ b/ros_google_cloud_language/node_scripts/analyze_text.py @@ -9,7 +9,10 @@ import rospy import os import sys -from unidecode import unidecode +try: + from unidecode import unidecode +except ImportError: + pass from ros_google_cloud_language.msg import AnalyzeTextAction from ros_google_cloud_language.msg import AnalyzeTextResult