<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_within_document(self, document, occurrences, replacement_pattern):
 """ Replaces given pattern occurrences in given document using given settings. :param document: Document. :type document: QTextDocument :param replacement_pattern: Replacement pattern. :type replacement_pattern: unicode :return: Replaced occurrences count. :rtype: int """


        cursor = QTextCursor(document)
        cursor.beginEditBlock()
        offset = count = 0
        for occurence in sorted(occurrences, key=lambda x: x.position):
            cursor.setPosition(offset + occurence.position, QTextCursor.MoveAnchor)
            cursor.setPosition(offset + occurence.position + occurence.length, QTextCursor.KeepAnchor)
            cursor.insertText(replacement_pattern)
            offset += len(replacement_pattern) - occurence.length
            count += 1
        cursor.endEditBlock()
        return count