/* * formato: * <numero de transacciones> * I1,I2,I3...IN * * ejemplo: * 5 * a,b,c,d,e,f,g,h,i,j * a,b,c * d,e,f * g,h,i * j,a,b * c,d,e * * */ public void importarInformacion(String ruta = "datos.txt", double soporte = 0.2) { tree = new FPTree(); SortedSet <Item> dic = new SortedSet <Item>(new comparadorPorOcurrencia()); //INICIO PRIMERA LECTURA StreamReader lector = new StreamReader(ruta); int numTransacciones = Convert.ToInt32(lector.ReadLine()); this.epsilon = (int)(soporte * numTransacciones); Dictionary <String, int> dictionary = new Dictionary <String, int>(); for (int i = 0; i < numTransacciones; i++) { String[] itemsLector = lector.ReadLine().Split(','); for (int j = 0; j < itemsLector.Length; j++) { if (dictionary.ContainsKey(itemsLector[j])) { dictionary[itemsLector[j]] = dictionary[itemsLector[j]] + 1; } else { dictionary.Add(itemsLector[j], 1); } } } lector.Close(); //FIN PRIMERA LECTURA foreach (KeyValuePair <string, int> entry in dictionary) { if (entry.Value > epsilon) { dic.Add(new Item(entry.Key, entry.Value)); } } //Borra el diccionario dictionary = null; //INICIO SEGUNDA LECTURA lector = new StreamReader(ruta); lector.ReadLine(); for (int i = 0; i < numTransacciones; i++) { String[] itemsLector = lector.ReadLine().Split(','); List <string> t = new List <string>(); //TODO esto se puede mejorar foreach (Item ii in dic) { if (itemsLector.Contains(ii.Key)) { t.Add(ii.Key); } } //FIN TODO tree.addTransaction(t); } lector.Close(); }
private FPTree Podar(FPTree conditional, FPNode node, int epsilon) { HashSet <String> conteo = new HashSet <String>();; foreach (KeyValuePair <String, FPNode> entry in conditional.headersTable) { Int32 i = 0; FPNode n = entry.Value; while (n != null) { i += n.Conteo; n = n.Siguiente; } if (i >= epsilon) { conteo.Add(entry.Key); } } //instancia un nuevo arbol FPTree respuesta = new FPTree(); //optiene el nodo adecuado node = conditional.headersTable[node.Contenido]; //va a recorrer sus caminos y meterlos al arbol while (node != null) { //optiene el padre FPNode temp = node.Padre; //instancia la transaccion LinkedList <String> transaccion = new LinkedList <string>(); //mete los valores previos al nodo en la transaccion, si es adecuado while (temp.Contenido != null) { if (conteo.Contains <String>(temp.Contenido)) { transaccion.AddFirst(temp.Contenido); } temp = temp.Padre; } //si la transaccion no esta vacia if (transaccion.First != null) { //mete la transaccion con su conteo respectivo respuesta.addTransaction(transaccion, node.Conteo); } //pasa al siguiente camino node = node.Siguiente; } return(respuesta); }
//optiene el subarbol con los caminos respectivos sin filtrar private FPTree getCaminos(FPNode node) { //instancia un nuevo arbol FPTree caminos = new FPTree(); //va a recorrer sus caminos y meterlos al arbol while (node != null) { //optiene el padre //OJO: se cambio //OJO: se cambio //OJO: se cambio //OJO: se cambio //OJO: se cambio //OJO: se cambio //OJO: se cambio //OJO: se cambio FPNode temp = node; //OJO: se cambio //OJO: se cambio //OJO: se cambio //OJO: se cambio //OJO: se cambio //OJO: se cambio //OJO: se cambio //OJO: se cambio //utiliza una linkedList ya que el metodo addFirst es O(1) //instancia la transaccion LinkedList <String> transaccion = new LinkedList <string>(); //mete los valores previos al nodo en la transaccion while (temp.Contenido != null) { transaccion.AddFirst(temp.Contenido); temp = temp.Padre; } //si la transaccion no esta vacia if (transaccion.First != null) { //mete la transaccion con su conteo respectivo caminos.addTransaction(transaccion, node.Conteo); } //pasa al siguiente camino node = node.Siguiente; } return(caminos); }
private FPTree GetConditional(FPNode node, int epsilon) { //inicia el arbol con todos los caminos FPTree conditional = getCaminos(node); //inicia una variable que cuenta todo return(Podar(conditional, node, epsilon)); /* * //empieza a filtrar * //hace una copia de la headersTable ya que no se puede modificar y recorrer al mismo tiempo * //para cada item en el subArbol * foreach (FPNode entrada in Extenciones.CloneNode<FPNode>(conditional.headersTable.Values.ToList())) * { * //cuenta cuantos items hay * int suma = 0; * FPNode n = entrada; * while (n != null) * { * suma += n.Conteo; * n = n.Siguiente; * } * * * //si es menor que el epsilon * if (suma < epsilon) * { * //lo remueve * conditional.headersTable.Remove(entrada.Contenido); * n = entrada; * while (n != null) * { * n.Padre.Hijos.Remove(entrada.Contenido); * sumar(n.Padre, n.Hijos); * n = n.Siguiente; * } * } * } * */ }
public HashSet <ItemsSet <String> > FPGrow(int epsilon, List <String> previos = null) { //si previos es default if (previos == null) { //instancia a previos previos = new List <string>(); } //crea el conjuto de respuesta HashSet <ItemsSet <String> > respuesta = new HashSet <ItemsSet <string> >(); //para cada item en el arbol foreach (KeyValuePair <string, FPNode> entry in headersTable) { //obtiene su nodo inicial FPNode node = entry.Value; //crea un nuevo set de items ItemsSet <String> items = new ItemsSet <string>(); //agrega los los items previos que se han hecho y an llevado a esta instancia de arbol items.UnionWith(previos); //agrega el nodo que se esta evaluando items.Add(node.Contenido); //agrega a la respueta el set de items respuesta.Add(items); //crea una copia de los previos List <String> nueva = Extenciones.Clone <String>(previos); //agrega el actual nueva.Add(node.Contenido); //genera el arbol condicional para el item actual FPTree conditional = GetConditional(node, epsilon); //llama recursivamente sobre el arbol condicional y une las respuestas respuesta.UnionWith(conditional.FPGrow(epsilon, nueva)); } return(respuesta); }