Skip to content

Commit

Permalink
Fixing issues
Browse files Browse the repository at this point in the history
  • Loading branch information
suddrey committed Dec 10, 2020
1 parent fe396c5 commit 8b789aa
Show file tree
Hide file tree
Showing 10 changed files with 298 additions and 64 deletions.
1 change: 1 addition & 0 deletions grammar/intransitive.xml
Original file line number Diff line number Diff line change
Expand Up @@ -1405,6 +1405,7 @@
<member stem="etch" />
<member stem="translate" />
<member stem="spat" />
<member stem="patrol" />
<member stem="sizzle" />
<member stem="perplex" />
<member stem="frost" />
Expand Down
17 changes: 17 additions & 0 deletions grammar/lexicon_core.xml
Original file line number Diff line number Diff line change
Expand Up @@ -490,4 +490,21 @@
</lf>
</complexcat>
</entry>
</family>

<family name="Stop" pos="Stop">
<entry name="term">
<atomcat type="term">
<fs id="2">
<feat attr="index">
<lf><nomvar name="X" /></lf>
</feat>
</fs>
<lf>
<satop nomvar="X:stop">
<prop name="[*DEFAULT*]" />
</satop>
</lf>
</atomcat>
</entry>
</family>
44 changes: 44 additions & 0 deletions grammar/lexicon_decorators.xml
Original file line number Diff line number Diff line change
Expand Up @@ -197,4 +197,48 @@
</lf>
</complexcat>
</entry>
</family>

<family class="repeat" pos="Loop" closed="true">
<entry name="s/(s\np)">
<complexcat>
<atomcat type="s">
<fs id="2">
<feat attr="index">
<lf><nomvar name="E" /></lf>
</feat>
</fs>
</atomcat>
<slash dir="/" mode="&gt;" />
<complexcat>
<atomcat type="s">
<fs id="3">
<feat attr="index">
<lf><nomvar name="X" /></lf>
</feat>
</fs>
</atomcat>
<slash dir="\" mode="&lt;" />
<atomcat type="np">
<fs id="5">
<feat attr="index">
<lf><nomvar name="Y" /></lf>
</feat>
</fs>
</atomcat>
</complexcat>
<lf>
<satop nomvar="E:loop">
<prop name="[*DEFAULT*]" />
<diamond mode="body">
<nomvar name="X" />
</diamond>
<diamond mode="infinite">
<nomvar name="E" />
</diamond>
</satop>
</lf>
</complexcat>
</entry>
<member stem="repeatedly" />
</family>
8 changes: 7 additions & 1 deletion grammar/morph.xml
Original file line number Diff line number Diff line change
Expand Up @@ -113,6 +113,7 @@ $Revision: 1.8 $, $Date: 2010/09/13 04:37:19 $
<entry pos="N" word="kitchen" class="room" macros="@sg @sg-X"/>
<entry pos="N" word="storage" class="room" macros="@sg @sg-X"/>
<entry pos="N" word="dining" class="room" macros="@sg @sg-X"/>
<entry pos="N" word="lounge" class="room" macros="@sg @sg-X"/>

<entry pos="N" word="room" class="room" macros="@sg @sg-X"/>
<entry pos="N" word="hall" stem="room" class="room" macros="@sg @sg-X"/>
Expand Down Expand Up @@ -464,7 +465,12 @@ $Revision: 1.8 $, $Date: 2010/09/13 04:37:19 $
<entry pos="Conditional" word="when" />
<entry pos="Conditional" word="once" stem="when" />
<entry pos="Conditional" word="till" stem="until" />


<entry pos="Loop" word="repeatedly" />
<entry pos="Loop" word="continuously" stem="repeatedly" />

<entry pos="Stop" word="stop" />

<!-- ======================= Prepositions ======================== -->

<entry pos="Prep" word="for" />
Expand Down
17 changes: 11 additions & 6 deletions launch/lingua.launch
Original file line number Diff line number Diff line change
@@ -1,10 +1,14 @@
<launch>
<arg name="gui" value="true" />
<arg name="sim" value="true" />

<arg name="method_path" default="$(find lingua)/data/methods" />

<arg name="grammar" value="$(find lingua)/grammar/grammar.xml" />
<arg name="handlers" value="$(find lingua)/data/world/handlers.json" />

<arg name="objects_file" default="" />
<arg name="types_file" default="" />

<include file="$(find lingua_world)/launch/world.launch" />

<include file="$(find lingua_kb)/launch/lingua_kb.launch">
Expand All @@ -19,12 +23,13 @@
<node pkg="rqt_py_trees" type="rqt_py_trees" name="rqt_py_trees" />
</group>

<group if="$(arg sim)">
<node pkg="lingua_tests" type="manipulator" name="manipulator" output="screen" />
<node pkg="lingua_tests" type="world_model" name="world_model" />
</group>

<node pkg="lingua" type="resolver" name="resolver" output="screen" />
<node pkg="lingua" type="speech" name="speech" />

<node pkg="lingua" type="lingua" name="lingua" output="screen">
<param name="method_path" value="$(arg method_path)" />
<param name="objects_file" value="$(arg objects_file)" if="$(eval len(str(arg('objects_file'))) > 0)" />
<param name="types_file" value="$(arg types_file)" if="$(eval len(str(arg('types_file'))) > 0)" />
</node>

</launch>
17 changes: 17 additions & 0 deletions scripts/lingua
Original file line number Diff line number Diff line change
Expand Up @@ -8,9 +8,26 @@ from lingua.trees import Lingua, OneShotSelector
from rv_trees.trees import BehaviourTree
from rv_trees.leaves_ros import Leaf

from lingua_kb.srv import Clear, Load

if __name__ == '__main__':
rospy.init_node('lingua')

rospy.ServiceProxy('/kb/clear', Clear).wait_for_service()

if rospy.has_param('~objects_file'):
rospy.loginfo('Loading objects from: {}'.format(rospy.get_param('~objects_file')))
rospy.ServiceProxy('/kb/clear', Clear)('objects')
rospy.ServiceProxy('/kb/load', Load)('objects', rospy.get_param('~objects_file'))

if rospy.has_param('~types_file'):
rospy.loginfo('Loading types from: {}'.format(rospy.get_param('~types_file')))
rospy.ServiceProxy('/kb/clear', Clear)('types')
rospy.ServiceProxy('/kb/load', Load)('types', rospy.get_param('~types_file'))

if rospy.has_param('~method_path'):
Method.set_path(rospy.get_param('~method_path'))

Method.load()

bt = BehaviourTree('Lingua', Lingua())
Expand Down
38 changes: 35 additions & 3 deletions src/lingua/ccg_reader/ccg_reader.py
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,9 @@ def read(node):
if ForLoopReader.is_for(node):
return ForLoopReader.read(node)

if InfiniteLoopReader.is_repeat(node):
return InfiniteLoopReader.read(node)

if ObjectReader.is_object(node):
return ObjectReader.read(node)

Expand Down Expand Up @@ -240,8 +243,8 @@ def get_limit(node):
def is_object(node):
try:
if node.tag == 'satop':
return node.get('nom').split(':')[1] in ['object', 'tool', 'dummy']
return node.find('nom').get('name').split(':')[1] in ['object', 'tool', 'dummy']
return node.get('nom').split(':')[1] in ['object', 'tool', 'dummy', 'room']
return node.find('nom').get('name').split(':')[1] in ['object', 'tool', 'dummy', 'room']
except Exception as e:
print(str(e))
return False
Expand Down Expand Up @@ -382,6 +385,24 @@ def is_for(node):

return None

class InfiniteLoopReader:
@staticmethod
def read(node):
return InfiniteLoop(
ConditionalReader.get_body(node)
)

@staticmethod
def is_repeat(node):
try:
if node.tag == 'satop':
return ':loop' in node.get('nom') and node.find('prop').get('name') == 'repeatedly'
return ':loop' in node.find('nom').get('name') and node.find('prop').get('name') == 'repeatedly'
except Exception as e:
print(str(e))

return None

class DurationReader:
@staticmethod
def read(node):
Expand Down Expand Up @@ -518,4 +539,15 @@ def read(node):

@staticmethod
def get_value(node):
return node.find('prop').get('name')
return node.find('prop').get('name')

class StopReader:
@staticmethod
def is_stop(node):
try:
if node.tag == 'satop':
return ':stop' in node.get('nom')
return ':stop' in node.find('nom').get('name')
except Exception as e:
print(str(e))
return False
22 changes: 14 additions & 8 deletions src/lingua/method.py
Original file line number Diff line number Diff line change
Expand Up @@ -151,7 +151,7 @@ def to_tree(self):
return subtree

children = []

for args in self.zip_arguments(self.arguments):
branch = self.generate_tree(args, setup=True)
children.append(branch)
Expand Down Expand Up @@ -255,6 +255,7 @@ def generate_tree(self, arguments, setup=False):
return root

def generate_branch(self, branch, arguments):
print(branch)
if branch['type'] == 'sequence':
children = list([self.generate_branch(child, arguments) for child in branch['children']])
return Sequence(branch['name'] if 'name' in branch else None if 'name' in branch else 'sequence', children=children)
Expand All @@ -267,14 +268,14 @@ def generate_branch(self, branch, arguments):
data = copy.deepcopy(branch)

if 'args' in data and 'load_value' in data['args']:
matches = re.findall('\${([^}]*)}', data['args']['load_value'])
matches = re.findall(r'\${([^}]*)}', data['args']['load_value'])

for match in matches:
data['args']['load_value'] = arguments[match]

module = importlib.import_module(data['package'])
class_type = module.__getattribute__(data['class_name'])
return class_type(name=data['name'] if 'name' in data else None, **data['args'] if 'args' in data else {})
return class_type(**data['args'] if 'args' in data else {})

if branch['type'] == 'decorator':
data = copy.deepcopy(branch)
Expand All @@ -286,10 +287,8 @@ def generate_branch(self, branch, arguments):
matches = re.findall('\${([^}]*)}', data['args']['predicate'])

for match in matches:
print(arguments[match])
data['args']['predicate'] = data['args']['predicate'].replace('${' + match + '}', str(arguments[match].to_query()))
print(data['args'])


try:
node_name = data['name'] if 'name' in data else data['args']['predicate']
except KeyError:
Expand All @@ -298,6 +297,13 @@ def generate_branch(self, branch, arguments):
return class_type(self.generate_branch(data['child'], arguments), name=node_name, **data['args'] if 'args' in data else {})

if branch['type'] == 'behaviour':

if 'args' in branch and 'mapping' in branch['args']:
for idx in branch['args']['mapping']:
if isinstance(branch['args']['mapping'][idx], dict):
arguments[idx] = Base.from_json(branch['args']['mapping'][idx], arguments)
branch['args']['mapping'][idx] = idx

return Subtree(name=branch['name'] if 'name' in branch else branch['method_name'], method_name=branch['method_name'], arguments=arguments, **branch['args'] if 'args' in branch else {})

def toJSON(self):
Expand All @@ -309,4 +315,4 @@ def toJSON(self):
}

from .trees import Subtree, Preconditions, LearnMethod
from .types import Conjunction, Groundable, Attribute
from .types import Base, Conjunction, Groundable, Attribute
19 changes: 12 additions & 7 deletions src/lingua/trees.py
Original file line number Diff line number Diff line change
Expand Up @@ -129,11 +129,16 @@ def get_last_input(self):

def speech_cb(self, msg):
# Get XML parses from CCG parser for input text
if msg.data in ['done', 'finished', 'that\'s it'] and self.get_listener():
if msg.data in ['try', 'done', 'finished', 'that\'s it'] and self.get_listener():
self.get_listener().set_input(Terminal())
self.pub_speech.publish(String(data='Thank you'))
return

if msg.data in ['stop']:
self.stop()
self.children.clear()
return

result = self.parser(msg.data
.replace(',', ' , ')
.replace(' ', ' ')
Expand Down Expand Up @@ -298,7 +303,7 @@ def add_child(leaf):
return self.steps

def expand(leaf):
subtree = Sequence(children=[step.to_btree() for step in self.steps])
subtree = Sequence(children=[step.to_btree(training=True) for step in self.steps])
subtree.setup(timeout=0)

self.executor.add_child(subtree)
Expand All @@ -317,16 +322,16 @@ def learn(leaf):
else:
root = self.expanded[0].to_json(self.parent.arguments)

method=Method(
name='gnash(tool arg0)',
learnt=Method(
name=method.get_name(),
preconditions=[],
postconditions=[],
root=root
)

Method.add(method)
Method.add(learnt)

return method
return learnt

self.executor = OneShotSelector(name='executor')

Expand All @@ -352,7 +357,7 @@ def learn(leaf):
)
]),
Sequence(name='Execute Instructions', children=[
Say(load_value='Let me try'),
Say(load_value='Let me try once'),
Leaf(
name='Expand',
result_fn=expand #type(leaf.loaded_data) != Terminal
Expand Down
Loading

0 comments on commit 8b789aa

Please sign in to comment.