Spaces:
Runtime error
Runtime error
cryptocalypse
commited on
Commit
•
6fb487c
1
Parent(s):
74c9c9a
memory draf lib, grapher draft lib
Browse files- lib/grepher.py +115 -0
- lib/memory.py +62 -0
lib/grepher.py
ADDED
@@ -0,0 +1,115 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
import requests
|
2 |
+
import json
|
3 |
+
import networkx as nx
|
4 |
+
import matplotlib.pyplot as plt
|
5 |
+
from fuzzywuzzy import fuzz
|
6 |
+
from fuzzywuzzy import process
|
7 |
+
from lib.memory import *
|
8 |
+
|
9 |
+
|
10 |
+
class APIRequester:
|
11 |
+
def __init__(self):
|
12 |
+
pass
|
13 |
+
|
14 |
+
def make_request(self, url):
|
15 |
+
response = requests.get(url)
|
16 |
+
if response.status_code == 200:
|
17 |
+
return response.json()
|
18 |
+
else:
|
19 |
+
return None
|
20 |
+
|
21 |
+
class JSONParser:
|
22 |
+
def __init__(self, memoria_nlp, threshold=70):
|
23 |
+
self.threshold = threshold
|
24 |
+
self.graph = nx.Graph()
|
25 |
+
self.memoria_nlp = memoria_nlp
|
26 |
+
|
27 |
+
def parse_json(self, data, parent=None):
|
28 |
+
if isinstance(data, dict):
|
29 |
+
for key, value in data.items():
|
30 |
+
if parent:
|
31 |
+
self.graph.add_node(parent)
|
32 |
+
self.graph.add_node(key)
|
33 |
+
self.graph.add_edge(parent, key)
|
34 |
+
|
35 |
+
for node in self.graph.nodes():
|
36 |
+
if node != parent and fuzz.ratio(node, key) >= self.threshold:
|
37 |
+
self.graph = nx.contracted_nodes(self.graph, node, key, self_loops=False)
|
38 |
+
|
39 |
+
self.memoria_nlp.agregar_concepto("keys", [(key, 1.0)])
|
40 |
+
|
41 |
+
if isinstance(value, (dict, list)):
|
42 |
+
self.parse_json(value, key)
|
43 |
+
else:
|
44 |
+
self.memoria_nlp.agregar_concepto("values", [(str(value), 1.0)])
|
45 |
+
if parent:
|
46 |
+
self.graph.add_node(value)
|
47 |
+
self.graph.add_edge(key, value)
|
48 |
+
|
49 |
+
for node in self.graph.nodes():
|
50 |
+
if node != value and fuzz.ratio(node, value) >= self.threshold:
|
51 |
+
self.graph = nx.contracted_nodes(self.graph, node, value, self_loops=False)
|
52 |
+
elif isinstance(data, list):
|
53 |
+
for item in data:
|
54 |
+
self.parse_json(item, parent)
|
55 |
+
|
56 |
+
def draw_graph(self):
|
57 |
+
pos = nx.spring_layout(self.graph, seed=42)
|
58 |
+
nx.draw(self.graph, pos, with_labels=True, node_size=700, node_color='skyblue', font_size=10, font_weight='bold')
|
59 |
+
plt.title("JSON Graph")
|
60 |
+
plt.show()
|
61 |
+
|
62 |
+
def guardar_en_memoria(self):
|
63 |
+
keys = self.memoria_nlp.obtener_conceptos_acotados(100)
|
64 |
+
with open("memoria.json", "w") as file:
|
65 |
+
json.dump(keys, file)
|
66 |
+
|
67 |
+
def buscar_nodo(self, nodo):
|
68 |
+
return process.extractOne(nodo, self.graph.nodes())[0]
|
69 |
+
|
70 |
+
def eliminar_nodo(self, nodo):
|
71 |
+
self.graph.remove_node(nodo)
|
72 |
+
|
73 |
+
def agregar_nodo(self, nodo):
|
74 |
+
self.graph.add_node(nodo)
|
75 |
+
|
76 |
+
def distancia_entre_nodos(self, nodo1, nodo2):
|
77 |
+
return nx.shortest_path_length(self.graph, source=nodo1, target=nodo2)
|
78 |
+
|
79 |
+
def ruta_entre_nodos(self, nodo1, nodo2):
|
80 |
+
return nx.shortest_path(self.graph, source=nodo1, target=nodo2)
|
81 |
+
|
82 |
+
def unir_grafos(self, otro_grafo, umbral):
|
83 |
+
for nodo in otro_grafo.nodes():
|
84 |
+
nodo_similar = process.extractOne(nodo, self.graph.nodes())[0]
|
85 |
+
if fuzz.ratio(nodo, nodo_similar) >= umbral:
|
86 |
+
self.graph = nx.contracted_nodes(self.graph, nodo_similar, nodo, self_loops=False)
|
87 |
+
else:
|
88 |
+
self.graph.add_node(nodo)
|
89 |
+
for vecino in otro_grafo.neighbors(nodo):
|
90 |
+
self.graph.add_edge(nodo, vecino)
|
91 |
+
|
92 |
+
# Ejemplo de uso
|
93 |
+
memoria_nlp = MemoriaRobotNLP(max_size=100)
|
94 |
+
json_parser = JSONParser(memoria_nlp)
|
95 |
+
|
96 |
+
api_requester = APIRequester()
|
97 |
+
url = "https://jsonplaceholder.typicode.com/posts"
|
98 |
+
data = api_requester.make_request(url)
|
99 |
+
|
100 |
+
if data:
|
101 |
+
json_parser.parse_json(data)
|
102 |
+
json_parser.draw_graph()
|
103 |
+
|
104 |
+
otro_parser = JSONParser(MemoriaRobotNLP(max_size=100))
|
105 |
+
otro_parser.parse_json({"id": 101, "title": "New Title", "userId": 11})
|
106 |
+
|
107 |
+
print("Uniendo los grafos...")
|
108 |
+
json_parser.unir_grafos(otro_parser.graph, umbral=80)
|
109 |
+
print("Grafo unido:")
|
110 |
+
json_parser.draw_graph()
|
111 |
+
|
112 |
+
json_parser.guardar_en_memoria()
|
113 |
+
else:
|
114 |
+
print("Error al realizar la solicitud a la API.")
|
115 |
+
|
lib/memory.py
ADDED
@@ -0,0 +1,62 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
class MemoriaRobotNLP:
|
2 |
+
def __init__(self, max_size):
|
3 |
+
self.max_size = max_size
|
4 |
+
self.memoria = {}
|
5 |
+
|
6 |
+
def agregar_concepto(self, concepto, strings):
|
7 |
+
if concepto not in self.memoria:
|
8 |
+
self.memoria[concepto] = []
|
9 |
+
|
10 |
+
for string, prioridad in strings:
|
11 |
+
self.memoria[concepto].append((string, prioridad))
|
12 |
+
|
13 |
+
def eliminar_concepto(self, concepto):
|
14 |
+
if concepto in self.memoria:
|
15 |
+
del self.memoria[concepto]
|
16 |
+
|
17 |
+
def agregar_string(self, concepto, string, prioridad):
|
18 |
+
if concepto not in self.memoria:
|
19 |
+
self.memoria[concepto] = []
|
20 |
+
|
21 |
+
self.memoria[concepto].append((string, prioridad))
|
22 |
+
|
23 |
+
def eliminar_string(self, concepto, string):
|
24 |
+
if concepto in self.memoria:
|
25 |
+
self.memoria[concepto] = [(s, p) for s, p in self.memoria[concepto] if s != string]
|
26 |
+
|
27 |
+
def obtener_conceptos_acotados(self, espacio_disponible):
|
28 |
+
memoria_ordenada = sorted(self.memoria.items(), key=lambda x: sum(prioridad for _, prioridad in x[1]), reverse=True)
|
29 |
+
espacio_utilizado = 0
|
30 |
+
conceptos_acotados = []
|
31 |
+
|
32 |
+
for concepto, strings in memoria_ordenada:
|
33 |
+
espacio_concepto = sum(prioridad for _, prioridad in strings)
|
34 |
+
if espacio_utilizado + espacio_concepto <= espacio_disponible:
|
35 |
+
conceptos_acotados.append((concepto, strings))
|
36 |
+
espacio_utilizado += espacio_concepto
|
37 |
+
else:
|
38 |
+
break
|
39 |
+
|
40 |
+
return conceptos_acotados
|
41 |
+
|
42 |
+
|
43 |
+
# Ejemplo de uso
|
44 |
+
memoria_robot = MemoriaRobotNLP(max_size=100)
|
45 |
+
|
46 |
+
memoria_robot.agregar_concepto("animales", [("perro", 0.8), ("gato", 0.7), ("pájaro", 0.5)])
|
47 |
+
memoria_robot.agregar_concepto("colores", [("rojo", 0.9), ("verde", 0.6), ("azul", 0.7)])
|
48 |
+
|
49 |
+
print("Memoria completa:")
|
50 |
+
print(memoria_robot.memoria)
|
51 |
+
|
52 |
+
memoria_robot.agregar_string("animales", "pez", 0.6)
|
53 |
+
memoria_robot.eliminar_string("colores", "verde")
|
54 |
+
memoria_robot.eliminar_concepto("colores")
|
55 |
+
|
56 |
+
print("\nMemoria después de modificaciones:")
|
57 |
+
print(memoria_robot.memoria)
|
58 |
+
|
59 |
+
conceptos_acotados = memoria_robot.obtener_conceptos_acotados(50)
|
60 |
+
print("\nConceptos acotados a un tamaño máximo de memoria:")
|
61 |
+
print(conceptos_acotados)
|
62 |
+
|