Exemplo n.º 1
0
    def make(self, ontname, threshold, link):
        weighted_tokens = WeightAttacher.make(self.user_stories, self.weights)

        self.onto = Ontology(ontname, self.user_stories)
        self.prolog = Ontology(ontname, self.user_stories)

        pf = PatternFactory(self.onto, self.prolog, weighted_tokens)
        self.onto = pf.make_patterns(self.user_stories, threshold)
        self.prolog = pf.prolog

        if link:
            self.link_to_story(self.onto.classes, self.user_stories)

        per_role_out = []
        per_role_onto = self.get_per_role(self.user_stories, link)

        for p in per_role_onto:
            per_role_out.append([p[0].replace('/', '_'), str(p[1])])

        return (OntologyGenerator(self.onto), PrologGenerator(self.prolog),
                per_role_out)
Exemplo n.º 2
0
class Constructor:
    def __init__(self, nlp, user_stories, matrix):
        self.nlp = nlp
        self.user_stories = user_stories
        self.weights = matrix['sum'].reset_index().values.tolist()

    def make(self, ontname, threshold, link):
        weighted_tokens = WeightAttacher.make(self.user_stories, self.weights)

        self.onto = Ontology(ontname, self.user_stories)
        self.prolog = Ontology(ontname, self.user_stories)

        pf = PatternFactory(self.onto, self.prolog, weighted_tokens)
        self.onto = pf.make_patterns(self.user_stories, threshold)
        self.prolog = pf.prolog

        if link:
            self.link_to_story(self.onto.classes, self.user_stories)

        g = Generator(self.onto.classes, self.onto.relationships)
        g_prolog = Generator(self.prolog.classes, self.prolog.relationships, False)

        per_role_out = []
        per_role_onto = self.get_per_role(self.user_stories, link)

        for p in per_role_onto:
            per_role_out.append([p[0].replace('/', '_'), p[1].prt(self.onto)])

        return g.prt(self.onto), g_prolog.prt(self.prolog), self.onto, self.prolog, per_role_out

    def link_to_story(self, classes, stories):
        used_stories = []

        for cl in classes:
            for story in cl.stories:
                if story >= 0:
                    s = self.get_story(int(story), stories)
                    parts = self.get_parts(cl.name, s)

                    # for part in part_name:
                    #	n = s.txtnr() + part
                    #	self.onto.get_class_by_name(-1, n, s.txtnr())
                    #	self.onto.new_relationship(-1, cl.name, cl.name + 'OccursIn' + n, n)
                    self.onto.new_relationship(-1, cl.name, cl.name + 'OccursIn' + s.txtnr(), s.txtnr())

                    for part in parts:
                        self.prolog.new_relationship(-1, cl.name, part, s.txtnr())

                    used_stories.append(s.txtnr())

        for story in used_stories:
            self.onto.get_class_by_name(-1, story, 'UserStory')

    def get_per_role(self, stories, link):
        roles_link = []
        roles = []
        stories_per_role = []
        per_role_ontos = []

        # Get a list of roles and a list where the stories are linked to their roles
        for story in self.user_stories:
            roles_link.append([story.role.t, story.number])
            if str.lower(story.role.t) not in [str.lower(s) for s in roles]:
                roles.append(story.role.t)

        # Get a list of stories per role and get the generator object for these stories
        for role in roles:
            stories_per_role = []
            for link in roles_link:
                if str.lower(role) == str.lower(link[0]):
                    stories_per_role.append(link[1])

            per_role_ontos.append([role, self.get_generator(role, stories_per_role, link)])

        return per_role_ontos

    def get_generator(self, role, spr, link):
        role_classes = []
        role_relationships = []
        cl_names = []

        # Get classes
        for cl in self.onto.classes:
            for story in cl.stories:
                if story >= 0 and story in spr and cl.name not in cl_names:
                    role_classes.append(cl)
                    cl_names.append(cl.name)
                    if cl.parent != '':
                        for cp in self.onto.classes:
                            if cp.name == cl.parent:
                                role_classes.append(cp)

            # Get the general classes
            if cl.stories[0] == -1:
                if cl.name == 'FunctionalRole' or cl.name == 'Person':
                    role_classes.append(cl)

        story_classes = []

        # Get all relationships belonging to these classes
        for rel in self.onto.relationships:
            for story in rel.stories:
                if rel.domain in cl_names and rel.range in cl_names and story in spr:
                    role_relationships.append(rel)

            # If 'link' add these classes too
            if link:
                for story in spr:
                    if rel.domain in cl_names and rel.range == 'US' + str(story):
                        role_relationships.append(rel)
                        story_classes.append(rel.range)

        # Retrieve all classes for the relationships created in link
        if link:
            for cl in self.onto.classes:
                for c in story_classes:
                    if cl.name == c:
                        role_classes.append(cl)
                if cl.name == 'UserStory':
                    role_classes.append(cl)

        return Generator(role_classes, role_relationships)

    def get_story(self, nr, stories):
        for story in stories:
            if nr == story.number:
                return story
        return False

    def get_parts(self, class_name, story):
        case = class_name.split()

        means_compounds = []
        means_compounds.append(story.means.main_object.compound)
        ends_compounds = flatten(story.ends.compounds)

        if story.means.free_form:
            if len(story.means.compounds) > 0:
                means_compounds.extend(flatten(story.means.compounds))

        role = []
        means = []
        ends = []
        rme = []

        for token in story.data:
            if token in story.role.text:
                if len(case) != 1:
                    role.append(get_case(token))
                elif token not in story.role.functional_role.compound:
                    role.append(get_case(token))
            if token in story.means.text:
                if len(case) != 1:
                    means.append(get_case(token))
                elif token not in means_compounds:
                    means.append(get_case(token))