コード例 #1
0
def set_new_undercut(premisegroup_uid, current_argument: Argument, db_user: User, issue: Issue) \
        -> Tuple[Argument, bool]:
    """
    Inserts a new undercut with the given parameters.

    :param premisegroup_uid: premisegroup_uid
    :param current_argument: Argument
    :param db_user: User
    :param issue: Issue.uid
    :return: Argument, Boolean if the argument is a duplicate
    """
    # duplicate?
    db_argument = DBDiscussionSession.query(Argument).filter(Argument.premisegroup_uid == premisegroup_uid,
                                                             Argument.is_supportive == False,
                                                             Argument.argument_uid == current_argument.uid).first()
    if db_argument:
        return db_argument, True
    else:
        new_argument = Argument(premisegroup=premisegroup_uid,
                                is_supportive=False,
                                author=db_user.uid,
                                issue=issue.uid)
        new_argument.set_conclusions_argument(current_argument.uid)
        DBDiscussionSession.add(new_argument)
        DBDiscussionSession.flush()
        transaction.commit()
        return new_argument, False
コード例 #2
0
ファイル: query.py プロジェクト: project-renard-survey/dbas
def __revoke_argument(db_argument: Argument, db_user: User):
    """
    Revokes the user as author of the argument

    :param db_user: User.uid
    :param db_argument: Argument.uid
    :return: Argument, Boolean, String
    """
    # does the argument has any attack or supports?
    relations = [
        get_undermines_for_argument_uid(db_argument.uid),
        get_supports_for_argument_uid(db_argument.uid),
        get_undercuts_for_argument_uid(db_argument.uid),
        get_rebuts_for_argument_uid(db_argument.uid)
    ]
    is_involved = sum([len(rel) if rel else 0 for rel in relations]) > 0

    if is_involved:
        LOG.debug("Author of argument %s changed from %s to anonymous",
                  db_argument.uid, db_user.uid)
        db_new_author = DBDiscussionSession.query(User).filter_by(
            nickname=nick_of_anonymous_user).first()
        db_argument.author_uid = db_new_author.uid
    else:
        LOG.debug("Disabling argument %s", db_argument.uid)
        db_argument.set_disabled(True)

    DBDiscussionSession.add(db_argument)
    DBDiscussionSession.flush()

    return db_argument
コード例 #3
0
    def test_get_text_for_conclusion(self):
        argument1 = Argument(premisegroup=4,
                             is_supportive=True,
                             author=1,
                             issue=1,
                             conclusion=3)
        # 'argument' is an argument
        self.assertEqual(
            lib.get_text_for_conclusion(argument=argument1,
                                        start_with_intro=False,
                                        rearrange_intro=False),
            'we should get a dog')

        argument2 = Argument(premisegroup=1,
                             is_supportive=False,
                             author=1,
                             issue=1)
        # 'argument' is a statement
        self.assertEqual(
            lib.get_text_for_conclusion(argument=argument2,
                                        start_with_intro=True,
                                        rearrange_intro=True), '')

        # unknown conclusion id
        argument3 = Argument(premisegroup=0,
                             is_supportive=True,
                             author=0,
                             issue=0,
                             conclusion=0)
        self.assertEqual(
            lib.get_text_for_conclusion(argument=argument3,
                                        start_with_intro=False,
                                        rearrange_intro=True), '')
コード例 #4
0
def __build_single_argument(db_argument: Argument, rearrange_intro: bool, with_html_tag: bool, colored_position: bool,
                            attack_type: str, _t: Translator, start_with_intro: bool, is_users_opinion: bool,
                            anonymous_style: bool, support_counter_argument: bool = False, author_uid=None):
    """
    Build up argument text for a single argument

    Please, do not touch this!

    :param rearrange_intro: Boolean
    :param with_html_tag: Boolean
    :param colored_position: Boolean
    :param attack_type: String
    :param _t: Translator
    :param start_with_intro: Boolean
    :param is_users_opinion: Boolean
    :param anonymous_style: Boolean
    :param support_counter_argument: Boolean
    :param author_uid: User.uid
    :return: String
    """
    premises_text = db_argument.get_premisegroup_text()
    conclusion_text = db_argument.get_conclusion_text()
    lang = db_argument.lang

    if lang != 'de':
        premises_text = start_with_small(premises_text)

    tag_dict = __get_tags_for_building_single_argument(with_html_tag, attack_type, colored_position, premises_text,
                                                       conclusion_text)
    premises_text = tag_dict['premise']
    conclusion_text = tag_dict['conclusion']
    sb = tag_dict['tag_begin']
    sb_none = tag_dict['tag_none']
    se = tag_dict['tag_end']

    marked_element = False
    if author_uid:
        db_marked = DBDiscussionSession.query(MarkedArgument).filter(MarkedArgument.argument_uid == db_argument.uid,
                                                                     MarkedArgument.author_uid == author_uid).first()
        marked_element = db_marked is not None

    you_have_the_opinion_that = _t.get(_.youHaveTheOpinionThat).format('').strip()

    if lang == 'de':
        ret_value = __build_single_argument_for_de(_t, sb, se, you_have_the_opinion_that, start_with_intro,
                                                   anonymous_style, rearrange_intro, db_argument, attack_type, sb_none,
                                                   marked_element, lang, premises_text, conclusion_text,
                                                   is_users_opinion,
                                                   support_counter_argument)
    else:
        ret_value = __build_single_argument_for_en(_t, sb, se, you_have_the_opinion_that, marked_element,
                                                   conclusion_text,
                                                   premises_text, db_argument)
    return ret_value.replace('  ', ' ')
コード例 #5
0
def __create_argument_by_uids(db_user: User, premisegroup_uid, conclusion_uid,
                              argument_uid, is_supportive,
                              db_issue: Issue) -> Optional[Argument]:
    """
    Connects the given id's to a new argument

    :param db_user: User.nickname
    :param premisegroup_uid: PremiseGroup.uid
    :param conclusion_uid: Statement.uid
    :param argument_uid: Argument.uid
    :param is_supportive: Boolean
    :param db_issue: Issue
    :return:
    """
    LOG.debug(
        "Entering __create_argument_by_uids with user: %s, premisegroup_uid: %s, conclusion_uid :%s, "
        "argument_uid: %s, is_supportive: %s, issue: %s", db_user.nickname,
        premisegroup_uid, conclusion_uid, argument_uid, is_supportive,
        db_issue.uid)

    new_argument = DBDiscussionSession.query(Argument).filter(
        Argument.premisegroup_uid == premisegroup_uid,
        Argument.is_supportive == is_supportive,
        Argument.conclusion_uid == conclusion_uid,
        Argument.issue_uid == db_issue.uid).first()
    if not new_argument:
        new_argument = Argument(premisegroup=premisegroup_uid,
                                is_supportive=is_supportive,
                                author=db_user.uid,
                                issue=db_issue.uid,
                                conclusion=conclusion_uid)
        new_argument.set_conclusions_argument(argument_uid)

        DBDiscussionSession.add(new_argument)
        DBDiscussionSession.flush()

        new_argument = DBDiscussionSession.query(Argument).filter(
            Argument.premisegroup_uid == premisegroup_uid,
            Argument.is_supportive == is_supportive,
            Argument.author_uid == db_user.uid,
            Argument.conclusion_uid == conclusion_uid,
            Argument.argument_uid == argument_uid,
            Argument.issue_uid == db_issue.uid).first()
    transaction.commit()
    if new_argument:
        LOG.debug("Argument was inserted")
        return new_argument
    else:
        LOG.debug("Argument was not inserted")
        return None
コード例 #6
0
def __set_rebut_or_support(
        premisegroup_uid: int, current_argument: Argument, db_user: User,
        db_issue: Issue,
        is_supportive: bool) -> Tuple[Union[Argument, bool], bool]:
    db_arguments = get_enabled_arguments_as_query()
    db_argument = db_arguments.filter(
        Argument.premisegroup_uid == premisegroup_uid,
        Argument.is_supportive == True,
        Argument.conclusion_uid == current_argument.conclusion_uid).first()
    if db_argument:
        return db_argument, True
    else:
        db_tmp = DBDiscussionSession.query(Premise).filter_by(
            premisegroup_uid=premisegroup_uid).all()
        if any([
                p.statement_uid == current_argument.conclusion_uid
                for p in db_tmp
        ]):
            return False, False
        new_argument = Argument(premisegroup=premisegroup_uid,
                                is_supportive=is_supportive,
                                author=db_user.uid,
                                issue=db_issue.uid,
                                conclusion=current_argument.conclusion_uid)
        DBDiscussionSession.add(new_argument)
        DBDiscussionSession.flush()
        transaction.commit()
        return new_argument, False
コード例 #7
0
def __create_argument_by_uids(db_user: User, premisegroup_uid, conclusion_uid, argument_uid, is_supportive,
                              db_issue: Issue) -> Union[Argument, None]:
    """
    Connects the given id's to a new argument

    :param db_user: User.nickname
    :param premisegroup_uid: PremiseGroup.uid
    :param conclusion_uid: Statement.uid
    :param argument_uid: Argument.uid
    :param is_supportive: Boolean
    :param db_issue: Issue
    :return:
    """
    logger('StatementsHelper', 'main with user: '******', premisegroup_uid: ' + str(premisegroup_uid) +
           ', conclusion_uid: ' + str(conclusion_uid) +
           ', argument_uid: ' + str(argument_uid) +
           ', is_supportive: ' + str(is_supportive) +
           ', issue: ' + str(db_issue.uid))

    new_argument = DBDiscussionSession.query(Argument).filter(Argument.premisegroup_uid == premisegroup_uid,
                                                              Argument.is_supportive == is_supportive,
                                                              Argument.conclusion_uid == conclusion_uid,
                                                              Argument.issue_uid == db_issue.uid).first()
    if not new_argument:
        new_argument = Argument(premisegroup=premisegroup_uid, is_supportive=is_supportive, author=db_user.uid,
                                issue=db_issue.uid, conclusion=conclusion_uid)
        new_argument.set_conclusions_argument(argument_uid)

        DBDiscussionSession.add(new_argument)
        DBDiscussionSession.flush()

        # TODO This should be redundant code! new_argument should be the new argument
        new_argument = DBDiscussionSession.query(Argument).filter(Argument.premisegroup_uid == premisegroup_uid,
                                                                  Argument.is_supportive == is_supportive,
                                                                  Argument.author_uid == db_user.uid,
                                                                  Argument.conclusion_uid == conclusion_uid,
                                                                  Argument.argument_uid == argument_uid,
                                                                  Argument.issue_uid == db_issue.uid).first()
    transaction.commit()
    if new_argument:
        logger('StatementsHelper', 'argument was inserted')
        return new_argument
    else:
        logger('StatementsHelper', 'argument was not inserted')
        return None
コード例 #8
0
    def __get_text_parts_of_argument(self, db_argument: Argument):
        """
        Get all parts of an argument as string

        :param db_argument: Argument.uid
        :return: list of strings
        """
        LOG.debug("Get all parts of an argument as string")
        ret_list = list()

        # get premise of current argument
        db_premises = DBDiscussionSession.query(Premise).filter_by(
            premisegroup_uid=db_argument.premisegroup_uid).all()
        premises_uids = [premise.uid for premise in db_premises]
        for uid in premises_uids:
            LOG.debug("Add premise of argument %s", db_argument.uid)
            statement = DBDiscussionSession.query(Statement).get(uid)
            ret_list.append(
                self.__get_part_dict('premise', statement.get_text(),
                                     db_argument.uid, uid))

        if db_argument.argument_uid is None:  # get conclusion of current argument
            conclusion = db_argument.get_conclusion_text()
            LOG.debug("Add statement of argument %s", db_argument.uid)
            ret_list.append(
                self.__get_part_dict('conclusion', conclusion, db_argument.uid,
                                     db_argument.conclusion_uid))
        else:  # or get the conclusions argument
            db_conclusions_argument = DBDiscussionSession.query(Argument).get(
                db_argument.argument_uid)

            while db_conclusions_argument.argument_uid is not None:  # get further conclusions arguments

                # get premise of conclusions arguments
                db_premises = DBDiscussionSession.query(Premise).filter_by(
                    premisegroup_uid=db_argument.premisegroup_uid).all()
                premises_uids = [premise.uid for premise in db_premises]
                for uid in premises_uids:
                    statement = DBDiscussionSession.query(Statement).get(uid)
                    LOG.debug("Add premise of argument %s",
                              db_conclusions_argument.uid)
                    ret_list.append(
                        self.__get_part_dict('premise', statement.get_text(),
                                             db_conclusions_argument.uid, uid))

                db_conclusions_argument = DBDiscussionSession.query(
                    Argument).get(db_conclusions_argument.argument_uid)

            # get the last conclusion of the chain
            conclusion = db_conclusions_argument.get_conclusion_text()
            LOG.debug("Add statement of argument %s",
                      db_conclusions_argument.uid)
            ret_list.append(
                self.__get_part_dict('conclusion', conclusion,
                                     db_conclusions_argument.uid,
                                     db_conclusions_argument.conclusion_uid))

        return ret_list[::-1]
コード例 #9
0
def set_new_undermine_or_support_for_pgroup(premisegroup_uid: int,
                                            current_argument: Argument,
                                            is_supportive: bool, db_user: User,
                                            db_issue: Issue):
    """
    Inserts a new undermine or support with the given parameters.

    :param premisegroup_uid: premisegroup_uid
    :param current_argument: Argument
    :param is_supportive: Boolean
    :param db_user: User
    :param issue: Issue.uid
    :return: Argument, Boolean if the argument is a duplicate
    """
    already_in = []

    # all premises out of current pgroup
    db_premises = DBDiscussionSession.query(Premise).filter_by(
        premisegroup_uid=current_argument.premisegroup_uid).all()
    for premise in db_premises:
        new_arguments = []
        db_arguments = get_enabled_arguments_as_query()
        db_argument = db_arguments.filter(
            Argument.premisegroup_uid == premisegroup_uid,
            Argument.is_supportive == True,
            Argument.conclusion_uid == premise.statement_uid).first()
        if db_argument:
            continue

        db_tmp = DBDiscussionSession.query(Premise).filter_by(
            premisegroup_uid=premisegroup_uid).all()
        if any([p.statement_uid == premise.statement_uid for p in db_tmp]):
            return False

        new_arguments.append(
            Argument(premisegroup=premisegroup_uid,
                     is_supportive=is_supportive,
                     author=db_user.uid,
                     issue=db_issue.uid,
                     conclusion=premise.statement_uid))

        if len(new_arguments) > 0:
            DBDiscussionSession.add_all(new_arguments)
            DBDiscussionSession.flush()
            transaction.commit()

            already_in += new_arguments

    rnd = random.randint(0, len(already_in) - 1)
    return already_in[rnd]
コード例 #10
0
def __build_val_for_jump(db_argument: Argument, tag_premise, tag_conclusion,
                         tag_end, _t) -> str:
    premises = db_argument.get_premisegroup_text()
    if premises[-1] != '.':
        premises += '.'
    conclusion = db_argument.get_conclusion_text()

    because = _t.get(_.because).lower()
    conclusion = tag_conclusion + conclusion + tag_end
    premises = tag_premise + premises + tag_end

    intro = (start_con + _t.get(_.isNotRight).lower() +
             end_tag) if not db_argument.is_supportive else ''
    ret_value = '{} {} {} {}'.format(conclusion, intro, because, premises)
    if _t.get_lang() == 'de':
        intro = _t.get(
            _.itIsTrueThatAnonymous) if db_argument.is_supportive else _t.get(
                _.itIsFalseThatAnonymous)
        intro = start_with_capital(intro)
        intro = (start_pro
                 if db_argument.is_supportive else start_con) + intro + end_tag
        ret_value = '{} {}, {} {}'.format(intro, conclusion, because, premises)

    return ret_value
コード例 #11
0
def get_user_with_same_opinion_for_premisegroups_of_arg(
        db_argument: Argument, db_premises: List[Premise], db_user: User,
        lang: str, main_page: str):
    """
    Returns nested dictionary with all kinds of information about the votes of the premisegroups.

    :param db_argument:
    :param db_premises:
    :param db_user: User
    :param lang: language
    :param main_page: url
    :return: {'users':[{nickname1.avatar_url, nickname1.vote_timestamp}*]}
    """
    _t = Translator(lang)
    all_users = []
    text = db_argument.get_premisegroup_text()

    premise_statement_uids = [p.statement_uid for p in db_premises]
    db_clicks = DBDiscussionSession.query(ClickedStatement).filter(
        ClickedStatement.statement_uid.in_(premise_statement_uids),
        ClickedStatement.is_up_vote == True, ClickedStatement.is_valid == True,
        ClickedStatement.author_uid != db_user.uid).all()
    db_seens = DBDiscussionSession.query(SeenStatement).filter(
        SeenStatement.statement_uid.in_(premise_statement_uids)).all()

    for click in db_clicks:
        click_user = DBDiscussionSession.query(User).get(click.author_uid)
        users_dict = create_users_dict(click_user, click.timestamp, main_page,
                                       lang)
        all_users.append(users_dict)

    return {
        'uid': str(db_argument.uid),
        'text': '... {} {}'.format(_t.get(_.because).lower(), text),
        'users': all_users,
        'message': __get_text_for_clickcount(len(db_clicks), db_user.uid, _t),
        'seen_by': len(db_seens)
    }
コード例 #12
0
    def __get_dict_for_argumentation(self, user_arg: Argument,
                                     confrontation_arg_uid: int, history: str,
                                     attack: Relations, nickname: str,
                                     is_supportive: bool) -> dict:
        """
        Returns dict() for the reaction step

        :param user_arg: Argument
        :param confrontation_arg_uid:  Argument.uid
        :param history: String
        :param attack: String
        :param nickname: User.nickname
        :param is_supportive: Boolean
        :return: dict()
        """
        premise = user_arg.get_premisegroup_text()
        conclusion = get_text_for_conclusion(user_arg)
        db_confrontation = DBDiscussionSession.query(Argument).get(
            confrontation_arg_uid)
        confr = db_confrontation.get_premisegroup_text()
        sys_conclusion = (db_confrontation.get_conclusion_text())
        if attack == Relations.UNDERMINE:
            if db_confrontation.conclusion_uid != 0:
                premise = db_confrontation.get_conclusion_text()
            else:
                premise = get_text_for_argument_uid(
                    db_confrontation.argument_uid,
                    True,
                    colored_position=True,
                    attack_type=attack)

        # did the user changed his opinion?
        history = history_handler.split(history)
        user_changed_opinion = len(history) > 1 and '/undercut/' in history[-2]

        # argumentation is a reply for an argument, if the arguments conclusion of the user is no position
        conclusion_uid = user_arg.conclusion_uid
        tmp_arg = user_arg
        while not conclusion_uid:
            tmp_arg = DBDiscussionSession.query(Argument).get(
                tmp_arg.argument_uid)
            conclusion_uid = tmp_arg.conclusion_uid

        db_statement = DBDiscussionSession.query(Statement).get(conclusion_uid)
        reply_for_argument = not (db_statement and db_statement.is_position)
        support_counter_argument = 'reaction' in self.history.split('-')[-1]

        current_argument = get_text_for_argument_uid(
            user_arg.uid,
            nickname=nickname,
            with_html_tag=True,
            colored_position=True,
            user_changed_opinion=user_changed_opinion,
            attack_type=attack,
            minimize_on_undercut=True,
            support_counter_argument=support_counter_argument)

        current_argument = start_with_capital(current_argument)
        if self.lang != 'de':
            premise = start_with_small(premise)

        # check for support and build text
        _tn = Translator(self.lang)
        user_text = (_tn.get(_.otherParticipantsConvincedYouThat) +
                     ': ') if user_changed_opinion else ''
        user_text += current_argument if current_argument != '' else premise

        sys_text, gender = get_text_for_confrontation(
            self.lang, nickname, premise, conclusion, sys_conclusion,
            is_supportive, attack, confr, reply_for_argument,
            not user_arg.is_supportive, user_arg, db_confrontation)
        gender_of_counter_arg = gender

        return {
            'user': user_text,
            'sys': sys_text,
            'gender': gender_of_counter_arg,
            'confrontation': db_confrontation
        }
コード例 #13
0
def __split_premisegroup(review):
    """
    Splits a premisegroup into the items, which are mapped with the given review

    :param review: ReviewSplit.uid
    :return: None
    """
    db_values = DBDiscussionSession.query(ReviewSplitValues).filter_by(
        review_uid=review.uid).all()
    db_old_premises = DBDiscussionSession.query(Premise).filter_by(
        premisegroup_uid=review.premisegroup_uid).all()
    db_issue = DBDiscussionSession.query(Issue).get(
        db_old_premises[0].issue_uid)
    db_old_statement_ids = [p.statement_uid for p in db_old_premises]
    db_first_old_statement = DBDiscussionSession.query(Statement).get(
        db_old_premises[0].uid)
    db_user = DBDiscussionSession.query(User).get(review.detector_uid)

    if db_values:
        logger('review.opinions',
               'split given premisegroup into the mapped, new statements')
        db_statements = []
        for value in db_values:
            new_statement, tmp = set_statement(
                value.content, db_user, db_first_old_statement.is_position,
                db_issue)
            db_statements.append(new_statement)
    else:
        logger('review.opinions', 'just split the premisegroup')
        db_statements = DBDiscussionSession.query(Statement).filter(
            Statement.uid.in_(db_old_statement_ids)).all()

    # new premisegroups, for each statement a new one
    new_premisegroup_ids = []
    new_premise_ids = []
    for statement in db_statements:
        db_new_premisegroup = PremiseGroup(author=db_user.uid)
        DBDiscussionSession.add(db_new_premisegroup)
        DBDiscussionSession.flush()
        new_premisegroup_ids.append(db_new_premisegroup.uid)

        db_new_premise = Premise(db_new_premisegroup.uid, statement.uid, False,
                                 db_user.uid, statement.issue_uid)
        DBDiscussionSession.add(db_new_premise)
        DBDiscussionSession.flush()
        new_premise_ids.append(db_new_premise.uid)

        # note new added pgroup
        DBDiscussionSession.add(
            PremiseGroupSplitted(review.uid, review.premisegroup_uid,
                                 db_new_premisegroup.uid))

    # swap the premisegroup occurence in every argument and add new arguments for the new premises
    db_arguments = DBDiscussionSession.query(Argument).filter_by(
        premisegroup_uid=review.premisegroup_uid).all()
    for argument in db_arguments:
        argument.set_premisegroup(new_premisegroup_ids[0])
        DBDiscussionSession.add(argument)

        for uid in new_premisegroup_ids[1:]:
            argument = Argument(uid, argument.is_supportive,
                                argument.author_uid, argument.issue_uid,
                                argument.conclusion_uid, argument.argument_uid,
                                argument.is_disabled)
            DBDiscussionSession.add(argument)
            DBDiscussionSession.flush()
            DBDiscussionSession.add(
                ArgumentsAddedByPremiseGroupSplit(review.uid, argument.uid))

    # swap the conclusion in every argument
    new_statements_uids = [s.uid for s in db_statements]
    for old_statement_uid in db_old_statement_ids:
        db_arguments = DBDiscussionSession.query(Argument).filter_by(
            conclusion_uid=old_statement_uid).all()
        for argument in db_arguments:
            argument.set_conclusion(new_statements_uids[0])
            DBDiscussionSession.add(argument)
            DBDiscussionSession.add(
                StatementReplacementsByPremiseGroupSplit(
                    review.uid, old_statement_uid, new_statements_uids[0]))
            DBDiscussionSession.flush()

            for statement_uid in new_statements_uids[1:]:
                db_argument = Argument(argument.premisegroup_uid,
                                       argument.is_supportive,
                                       argument.author_uid, argument.issue_uid,
                                       statement_uid, argument.argument_uid,
                                       argument.is_disabled)
                DBDiscussionSession.add(db_argument)
                DBDiscussionSession.add(
                    StatementReplacementsByPremiseGroupSplit(
                        review.uid, old_statement_uid, statement_uid))
                DBDiscussionSession.flush()

    # finish
    DBDiscussionSession.flush()
    transaction.commit()