Exemple #1
0
    def _filter_part(lattice, indexes, part):
        n = int(len(lattice) * part)
        key_ = lambda x: x[1]  #TODO: check if this works. before it was cmp
        sorted_indexes = sorted(list(indexes.items()), key_, reverse=True)
        filtered_concepts = sorted_indexes[:n]

        values = sorted_indexes
        eps = values[n - 2][1] - values[n - 1][1]

        other_concepts = sorted_indexes[n:]
        for concept in other_concepts:
            if abs(concept[1] - values[n][1]) < eps:
                filtered_concepts.append(concept)

        return fca.ConceptSystem([c[0] for c in filtered_concepts])
Exemple #2
0
 def SortColumn(self, col):
     if col > 1:
         meta_col = col - 2
         meta_key = self.concept_system[0].meta.keys()[meta_col]
         data = self.concept_system[:]
         if self._sorted_columns[meta_col] == "no" or \
            self._sorted_columns[meta_col] == "ascent":
             descent = lambda x, y: cmp(y.meta[meta_key], x.meta[meta_key])
             data.sort(descent)
             self._sorted_columns[meta_col] = "descent"
         elif self._sorted_columns[meta_col] == "descent":
             ascent = lambda x, y: cmp(x.meta[meta_key], y.meta[meta_key])
             data.sort(ascent)
             self._sorted_columns[meta_col] = "ascent"
         self.concept_system = fca.ConceptSystem(data)
Exemple #3
0
 def _filter_part(lattice, indexes, part):
     n = int(len(lattice) * part)
     cmp_ = lambda x,y: cmp(x[1], y[1])
     sorted_indexes = sorted(indexes.items(), cmp_, reverse=True)
     filtered_concepts = sorted_indexes[:n]
     
     values = sorted_indexes
     eps = values[n-2][1]-values[n-1][1]
     
     other_concepts = sorted_indexes[n:]
     for concept in other_concepts:
         if abs(concept[1] - values[n][1]) < eps:
             filtered_concepts.append(concept)
         
     return fca.ConceptSystem([c[0] for c in filtered_concepts])
Exemple #4
0
    def _filter_abs(lattice, indexes, n):
        key_ = lambda x: x[1]  #TODO: check if this works. before it was cmp
        sorted_indexes = sorted(list(indexes.items()), key_, reverse=True)
        filtered_concepts = sorted_indexes[:int(n)]

        return fca.ConceptSystem([c[0] for c in filtered_concepts])
Exemple #5
0
 def _filter_value(lattice, indexes, value):
     filtered_concepts = [
         item for item in list(indexes.items()) if item[1] >= value
     ]
     return fca.ConceptSystem([c[0] for c in filtered_concepts])
Exemple #6
0
 def _filter_abs(lattice, indexes, n):
     cmp_ = lambda x,y: cmp(x[1], y[1])
     sorted_indexes = sorted(indexes.items(), cmp_, reverse=True)
     filtered_concepts = sorted_indexes[:int(n)]
         
     return fca.ConceptSystem([c[0] for c in filtered_concepts])
Exemple #7
0
def read_xml(path):
    """Read concept system from valid xml file.
    
    Examples
    ========
    
    >>> cs = read_xml('tests/concepts.xml')
    >>> print cs
    (['obj1', 'obj2', 'obj3'], ['attr1'])
    (['obj2', 'obj3'], ['attr1', 'attr2'])
    (['obj1'], ['attr1', 'attr3'])
    ([], M)
    """
    global new_obj, new_attr, cs, buffer

    buffer = ""

    cs = fca.ConceptSystem()

    new_obj = None
    new_attr = None

    objects = []
    d_objects = {}

    attributes = []
    d_attributes = {}

    new_intent = []
    new_extent = []
    new_meta = {}

    def start_element(name, attrs):
        global new_obj, new_attr, buffer
        global new_extent, new_intent, new_meta
        if name == "object":
            if "id" in attrs.keys():
                buffer = ""
                new_obj = attrs["id"]
            elif "ref" in attrs.keys():
                new_extent.append(d_objects[attrs["ref"]])
        elif name == "attribute":
            if "id" in attrs.keys():
                buffer = ""
                new_attr = attrs["id"]
            elif "ref" in attrs.keys():
                new_intent.append(d_attributes[attrs["ref"]])
        elif name == "meta":
            for key in attrs.keys():
                new_meta[str(key).replace("_", " ")] = float(attrs[key])
        elif name == "concept":
            new_intent = []
            new_extent = []
            new_meta = {}

    def end_element(name):
        global cs, new_intent, new_extent, new_meta
        global new_obj, new_attr, buffer
        if name == "object":
            if new_obj:
                d_objects[new_obj] = buffer
                objects.append(buffer)
                new_obj = None
                buffer = ""
        elif name == "attribute":
            if new_attr:
                d_attributes[new_attr] = buffer
                attributes.append(buffer)
                new_attr = None
                buffer = ""
        elif name == "concept":
            new_concept = fca.Concept(new_extent, new_intent)
            new_concept.meta = new_meta
            cs.append(new_concept)

            new_extent = []
            new_intent = []
            new_meta = {}

    def char_data(data):
        global buffer
        if data[0] == "\n":
            return
        data = data.strip()
        buffer += data

    p = xml.parsers.expat.ParserCreate()

    p.StartElementHandler = start_element
    p.EndElementHandler = end_element
    p.CharacterDataHandler = char_data

    f = open(path)
    p.ParseFile(f)

    return cs
Exemple #8
0
def uread_xml(path):
    global new_obj, new_attr, cs, buffer

    buffer = ""

    cs = fca.ConceptSystem()

    new_obj = None
    new_attr = None

    objects = []
    d_objects = {}

    attributes = []
    d_attributes = {}

    new_intent = []
    new_extent = []
    new_meta = {}

    def start_element(name, attrs):
        global new_obj, new_attr, buffer
        global new_extent, new_intent, new_meta
        if name == "object":
            if "id" in attrs.keys():
                buffer = ""
                new_obj = attrs["id"]
            elif "ref" in attrs.keys():
                new_extent.append(d_objects[attrs["ref"]])
        elif name == "attribute":
            if "id" in attrs.keys():
                buffer = ""
                new_attr = attrs["id"]
            elif "ref" in attrs.keys():
                new_intent.append(d_attributes[attrs["ref"]])
        elif name == "meta":
            for key in attrs.keys():
                new_meta[str(key).replace("_", " ")] = float(attrs[key])
        elif name == "concept":
            new_intent = []
            new_extent = []
            new_meta = {}

    def end_element(name):
        global cs, new_intent, new_extent, new_meta
        global new_obj, new_attr, buffer
        if name == "object":
            if new_obj:
                d_objects[new_obj] = buffer
                objects.append(buffer)
                new_obj = None
                buffer = ""
        elif name == "attribute":
            if new_attr:
                d_attributes[new_attr] = buffer
                attributes.append(buffer)
                new_attr = None
                buffer = ""
        elif name == "concept":
            new_concept = fca.Concept(new_extent, new_intent)
            new_concept.meta = new_meta
            cs.append(new_concept)

            new_extent = []
            new_intent = []
            new_meta = {}

    def char_data(data):
        global buffer
        if data[0] == "\n":
            return
        data = data.strip()
        buffer += data

    p = xml.parsers.expat.ParserCreate(encoding="UTF-8")

    p.StartElementHandler = start_element
    p.EndElementHandler = end_element
    p.CharacterDataHandler = char_data

    f = open(path)
    p.ParseFile(f)

    return cs