# Path: semantics/parsing.py
# def process_parse_tree(parse_tree_input, text_input, knowledge_base=None, quiet=False):
#     """Produces semantic interpretations of parse trees."""
#     if not quiet:
#         print "Processing:", repr(text_input)
# 
#     # Perform tree operations
#     frames = extract_frames_from_parse(parse_tree_input)
#     if not quiet:
#         print "Semantic frames:", frames
# 
#     # Extract meaning
#     semantic_structures = create_semantic_structures(frames)
# 
#     # Update KB
#     if knowledge_base:
#         kb_response = knowledge_base.process_semantic_structures(semantic_structures, source='cmdr')
#     else:
#         kb_response = ''
# 
#     # Extract commands.
#     new_commands = [item for item in semantic_structures if isinstance(item, Command)]
#     if knowledge_base:
#         knowledge_base.fill_commands(new_commands)
#     if new_commands and not quiet:
#         print "New commands:"
#         for command in new_commands:
#             print command
# 
#     if kb_response and not quiet:
#         print "KB response:", kb_response
# 
#     return (frames, new_commands, kb_response)


import roslib
import rospy
import sys
import os
import json
from nlp.srv import String
from semantics.parsing import process_parse_tree
# the Free Software Foundation, either version 3 of the License, or
# (at your option) any later version.
# 
# SLURP is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with SLURP.  If not, see <http://www.gnu.org/licenses/>.

roslib.load_manifest('nlp')


# Get slurp into the sys.path
MODULE_DIR = os.path.dirname(os.path.abspath(__file__))
sys.path.append(os.path.join(MODULE_DIR, '..', '..', '..'))


NODE_NAME = "penn_nlp_semantics_server"
SERVICE_NAME = "penn_nlp_semantics_service"

# In-order names to give to return values of the response
SEMANTICS_KEYS = ('user_response', 'new_commands')


def process_text(request):
    """Return a parse response."""
    request_dict = json.loads(request.in_)
    rospy.loginfo("NLP semantics request: %r" % request_dict)

# Path: semantics/parsing.py
# def process_parse_tree(parse_tree_input, text_input, knowledge_base=None, quiet=False):
#     """Produces semantic interpretations of parse trees."""
#     if not quiet:
#         print "Processing:", repr(text_input)
# 
#     # Perform tree operations
#     frames = extract_frames_from_parse(parse_tree_input)
#     if not quiet:
#         print "Semantic frames:", frames
# 
#     # Extract meaning
#     semantic_structures = create_semantic_structures(frames)
# 
#     # Update KB
#     if knowledge_base:
#         kb_response = knowledge_base.process_semantic_structures(semantic_structures, source='cmdr')
#     else:
#         kb_response = ''
# 
#     # Extract commands.
#     new_commands = [item for item in semantic_structures if isinstance(item, Command)]
#     if knowledge_base:
#         knowledge_base.fill_commands(new_commands)
#     if new_commands and not quiet:
#         print "New commands:"
#         for command in new_commands:
#             print command
# 
#     if kb_response and not quiet:
#         print "KB response:", kb_response
# 
#     return (frames, new_commands, kb_response)


# Path: ros/nlp/src/semanticsservice.py
import roslib
import rospy
import sys
import os
import json
from nlp.srv import String
from semantics.parsing import process_parse_tree

# the Free Software Foundation, either version 3 of the License, or
# (at your option) any later version.
# 
# SLURP is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with SLURP.  If not, see <http://www.gnu.org/licenses/>.

roslib.load_manifest('nlp')


# Get slurp into the sys.path
MODULE_DIR = os.path.dirname(os.path.abspath(__file__))
sys.path.append(os.path.join(MODULE_DIR, '..', '..', '..'))


NODE_NAME = "penn_nlp_semantics_server"
SERVICE_NAME = "penn_nlp_semantics_service"

# In-order names to give to return values of the response
SEMANTICS_KEYS = ('user_response', 'new_commands')


def process_text(request):
    """Return a parse response."""
    request_dict = json.loads(request.in_)
    rospy.loginfo("NLP semantics request: %r" % request_dict)

    response = process_parse_tree(request_dict['tree'].encode('ascii', 'replace'), 