Esempio n. 1
0
def df_to_matrix(df):
    """ Converts a CCOBRA dataset into matrix form.

    """

    clean = df[["id", "task", "response"]]
    usr = list(clean["id"].unique())
    matrix = np.zeros((576, len(usr)))

    for _, row in clean.iterrows():
        usr_idx = usr.index(row["id"])
        syl_item = ccobra.Item(usr_idx, "syllogistic", row["task"],
                               "single-choice", "", 0)
        syllog = ccobra.syllogistic.Syllogism(syl_item)
        enc_resp = syllog.encode_response(row["response"].split(";"))

        syl_idx = ccobra.syllogistic.SYLLOGISMS.index(syllog.encoded_task)
        resp_idx = ccobra.syllogistic.RESPONSES.index(enc_resp)
        comb_idx = syl_idx * 9 + resp_idx

        if matrix[comb_idx, usr_idx] != 0:
            print("Tried to write twice to field")
            exit()
        matrix[comb_idx, usr_idx] = 1

    return matrix
Esempio n. 2
0
    def get_train_data_dict(self, ccobra_data):
        """ Extracts the training data dict mapping from subject identifiers to their corresponding
        list of tasks and responses.

        Parameters
        ----------
        ccobra_data : ccobra.CCobraData
            Data to convert to the training data representation.

        Returns
        -------
        dict
            Dictionary containing the training information (item, response, etc.).

        """

        train_data_dict = {}
        for id_info, subj_df in ccobra_data.get().groupby('id'):
            subj_data = []
            for _, row in subj_df.sort_values(['sequence']).iterrows():
                train_dict = {}

                # Add the Item
                train_dict['item'] = ccobra.Item(row['id'], row['domain'],
                                                 row['task'],
                                                 row['response_type'],
                                                 row['choices'],
                                                 row['sequence'])

                # Convert the response to its list representation
                if isinstance(row['response'], str):
                    if row['response_type'] == 'multiple-choice':
                        train_dict['response'] = [
                            y.split(';') for y in
                            [x.split('/') for x in row['response'].split('|')]
                        ]
                    else:
                        train_dict['response'] = [
                            x.split(';') for x in row['response'].split('/')
                        ]
                else:
                    train_dict['response'] = row['response']

                # Add the remaining elements
                for key, value in row.iteritems():
                    if key not in ccobra_data.required_fields:
                        train_dict[key] = value

                subj_data.append(train_dict)
            train_data_dict[id_info] = subj_data
        return train_data_dict
Esempio n. 3
0
def persubjectify(dataframe):
    """ Convert dataframe to iterable over subjects and their items nested (similar as taken by pre_train)

    :param dataframe: pandas dataframe. Required fields: "id", "domain", "task", "response_type", "choices"
    :return:
    """
    dataset = [[] for i in dataframe["id"].unique()]

    # handle the case where ids start at integers other than 0 (e.g. 1)
    dataframe["id_normalized"] = dataframe["id"] - dataframe["id"].min()

    for i, row in dataframe.iterrows():
        subject_id = row["id_normalized"]
        item = ccobra.Item(identifier=subject_id,
                           domain=row["domain"],
                           task=row["task"],
                           resp_type=row["response_type"],
                           choices=row["choices"])
        response = [row["response"].split(";")]
        dataset[subject_id].append({"item": item, "response": response})
    return dataset
Esempio n. 4
0
    def predict(self, item, **kwargs):
        """
        Generate random response prediction.
        """
        model = np.zeros((6, 3), dtype=bool)

        syl = ccobra.syllogistic_generalized.GeneralizedSyllogism(item)

        if syl.figure == 2:
            prs = item.task_str.split("/")
            new_task = prs[1] + "/" + prs[0]
            new_item = ccobra.Item(item.identifier, item.domain, new_task,
                                   item.response_type, item.choices_str,
                                   item.sequence_number)
            syl = ccobra.syllogistic_generalized.GeneralizedSyllogism(new_item)

        instances = bidict({syl.A: 0, syl.B: 1, syl.C: 2})

        print(instances)

        # Fill first premise
        q1, i0, i1 = syl.p1
        model = self.fill_first_premise(model, q1, instances[i0], instances[i1])

        # Fill model with second premise
        q2, i2, i3 = syl.p2
        model = self.fill_second_premise(model, q2, 1, 2, 0)

        print(syl.task)
        print(model.astype(int))

        for quantifier in self.axioms:
            print("Conclusion", instances[i0], instances[i1])
            if self.axioms[quantifier](model, 0, 2):
                print('Prediction', quantifier, syl.A, syl.C)
                return [[quantifier, syl.A, syl.C]]

        print('No conclusion possible')
        return [['NVC']]
Esempio n. 5
0
def syllogism_to_item(syllogism):
    """
    >>> syllogism_to_item("AA1").task
    [['All', 'x', 'y'], ['All', 'y', 'z']]
    >>> syllogism_to_item("EO4").task
    [['No', 'y', 'x'], ['Some not', 'y', 'z']]
    >>> syllogism_to_item("II2").task
    [['Some', 'y', 'x'], ['Some', 'z', 'y']]
    """

    q1 = {"A": "All", "I": "Some", "E": "No", "O": "Some not"}[syllogism[0]]
    q2 = {"A": "All", "I": "Some", "E": "No", "O": "Some not"}[syllogism[1]]
    to = {
        "1": ["x;y", "y;z"],
        "2": ["y;x", "z;y"],
        "3": ["x;y", "z;y"],
        "4": ["y;x", "y;z"]
    }[syllogism[2]]

    # task = e.g. "All;x;y/All;y;z",
    task = q1 + ";" + to[0] + "/" + q2 + ";" + to[1]
    return ccobra.Item(0, "syllogistic", task, "single-choice",
                       GENERIC_CHOICES)
Esempio n. 6
0
    "Some;y;x/Some not;z;y", "Some;x;y/Some not;z;y", "Some;y;x/Some not;y;z",
    "No;x;y/All;y;z", "No;y;x/All;z;y", "No;x;y/All;z;y", "No;y;x/All;y;z",
    "No;x;y/Some;y;z", "No;y;x/Some;z;y", "No;x;y/Some;z;y", "No;y;x/Some;y;z",
    "No;x;y/No;y;z", "No;y;x/No;z;y", "No;x;y/No;z;y", "No;y;x/No;y;z",
    "No;x;y/Some not;y;z", "No;y;x/Some not;z;y", "No;x;y/Some not;z;y",
    "No;y;x/Some not;y;z", "Some not;x;y/All;y;z", "Some not;y;x/All;z;y",
    "Some not;x;y/All;z;y", "Some not;y;x/All;y;z", "Some not;x;y/Some;y;z",
    "Some not;y;x/Some;z;y", "Some not;x;y/Some;z;y", "Some not;y;x/Some;y;z",
    "Some not;x;y/No;y;z", "Some not;y;x/No;z;y", "Some not;x;y/No;z;y",
    "Some not;y;x/No;y;z", "Some not;x;y/Some not;y;z",
    "Some not;y;x/Some not;z;y", "Some not;x;y/Some not;z;y",
    "Some not;y;x/Some not;y;z"
]
GENERIC_CHOICES = "All/x/z|All/z/x|Some/x/z|Some/z/x|Some not/x/z|Some not/z/x|No/x/z|No/z/x|NVC"
GENERIC_ITEMS = [
    ccobra.Item(0, "syllogistic", task, "single-choice", GENERIC_CHOICES)
    for task in GENERIC_TASKS
]

# ---- SYLLOGISTIC FUNCTIONS ---- #


def add_implicatures(conclusions, existential=True, gricean=True):
    """
    existential: A -> I, E -> O
    gricean:     I -> O, O -> I

    >>> add_implicatures(["Aac", "Aca"], gricean=False)
    ['Aac', 'Aca', 'Iac', 'Ica']
    >>> add_implicatures(["Aac", "Eac", "NVC"], gricean=False)
    ['Aac', 'Eac', 'NVC', 'Iac', 'Oac']
Esempio n. 7
0
        "Few;A;B/Few;B;C", "Few;A;B/Most;B;C", "Few;A;B/No;B;C",
        "Few;A;B/Some not;B;C", "Few;A;B/Some;B;C", "Most;A;B/All;B;C",
        "Most;A;B/Few;B;C", "Most;A;B/Most;B;C", "Most;A;B/No;B;C",
        "Most;A;B/Some not;B;C", "Most;A;B/Some;B;C", "No;A;B/Few;B;C",
        "No;A;B/Most;B;C", "Some not;A;B/Few;B;C", "Some not;A;B/Most;B;C",
        "Some;A;B/Few;B;C", "Some;A;B/Most;B;C"
        # "All;A;B/Few;B;C"
        # ,"All;A;B/Few;B;C"
        # "Some;fencers;engineers/Some;campers;fencers"
        # ,"Most not;waiters;poets/Some;cashiers;waiters"
    ]
    choices = [
        "All;A;C|Some;A;C|No;A;C|Some not;A;C|Most;A;C|Few;A;C"
        # "All;C;A|Some;C;A|No;C;A|Some not;C;A|Most;C;A|Few;C;A"
        # "All;engineers;campers|All;campers;engineers|Some;engineers;campers
        # |Some;campers;engineers|No;engineers;campers|No;campers;engineers|Some not;engineers;campers|Some not;campers;engineers|Most;engineers;campers|Most;campers;engineers|Most not;engineers;campers|Most not;campers;engineers|NVC"
        # ,"All;poets;cashiers|All;cashiers;poets|Some;poets;cashiers|Some
        # ;cashiers;poets|No;poets;cashiers|No;cashiers;poets|Some not;poets;cashiers|Some not;cashiers;poets|Most;poets;cashiers|Most;cashiers;poets|Most not;poets;cashiers|Most not;cashiers;poets|NVC"
    ]

    # single_choice = "All;A;C|Some;A;C|No;A;C|Some not;A;C|Most;A;C|Few;A;C"
    single_choice = "All;C;A|Some;C;A|No;C;A|Some not;C;A|Most;C;A|Few;C;A"

    idx = 0
    for task in tasks:
        test_item = ccobra.Item(identifier, domain, task, resp_type,
                                single_choice, sequence_number)
        result = pm.predict(test_item)
        print("--------------------------------------")
        idx += 1
Esempio n. 8
0
        return [['NVC']]

if __name__ == "__main__":
    # execute only if run as a script
    pm = PreferredModel()

    identifier = "TEST"
    sequence_number = 1
    domain = "syllogistic-generalized"
    resp_type = "single-choice"

    tasks = [
        "All;A;B/Few;B;C"
        ,"Some;fencers;engineers/Some;campers;fencers"
        ,"Most not;waiters;poets/Some;cashiers;waiters"
    ]
    choices = [
        "All;A;C|Some;A;C|No;A;C|Some not;A;C|Most;A;C|Few;A;C"
        ,"All;engineers;campers|All;campers;engineers|Some;engineers;campers|Some;campers;engineers|No;engineers;campers|No;campers;engineers|Some not;engineers;campers|Some not;campers;engineers|Most;engineers;campers|Most;campers;engineers|Most not;engineers;campers|Most not;campers;engineers|NVC"
        ,"All;poets;cashiers|All;cashiers;poets|Some;poets;cashiers|Some;cashiers;poets|No;poets;cashiers|No;cashiers;poets|Some not;poets;cashiers|Some not;cashiers;poets|Most;poets;cashiers|Most;cashiers;poets|Most not;poets;cashiers|Most not;cashiers;poets|NVC"
    ]

    idx = 0;
    for task in tasks:
        test_item = ccobra.Item(identifier, domain, task, resp_type,
                                choices[idx], sequence_number)
        result = pm.predict(test_item)
        print("--------------------------------------")
        idx += 1