|
import re |
|
|
|
class MemoryRobotNLP: |
|
def __init__(self, max_size): |
|
self.max_size = max_size |
|
self.memory = {} |
|
|
|
def add_concept(self, concepto, string): |
|
|
|
if concepto not in self.memory: |
|
self.memory[concepto] = [] |
|
|
|
priority = 1 - (1/len(concepto)) |
|
self.memory[concepto].append({"Found_text":string, "Priority_Weight":priority}) |
|
|
|
def delete_concept(self, concepto): |
|
if concepto in self.memory: |
|
del self.memory[concepto] |
|
|
|
def add_string(self, concepto, string, prioridad): |
|
if concepto not in self.memory: |
|
self.memory[concepto] = [] |
|
|
|
self.memory[concepto].append((string, prioridad)) |
|
|
|
def delete_string(self, concepto, string): |
|
if concepto in self.memory: |
|
self.memory[concepto] = [(s, p) for s, p in self.memory[concepto] if s != string] |
|
|
|
def search_concept_pattern(self, patron): |
|
resultados = {} |
|
|
|
for concepto, strings in self.memory.items(): |
|
for string, _ in strings: |
|
if re.search(patron, string,re.IGNORECASE): |
|
if concepto not in resultados: |
|
resultados[concepto] = [] |
|
resultados[concepto].append(string) |
|
|
|
return resultados |
|
def get_concepts_substrings(self, espacio_disponible): |
|
memoria_ordenada = sorted(self.memory.items(), key=lambda x: sum(prioridad for _, prioridad in x[1]), reverse=True) |
|
espacio_utilizado = 0 |
|
conceptos_acotados = [] |
|
|
|
for concepto, strings in memoria_ordenada: |
|
espacio_concepto = sum(prioridad for _, prioridad in strings) |
|
if espacio_utilizado + espacio_concepto <= espacio_disponible: |
|
conceptos_acotados.append((concepto, strings)) |
|
espacio_utilizado += espacio_concepto |
|
else: |
|
break |
|
|
|
return conceptos_acotados |
|
|
|
|
|
|
|
|
|
|
|
if __name__ == "__main__": |
|
|
|
memoria_robot = MemoryRobotNLP(max_size=100) |
|
|
|
memoria_robot.add_concept("animales", [("perro", 0.8), ("gato", 0.7), ("pájaro", 0.5)]) |
|
memoria_robot.add_concept("colores", [("rojo", 0.9), ("verde", 0.6), ("azul", 0.7)]) |
|
|
|
print("Memoria completa:") |
|
print(memoria_robot.memory) |
|
|
|
memoria_robot.add_string("animales", "pez", 0.6) |
|
memoria_robot.delete_string("colores", "verde") |
|
memoria_robot.delete_concepto("colores") |
|
|
|
print("\nMemoria después de modificaciones:") |
|
print(memoria_robot.memory) |
|
|
|
conceptos_acotados = memoria_robot.get_concepts_substrings(50) |
|
print("\nConceptos acotados a un tamaño máximo de memoria:") |
|
print(conceptos_acotados) |
|
|
|
|