daily pastebin goal
24%
SHARE
TWEET

Untitled

a guest Jan 13th, 2018 53 Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. /**
  2.  * aqfuncs.c - Hilfsfunktionen fuer den AQ-Basis algorithmus
  3.  */
  4.  
  5. #include <errno.h>
  6. #include <stdio.h>
  7. #include <stdlib.h>
  8. #include <string.h>
  9. #include "aqfuncs.h"
  10.  
  11. // TODO del
  12. // ====================================================
  13. /* print_sample: print a 'struct sample' to 'out' */
  14. void print_sample(struct sample *s) {
  15.     int i;
  16.     printf("{");
  17.     for (i = 0; i < NUM_FIELDS; ++i)
  18.         if (i < NUM_FIELDS - 1)
  19.             printf("%s, ", s->values[i]);
  20.         else
  21.             printf("%s", s->values[i]);
  22.     printf("}\n");
  23. }
  24.  
  25. /* print_samples: print a list of samples to 'out' */
  26. void print_samples(struct list_head *head) {
  27.     struct sample *pos;
  28.     list_for_each_entry(pos, head, list)
  29.         print_sample(pos);
  30. }
  31. /* free_sample: free a 'struct sample' object */
  32. void free_sample(struct sample *s) {
  33.     int i;
  34.     for (i = 0; i < NUM_FIELDS; ++i)
  35.         if (s->values[i])
  36.             free(s->values[i]);
  37.     free(s);
  38. }
  39. /* delete_samples: free memory occupied by a list of samples */
  40. void delete_samples(struct list_head *head) {
  41.     struct sample *pos, *q;
  42.     list_for_each_entry_safe(pos, q, head, list) {
  43.         list_del(&pos->list);
  44.         free_sample(pos);
  45.     }
  46. }
  47.  
  48. // ====================================================
  49.  
  50. /**
  51.  * Specifies a concept of a positive sample regarding a negative sample. Returns a list of specified concepts
  52.  *
  53.  * gl   List head to store specified concepts in
  54.  * og   Concept to specify
  55.  * p    Positiv sample
  56.  * n    Negative sample
  57.  */
  58. void specify(struct list_head * gl, struct sample * og, struct sample * p,
  59.         struct sample * n) {
  60.     int i, j;
  61.     struct sample *c;
  62.  
  63.     for (i = 0; i < NUM_FIELDS; i++) {
  64.         c = calloc(1, sizeof(*c));
  65.         for (j = 0; j < NUM_FIELDS; j++) {
  66.             c->values[j] = malloc(50+ 1);
  67.             strcpy(c->values[j], WILDCARD);
  68.         }
  69.  
  70.         if (strcmp(og->values[i], WILDCARD) == 0) {
  71.  
  72.             strcpy(c->values[i], p->values[i]);
  73.  
  74.             // fill all other attributes with wildcard (*)
  75.             for (j = 0; j < NUM_FIELDS; j++) {
  76.                 if (i != j && strcmp(og->values[j], WILDCARD) == 0) {
  77.                     strcpy(c->values[j], WILDCARD);
  78.                 } else if (i != j) {
  79.                     strcpy(c->values[j], og->values[j]);
  80.                 }
  81.             }
  82.  
  83.  
  84.             if (match(c, n) != NUM_FIELDS) {
  85.  
  86.                 // add element to concepts list
  87.                 list_add_tail(&c->list, gl);
  88.             } else {
  89.                 free_sample(c);
  90.             }
  91.         }  else {
  92.             free_sample(c);
  93.         }
  94.     }
  95. }
  96. /**
  97.  * Creates a list of concepts for a positive sample
  98.  *
  99.  * gl   List head to store all concepts in
  100.  * p    Positive sample
  101.  * nl   List of negativ samples
  102.  */
  103. void star(struct list_head * gl, struct sample * p, struct list_head *nl) {
  104.     struct sample *u, *g, *n, *q;
  105.     int i;
  106.  
  107.     // init empty g
  108.     if (list_empty(gl)) {
  109.         u = calloc(1, sizeof(*u));
  110.         for (i = 0; i < NUM_FIELDS; i++) {
  111.             u->values[i] = malloc(10+ 1);
  112.             strcpy(u->values[i], WILDCARD);
  113.         }
  114.         list_add(&u->list, gl);
  115.     }
  116.  
  117.     list_for_each_entry_safe(g, q, gl, list) {
  118.         printf("Test concept:");
  119.         print_sample(g);
  120.         list_for_each_entry(n, nl, list) {
  121.             if (match(g, n) == NUM_FIELDS) {
  122.                 printf("Negative sample matched: ");
  123.                 print_sample(n);
  124.  
  125.                 // specify g
  126.                 specify(gl, g, p, n);
  127.  
  128.                 // rm old g
  129.                 list_del(&g->list);
  130.  
  131.                 printf("Specified to: \n");
  132.                 print_samples(gl);
  133.  
  134.                 break;
  135.             }
  136.         }
  137.     }
  138.  
  139.     printf("star done\n");
  140. }
  141.  
  142. /*
  143.  * Returns the number of correct matches between concept $c and sample $s
  144.  */
  145. int match(struct sample* c, struct sample* s) {
  146.     int matches, i;
  147.     matches = 0;
  148.     for (i = 0; i < NUM_FIELDS; i++) {
  149.         if (strcmp(c->values[i], WILDCARD) == 0 || strcmp(s->values[i],
  150.                 WILDCARD) == 0 || strcmp(s->values[i], c->values[i]) == 0) {
  151.             matches++;
  152.         }
  153.     }
  154.     return matches;
  155. }
  156.  
  157. /*
  158.  * Removes all positive examples from $pos which matches at least one concept in $g
  159.  */
  160. void cleanPos(struct list_head *g, struct list_head *pos) {
  161.     struct sample *ps, *c, *q;
  162.     list_for_each_entry(c, g, list) {
  163.         list_for_each_entry_safe(ps, q, pos, list) {
  164.             if (match(c, ps) == NUM_FIELDS) {
  165.                 printf("Delete positive example:");
  166.                 print_sample(ps);
  167.                 list_del(&ps->list);
  168.                 free_sample(ps);
  169.             }
  170.         }
  171.     }
  172. }
  173.  
  174. // ====================================================
  175. /* main, a lot of example stuff: */
  176. int main(int argc, char *argv[]) {
  177.     struct sample *pos, *ns;
  178.     LIST_HEAD(c);
  179.     LIST_HEAD(nsl);
  180.  
  181.    
  182.  
  183.     pos = calloc(1, sizeof(*pos));
  184.  
  185.     // pos
  186.     pos->values[0] = malloc(10 + 1);
  187.     pos->values[1] = malloc(10 + 1);
  188.     pos->values[2] = malloc(10 + 1);
  189.     strcpy(pos->values[0], "A");
  190.     strcpy(pos->values[1], "B");
  191.     strcpy(pos->values[2], "C");
  192.  
  193.     // neg
  194.     ns = calloc(1, sizeof(*ns));
  195.     ns->values[0] = malloc(10 + 1);
  196.     ns->values[1] = malloc(10 + 1);
  197.     ns->values[2] = malloc(10 + 1);
  198.     strcpy(ns->values[0], "A");
  199.     strcpy(ns->values[1], "A");
  200.     strcpy(ns->values[2], "D");
  201.  
  202.     list_add(&ns->list, &nsl);
  203.  
  204.     // neg
  205.     ns = calloc(1, sizeof(*ns));
  206.     ns->values[0] = malloc(10 + 1);
  207.     ns->values[1] = malloc(10 + 1);
  208.     ns->values[2] = malloc(10 + 1);
  209.     strcpy(ns->values[0], "R");
  210.     strcpy(ns->values[1], "B");
  211.     strcpy(ns->values[2], "D");
  212.  
  213.     list_add(&ns->list, &nsl);
  214.  
  215.  
  216.     printf("star...\n");
  217.     star(&c, pos, &nsl);
  218.  
  219.     printf("print...\n");
  220.     //print_samples(&c);
  221.  
  222.     printf("\ndeleting:");
  223.     delete_samples(&c);
  224.     delete_samples(&nsl);
  225.  
  226.     free_sample(pos);
  227.     free(ns);
  228.  
  229.     printf("done\n");
  230.     return 1;
  231. }
RAW Paste Data
Top