Skip to content

Commit

Permalink
Merge pull request #488 from tkmtnt7000/fix-python3-ros-google-cloud
Browse files Browse the repository at this point in the history
[ros_google_cloud_language] Fix for python3; Avoid attribute error
  • Loading branch information
k-okada authored Jul 31, 2023
2 parents 77c1dfb + d1ca3e2 commit d8182a3
Show file tree
Hide file tree
Showing 3 changed files with 28 additions and 6 deletions.
33 changes: 27 additions & 6 deletions ros_google_cloud_language/node_scripts/analyze_text.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,11 @@
import actionlib
import rospy
import os
import sys
try:
from unidecode import unidecode
except ImportError:
pass

from ros_google_cloud_language.msg import AnalyzeTextAction
from ros_google_cloud_language.msg import AnalyzeTextResult
Expand Down Expand Up @@ -50,13 +55,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,
Expand All @@ -74,9 +87,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
Expand Down
1 change: 1 addition & 0 deletions ros_google_cloud_language/package.xml
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@

<exec_depend>message_runtime</exec_depend>
<exec_depend>rospy</exec_depend>
<exec_depend condition="$ROS_PYTHON_VERSION == 3">python3-unidecode</exec_depend>

<depend>actionlib_msgs</depend>
<depend>diagnostic_msgs</depend>
Expand Down
Empty file modified ros_google_cloud_language/test/test_rospy_node.py
100644 → 100755
Empty file.

0 comments on commit d8182a3

Please sign in to comment.