Esempio n. 1
0
def create_graph_grampal(text, k=2):
	"""Create a graph with the keywords and their links using grampal as service.

	Args:
		text (:obj:`str`): The text of origin. \n
		k (:obj:`int`): The correlation value ,by default = 2.

	Returns:
		graph (`igraph`): The graph generated.

	"""
	if text is None:
		print("Error text: The text cannot be void")
		return None
	if k <= 0:
		print("Error k: The correlation value has to be > 0")
		return -1
	graph = igraph.Graph()
	ginstance = Grampal()
	counter = 0
	values = []
	values2 = []
	values3 = []
	uniq = OrderedDict()
	uniq2 = OrderedDict()
	uniq3 = OrderedDict()
	for sentence in text.sents:
		response = ginstance.analiza(sentence.text)
		if response.status_code != 200:
			continue
		lines = response.text.splitlines()
		for i in range(len(lines)):
			if lines[i] != "":
				if ginstance.info_syntactic(lines[i])in SYNTACTIC_GROUP:
					if ginstance.info_lemma(lines[i]) != "UNKN" and ginstance.info_lemma(lines[i]) not in EXCLUSIONS:
						values.append((ginstance.info_lemma(lines[i]), counter))
						values2.append((ginstance.info_lemma(lines[i]), ginstance.info_orig(lines[i])))
						values3.append((ginstance.info_lemma(lines[i]), ginstance.info_syntactic(lines[i])))
				counter += 1

	for node in values:
		uniq[node[0]] = node[1]
	for node in values2:
		uniq2[node[0]] = node[1]
	for node in values3:
		uniq3[node[0]] = node[1]
	for key, value in uniq.items():
		graph.add_vertices(1)
		graph.vs[graph.vcount()-1]["lema"] = key
		graph.vs[graph.vcount()-1]["pos"] = value
		graph.vs[graph.vcount()-1]["orig"] = uniq2.get(key)
		graph.vs[graph.vcount()-1]["occur"] = len(value)
		graph.vs[graph.vcount()-1]["fr"] = round(len(value)/len(uniq), 4)
		graph.vs[graph.vcount()-1]["syntactic"] = uniq3.get(key)[0]
	l_aux = list(uniq.keys())
	for counter in range(0, len(values)):
		for i in range(1, k+1):
			if counter +i < len(values):
				current = values[counter][0]
				jump = values[counter+i][0]
				if current != jump:
					try:
						if graph.get_eid(l_aux.index(current), l_aux.index(jump)):
							pass
					except:
						graph.add_edge(l_aux.index(current), l_aux.index(jump))
	return graph
Esempio n. 2
0
	def test_info_orig_none(self):
		ginstance = Grampal()
		self.assertEqual(ginstance.info_orig(None), None, "El texto a analizar no puede estar a None")
Esempio n. 3
0
	def test_info_orig_estandar(self):
		ginstance = Grampal()
		respuesta = ginstance.analiza("Soy")
		self.assertEqual(ginstance.info_orig(respuesta.text), "Soy", "El texto a analizar no puede estar a None")
Esempio n. 4
0
	def test_info_orig_empty(self):
		ginstance = Grampal()
		self.assertEqual(ginstance.info_orig(""), None, "El texto a analizar no puede estar vacío")