<SYSTEM_TASK:>
Solve the following problem using Python, implementing the functions described below, one line at a time
<END_TASK>
<USER_TASK:>
Description:
 def replace(self, pattern, replacement_pattern, **kwargs):
 """ Replaces current given pattern occurence in the document with the replacement pattern. Usage:: True True regular_expressions=True, backward_search=True, wrap_around=True) True :param pattern: Pattern to replace. :type pattern: unicode :param replacement_pattern: Replacement pattern. :type replacement_pattern: unicode :param \*\*kwargs: Format settings. :type \*\*kwargs: dict :return: Method success. :rtype: bool """


        settings = foundations.data_structures.Structure(**{"case_sensitive": False,
                                                            "regular_expressions": False})
        settings.update(kwargs)

        selected_text = self.get_selected_text()
        regex = "^{0}$".format(
            pattern if settings.regular_expressions else re.escape(foundations.strings.to_string(pattern)))
        flags = int() if settings.case_sensitive else re.IGNORECASE
        if not selected_text or not re.search(regex, selected_text, flags=flags):
            self.search(pattern, **kwargs)
            return False

        cursor = self.textCursor()
        metrics = self.get_selected_text_metrics()
        if cursor.isNull():
            return False

        if not cursor.hasSelection():
            return False

        cursor.insertText(replacement_pattern)

        self.patterns_replaced.emit([metrics])

        self.search(pattern, **kwargs)

        return True