Exemplo n.º 1
0
    def get_factor_part(self, matrix, story, part):
        for token in eval('story.' + str(part) + '.text'):
            if NLPUtility.case(token) in matrix.index.values:
                matrix = self.add(matrix, NLPUtility.case(token),
                                  story.txtnr(), self.score(token, story))

        return matrix
Exemplo n.º 2
0
    def count_occurence(self, cm, sl, stories):
        for story in stories:
            for token in story.data:
                c = NLPUtility.case(token)
                if c in cm.index.values:
                    for s in sl:
                        if s[0] == c:
                            s[1].append(story.number)

                    if self.is_phrasal('role.functional_role', token,
                                       story) == 1:
                        cm = self.add(cm, c, 'Functional Role')
                    elif self.is_phrasal('role.functional_role', token,
                                         story) == 2:
                        cm = self.add(cm, c, 'Functional Role Compound')

                    if self.is_phrasal('means.main_object', token, story) == 1:
                        cm = self.add(cm, c, 'Main Object')
                    elif self.is_phrasal('means.main_object', token,
                                         story) == 2:
                        cm = self.add(cm, c, 'Main Object Compound')

                    if self.is_freeform('means', token, story) == 1:
                        cm = self.add(cm, c, 'Means Free Form Noun')

                    if story.ends.free_form:
                        if self.is_phrasal('ends.main_object', token,
                                           story) > 0 or self.is_freeform(
                                               'ends', token, story) == 1:
                            cm = self.add(cm, c, 'Ends Free Form Noun')

        return cm, sl
Exemplo n.º 3
0
	def get_namedict(self, tokens):
		namedict = {}

		for token in tokens:
			namedict[token.lemma] = NLPUtility.case(token)

		return namedict
Exemplo n.º 4
0
	def get_role_means_ends(self, matrix, stories):
		cases = matrix.index.values

		for case in cases:
			for story in stories:
				if story.role.indicator:
					if case in [NLPUtility.case(token) for token in story.role.text]:
						matrix.set_value(case, (story.txtnr(), 'Role'), 1)
				if story.means.indicator:
					if case in [NLPUtility.case(token) for token in story.means.text]:
						matrix.set_value(case, (story.txtnr(), 'Means'), 1)
				if story.ends.indicator:
					if case in [NLPUtility.case(token) for token in story.ends.text]:
						matrix.set_value(case, (story.txtnr(), 'Ends'), 1)
								
		return matrix
Exemplo n.º 5
0
	def count_occurence(self, cm, sl, stories):
		for story in stories:
			for token in story.data:
				c = NLPUtility.case(token)
				if c in cm.index.values:
					for s in sl:
						if s[0] == c:
							s[1].append(story.number)					

					if self.is_phrasal('role.functional_role', token, story) == 1:
						cm = self.add(cm, c, 'Functional Role')
					elif self.is_phrasal('role.functional_role', token, story) == 2:
						cm = self.add(cm, c, 'Functional Role Compound')

					if self.is_phrasal('means.main_object', token, story) == 1:
						cm = self.add(cm, c, 'Main Object')
					elif self.is_phrasal('means.main_object', token, story) == 2:
						cm = self.add(cm, c, 'Main Object Compound')

					if self.is_freeform('means', token, story) == 1:
						cm = self.add(cm, c, 'Means Free Form Noun')
					
					if story.ends.free_form:
						if self.is_phrasal('ends.main_object', token, story) > 0 or self.is_freeform('ends', token, story) == 1:
							cm = self.add(cm, c, 'Ends Free Form Noun')
					
		return cm, sl
Exemplo n.º 6
0
    def get_namedict(self, tokens):
        namedict = {}

        for token in tokens:
            namedict[token.lemma] = NLPUtility.case(token)

        return namedict
Exemplo n.º 7
0
	def make(stories, weights):
		weighted_tokens = []
		indices = [weight[0] for weight in weights]
		w = 0.0
		c = ""

		for story in stories:
			if story.has_ends:
				parts = ['role', 'means', 'ends']
			else:
				parts = ['role', 'means']

			for part in parts:
				for token in eval('story.' + str(part) + '.text'):
					c = NLPUtility.case(token)
					if c in indices:
						for weight in weights:
							if weight[0] == c:
								w = weight[1]
								break
					else:
						w = 0.0
					weighted_tokens.append(WeightedToken(token, w))

		return weighted_tokens
Exemplo n.º 8
0
    def make(stories, weights):
        weighted_tokens = []
        indices = [weight[0] for weight in weights]
        w = 0.0
        c = ""

        for story in stories:
            if story.has_ends:
                parts = ['role', 'means', 'ends']
            else:
                parts = ['role', 'means']

            for part in parts:
                for token in eval('story.' + str(part) + '.text'):
                    c = NLPUtility.case(token)
                    if c in indices:
                        for weight in weights:
                            if weight[0] == c:
                                w = weight[1]
                                break
                    else:
                        w = 0.0
                    weighted_tokens.append(WeightedToken(token, w))

        return weighted_tokens
 def find_story(self, w_token, stories):
     nrs = []
     for story in stories:
         if w_token.case in [
                 NLPUtility.case(t) for t in story.data if t.pos_ == 'NOUN'
         ]:
             nrs.append(story.number)
     return nrs
Exemplo n.º 10
0
    def remove_indicators(self, matrix, stories, nlp):
        indicators = []

        for story in stories:
            ind = story.role.indicator + " " + story.means.indicator
            if story.has_ends:
                ind += " " + story.ends.indicator

            [indicators.append(NLPUtility.case(t)) for t in nlp(ind)]
Exemplo n.º 11
0
    def make_patterns(self, user_stories, threshold):
        pi = PatternIdentifier(self.weighted_tokens)
        self.sysname = str.lower(NLPUtility.case(user_stories[0].system.main))

        for story in user_stories:
            pi.identify(story)

        relationships = self.apply_threshold(pi.relationships, threshold)

        self.create(relationships, user_stories, threshold, pi.roles)

        return self.onto
Exemplo n.º 12
0
    def get_role_means_ends(self, matrix, stories):
        cases = matrix.index.values

        for case in cases:
            for story in stories:
                if story.role.indicator:
                    if case in [
                            NLPUtility.case(token) for token in story.role.text
                    ]:
                        matrix.set_value(case, (story.txtnr(), 'Role'), 1)
                if story.means.indicator:
                    if case in [
                            NLPUtility.case(token)
                            for token in story.means.text
                    ]:
                        matrix.set_value(case, (story.txtnr(), 'Means'), 1)
                if story.ends.indicator:
                    if case in [
                            NLPUtility.case(token) for token in story.ends.text
                    ]:
                        matrix.set_value(case, (story.txtnr(), 'Ends'), 1)
Exemplo n.º 13
0
	def make_patterns(self, user_stories, threshold):
		pi = PatternIdentifier(self.weighted_tokens)
		self.sysname = str.lower(NLPUtility.case(user_stories[0].system.main))
		
		for story in user_stories:
			pi.identify(story)

		relationships = self.apply_threshold(pi.relationships, threshold)	

		self.create(relationships, user_stories, threshold, pi.roles)

		return self.onto
Exemplo n.º 14
0
    def remove_verbs(self, matrix, stories):
        verbs = []
        cases = matrix.index.values.tolist()

        for case in cases:
            pos = []

            for story in stories:
                for token in story.data:
                    if NLPUtility.case(token) == case:
                        pos.append(token.pos_)

            if len(set(pos)) == 1 and pos[0] == 'VERB':
                verbs.append(case)

        return matrix[(-matrix.index.isin(verbs))]
Exemplo n.º 15
0
	def remove_indicators(self, matrix, stories, nlp):
		indicators = []

		for story in stories:
			ind = story.role.indicator + " " + story.means.indicator
			if story.has_ends:
				ind += " " + story.ends.indicator

			[indicators.append(NLPUtility.case(t)) for t in nlp(ind)]

			[indicators.append(i) for i in story.indicators]

		for indicator in indicators:
			if matrix.loc[indicator, 'sum'] > 0:
				indicators.remove(indicator)

		return matrix[(-matrix.index.isin(indicators))]
Exemplo n.º 16
0
	def get_parts(self, class_name, story):
		case = class_name.split()

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

		if story.means.free_form:
			if len(story.means.compounds) > 0:
				if type(story.means.compounds[0]) is list:
					mc = [item for item in sublist for sublist in story.means.compounds]
				else:
					mc = story.means.compounds
				means_compounds.extend(mc)
			
		if len(ends_compounds) > 0:
			if type(ends_compounds[0]) is list:
				ends_compounds = [item for item in sublist for sublist in story.ends.compounds]

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

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

		if Utility.is_sublist(case, role):
			rme.append('Role')

		if Utility.is_sublist(case, means):
			rme.append('Means')

		if Utility.is_sublist(case, ends):
			rme.append('Ends')

		return rme
Exemplo n.º 17
0
    def remove_verbs(self, matrix, stories):
        verbs = []
        cases = matrix.index.values.tolist()

        for case in cases:
            pos = []

            for story in stories:
                for token in story.data:
                    if NLPUtility.case(token) == case:
                        pos.append(token)

            if len(set(pos)) == 1 and NLPUtility.is_verb(pos[0]):
                verbs.append(case)

        for verb in verbs:
            if matrix.loc[verb, 'sum'] > 0:
                verbs.remove(verb)

        return matrix[(-matrix.index.isin(verbs))]
Exemplo n.º 18
0
	def remove_verbs(self, matrix, stories):
		verbs = []
		cases = matrix.index.values.tolist()		

		for case in cases:
			pos = []

			for story in stories:
				for token in story.data:
					if NLPUtility.case(token) == case:
						pos.append(token)

			if len(set(pos)) == 1 and NLPUtility.is_verb(pos[0]):
				verbs.append(case)

		for verb in verbs:
			if matrix.loc[verb, 'sum'] > 0:
				verbs.remove(verb)

		return matrix[(-matrix.index.isin(verbs))]
Exemplo n.º 19
0
	def __init__(self, token, weight):
		self.token = token
		self.case = NLPUtility.case(token)
		self.weight = weight
Exemplo n.º 20
0
	def find_story(self, w_token, stories):
		nrs = []
		for story in stories:
			if w_token.case in [NLPUtility.case(t) for t in story.data]:
				nrs.append(story.number)
		return nrs
Exemplo n.º 21
0
	def get_factor_part(self, matrix, story, part):
		for token in eval('story.' + str(part) + '.text'):
			if NLPUtility.case(token) in matrix.index.values:
				matrix = self.add(matrix, NLPUtility.case(token), story.txtnr(), eval('self.score_' + str(part) + '(token, story)'))

		return matrix
Exemplo n.º 22
0
        for token in story.data:
            if token in story.role.text:
                if len(case) != 1:
                    role.append(NLPUtility.case(token))
                elif token not in story.role.functional_role.compound:
                    role.append(NLPUtility.case(token))
            if token in story.means.text:
                if len(case) != 1:
                    means.append(NLPUtility.case(token))
                elif token not in means_compounds:
                    means.append(NLPUtility.case(token))
            if story.has_ends:
                if token in story.ends.text:
                    if len(case) != 1:
                        ends.append(NLPUtility.case(token))
                    elif token not in ends_compounds:
                        ends.append(NLPUtility.case(token))

        if Utility.is_sublist(case, role):
            rme.append('Role')

        if Utility.is_sublist(case, means):
            rme.append('Means')

        if Utility.is_sublist(case, ends):
            rme.append('Ends')

        return rme

Exemplo n.º 23
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 = story.ends.compounds

        if story.means.free_form:
            if len(story.means.compounds) > 0:
                if type(story.means.compounds[0]) is list:
                    mc = [
                        item for item in sublist
                        for sublist in story.means.compounds
                    ]
                else:
                    mc = story.means.compounds
                means_compounds.extend(mc)

        if len(ends_compounds) > 0:
            if type(ends_compounds[0]) is list:
                ends_compounds = [
                    item for item in sublist
                    for sublist in story.ends.compounds
                ]

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

        for token in story.data:
            if token in story.role.text:
                if len(case) != 1:
                    role.append(NLPUtility.case(token))
                elif token not in story.role.functional_role.compound:
                    role.append(NLPUtility.case(token))
            if token in story.means.text:
                if len(case) != 1:
                    means.append(NLPUtility.case(token))
                elif token not in means_compounds:
                    means.append(NLPUtility.case(token))
Exemplo n.º 24
0
 def __init__(self, token, weight):
     self.token = token
     self.case = NLPUtility.case(token)
     self.weight = weight