Ejemplo n.º 1
0
    def detect_object(self, tagged_text: TaggedText) -> db.onto.Object:
        """
        Detects an object mentioned in the input text, extracts its properties and saves it
        in the object placeholder.

        Parameters
        ----------
        tagged_text  an input text

        Returns
        -------
        an object placeholder to be grounded later
        """
        obj = None
        text = tagged_text.get_text()

        # try to detect one of the known objects in text
        for obj_str in self.class_map.keys():
            if tagged_text.contains_pos_token(obj_str, "NN"):
                obj = self.detect_explicit_object(tagged_text, obj_str)
                break

        # try to detect a coreference to an object
        if obj is None and tagged_text.contains_pos_token("it", "PRP"):
            obj = self.detect_coreferenced_object()

        self.logger.debug(f"Object detected for \"{text}\": {obj}")

        return obj
Ejemplo n.º 2
0
    def detect_object(self, tagged_text : TaggedText) -> db.onto.Object:
        """
        Detects an object mentioned in the input text, extracts its properties and saves it
        in the object placeholder.

        Parameters
        ----------
        tagged_text  an input text

        Returns
        -------
        an object placeholder to be grounded later
        """
        obj = None
        text = tagged_text.get_text()

        # try to detect one of the known objects in text

        for obj_str in self.class_map.keys():
            try:
                #TODO should be only NN, but we have a problem that kostka is detected as VB/VBD
                obj_str_lang = self.templ_det[self.lang][obj_str]
                if tagged_text.contains_pos_token(obj_str_lang, "NN") or tagged_text.contains_pos_token(obj_str_lang, "VBD") or tagged_text.contains_pos_token(obj_str_lang, "VB") or tagged_text.contains_pos_token(obj_str_lang, "NNS") :
                    obj = self.detect_explicit_object(tagged_text, obj_str)
                    break
            except:
                pass
        # try to detect a coreference to an object
        if obj is None and tagged_text.contains_pos_token("it", "PRP"):
            obj = self.detect_coreferenced_object()

        self.logger.debug(f"Object detected for \"{text}\": {obj}")
        self.ui.say(self.guidance_file[self.lang]["object_matched"]+" "+ text)
        return obj
Ejemplo n.º 3
0
    def detect_relative_location(
            self, tagged_text: TaggedText) -> db.onto.RelativeLocation:
        # for preventing cyclic imports in previous Python versions
        import nlp_crow.modules.ObjectDetector as ObjectDetector

        loc = None
        obj_rel_locs = ["center", "left", "right", "top", "bottom"]

        for rel_loc in obj_rel_locs:
            if tagged_text.contains_text(self.templ_det[self.lang][rel_loc]):
                # TODO temporary solution: detect the object which the location refers to in the part of the sentence *after* the location
                index = tagged_text.indices_of(rel_loc)[0]
                tagged_text_cut = tagged_text.cut(index + 1, None)

                # detect to which object the location refers
                od = ObjectDetector.ObjectDetector()
                relative_to = od.detect_object(tagged_text_cut)

                if not relative_to:
                    ad = AreaDetector()
                    relative_to = ad.detect_area(tagged_text_cut)

                if relative_to:
                    loc = db.onto.RelativeLocation()
                    loc.loc_type = rel_loc
                    loc.relative_to = relative_to

        return loc
Ejemplo n.º 4
0
    def detect_absolute_location(self, tagged_text: TaggedText) -> db.onto.Location:
        if tagged_text.contains_text("here"):
            return self.detect_current_finger_location()

        elif tagged_text.contains_text("down"):
            return self.detect_current_robot_handle_location()

        return self.detect_location_from_text(tagged_text)
Ejemplo n.º 5
0
    def match(self, tagged_text: TaggedText) -> None:
        od = ObjectDetector()
        ld = LocationDetector()

        self.location = ld.detect_location(tagged_text)
        # TODO: temporary solution: detect the "put subject" in 3 words after the "put" verb. Should be improved with better grammar parsing.
        put_index = tagged_text.indices_of("put")[0]
        tagged_text_cut = tagged_text.cut(put_index + 1, put_index + 4)

        self.object_to_put = od.detect_object(tagged_text_cut)
Ejemplo n.º 6
0
    def match(self, tagged_text: TaggedText, language='en') -> None:
        od = ObjectDetector(language=language)

        # TODO: temporary solution: detect the "put subject" in 3 words after the "put" verb. Should be improved with better grammar parsing.
        try:
            put_index = tagged_text.indices_of(
                self.templ_det[language]['tidy'])[0]
            tagged_text_cut = tagged_text.cut(put_index + 1, put_index + 4)

            self.object_to_put = od.detect_object(tagged_text_cut)
        except:
            self.logger.debug(f"Put index not detected.")
Ejemplo n.º 7
0
    def flatten(self) -> TaggedText:
        """
        Return the tagged text resulting from left-to-right pass through the tree.
        """
        tagged_text = TaggedText()

        for subnode in self.subnodes:
            if type(subnode) is TaggedToken:
                tagged_text.add_tagged_token(token=subnode.token, tag=subnode.tag)
            else:
                sub_tagged_text = subnode.flatten()
                tagged_text += sub_tagged_text

        return tagged_text
Ejemplo n.º 8
0
    def detect_location(self, tagged_text: TaggedText):
        """
        Tries to extract the information about the location in the text.

        Parameters
        ----------
        tagged_text  a text in which the location should be detected

        Returns
        -------
        the location object or None if no location is detected
        """
        tagged_text.lang = self.lang
        loc = self.detect_absolute_location(tagged_text)

        if not loc:
            self.logger.debug(
                f"Absolute Location not detected for \"{tagged_text.get_text()}\": {loc}. Trying relative location"
            )
            loc = self.detect_relative_location(tagged_text)

        if loc:
            self.logger.debug(
                f"Location detected for \"{tagged_text.get_text()}\": {loc}")
        else:
            self.logger.debug(
                f"Location not detected for \"{tagged_text.get_text()}\": {loc}"
            )

        return loc
Ejemplo n.º 9
0
    def match(self, tagged_text: TaggedText) -> None:
        match = tagged_text.match_regex(r"called (.*)")

        if match:
            self.task_name = match.group(1)
        else:
            uim = UserInputManager()
            self.task_name = uim.ask_for_name("tower")
Ejemplo n.º 10
0
    def match(self, tagged_text : TaggedText) -> None:
        # match text until "with" or "corners" is encountered
        match = tagged_text.match_regex(r"called (.*?)(?= with| corners)")

        if match:
            self.area_name = match.group(1)
        else:
            uim = UserInputManager()
            self.area_name = uim.ask_for_name("area")

        # match eight digits for area corners
        regex_pos_list = [("corners", "NN")] + ([(r".*", "CD")] * 8)
        matches = tagged_text.match_regex_pos_list(regex_pos_list)

        for i in range(1, len(matches), 2):
            coord_x = nlp.get_int(matches[i].string)
            coord_y = nlp.get_int(matches[i+1].string)
            corner = db.onto.Location(x=coord_x, y=coord_y, z=0)

            setattr(self, f"corner_{i // 2}", corner)
Ejemplo n.º 11
0
    def detect_geometry(self, tagged_text: TaggedText):
        go = None

        if tagged_text.contains_text(self.templ_det[self.lang]['point']):
            go = db.onto.GeometricObject("point")

        if go:
            self.logger.debug(
                f"Geometry detected for \"{tagged_text.get_text()}\": {go}")

        return go
Ejemplo n.º 12
0
    def detect_area(self,
                    tagged_text: TaggedText) -> db.onto.ObjectPlaceholder:
        area_list = self.db_api.get_class_objects(db.onto.Area)

        for area in area_list:
            if tagged_text.contains_text(area.name):
                self.logger.debug(
                    f"Area detected for \"{tagged_text.get_text()}\": {area}")

                return area

        return None
Ejemplo n.º 13
0
    def detect_color(self, text: TaggedText):
        """
        A simple method for detecting a color in text.
        Yes, there is some space for improvement.

        Parameters
        ----------
        text  an input text
        """
        for color in self.colors:
            if color in text.get_text():
                return color

        return None
Ejemplo n.º 14
0
    def detect_color(self, text: TaggedText):
        """
        A simple method for detecting a color in text.
        Yes, there is some space for improvement.

        Parameters
        ----------
        text  an input text
        """
        for color in self.colors:
            try:
                color_lang = self.templ_det[self.lang][color]
                if color_lang in text.get_text():
                    return color
            except:
                pass

        return None