Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse files

ASCII graph representation

  • Loading branch information...
commit 3176c5924de67a86a6131f17ff103b4045b05740 1 parent 3c1ad93
Mihai Maruseac authored
Showing with 134 additions and 2 deletions.
  1. +4 −0 id3.c
  2. +73 −1 id3graph.c
  3. +56 −0 id3graph.h
  4. +1 −1  id3learn.c
View
4 id3.c
@@ -174,6 +174,10 @@ static void learning_instance(int argc, char **argv)
usage();
}
+/**
+ * @brief Parses the cmd line for the case when we wish to represent an id3
+ * tree corresponding to a classifier.
+ */
static void graphing_instance(int argc, char **argv)
{
char *id3_file, *out_file;
View
74 id3graph.c
@@ -1,5 +1,5 @@
/*!
- * @file id3graph.h
+ * @file id3graph.c
* @brief Displaying id3 classifiers
* @author Mihai Maruseac (mihai@rosedu.org)
*
@@ -26,6 +26,7 @@ int id3_output_graph(FILE *id3, FILE *out, int graph_mode)
descr = read_description_file(id3);
cls = read_classifier(id3);
CHECK(cls != NULL, fail);
+ graph_classifier(descr, cls, graph_mode, out);
free_description(descr);
free_and_set_NULL(descr);
@@ -38,3 +39,74 @@ int id3_output_graph(FILE *id3, FILE *out, int graph_mode)
return set_error(EINVAL);
}
+void graph_classifier(const struct description *descr,
+ const struct classifier *cls, int graph_mode, FILE* out)
+{
+ switch (graph_mode) {
+ case TREE_ASCII:
+ graph_ascii(descr, cls, out, 0);
+ break;
+ case TREE_DOT:
+ graph_dot(descr, cls, out);
+ break;
+ case TREE_SCHEME:
+ graph_scheme(descr, cls, out);
+ break;
+ case TREE_IFTHEN:
+ graph_ifthen(descr, cls, out);
+ break;
+ }
+}
+
+void graph_ascii(const struct description *descr,
+ const struct classifier *cls, FILE* out, int level)
+{
+ int i, aid;
+ char *name, *val;
+
+ aid = cls->id;
+ name = descr->attribs[aid]->name;
+
+ if (cls->C == 0) {
+ fprintf(out, "%*c==> %s\n", TABS * level + 1, ' ',
+ descr->classes[cls->id]);
+ return;
+ }
+ if (descr->attribs[aid]->type == NUMERIC) {
+ for (i = 0; i < cls->C - 1; i++) {
+ fprintf(out, "%*c%s < %d\n", TABS * level + 1,
+ ' ', name, cls->values[i]);
+ graph_ascii(descr, cls->cls[i], out, level + 1);
+ }
+ fprintf(out, "%*c%s >= %d\n", TABS * level + 1, ' ',
+ name, cls->values[i - 1]);
+ graph_ascii(descr, cls->cls[i], out, level + 1);
+ } else
+ for (i = 0; i < cls->C; i++) {
+ val = (char *)
+ descr->attribs[aid]->
+ ptr[cls->values[i]];
+ fprintf(out, "%*c%s = %s\n", TABS * level + 1,
+ ' ', name, val);
+ graph_ascii(descr, cls->cls[i], out, level + 1);
+ }
+}
+
+void graph_dot(const struct description *descr,
+ const struct classifier *cls, FILE* out)
+{
+ fprintf(stderr, "TODO %s %d\n", __FILE__, __LINE__);
+}
+
+void graph_scheme(const struct description *descr,
+ const struct classifier *cls, FILE* out)
+{
+ fprintf(stderr, "TODO %s %d\n", __FILE__, __LINE__);
+}
+
+void graph_ifthen(const struct description *descr,
+ const struct classifier *cls, FILE* out)
+{
+ fprintf(stderr, "TODO %s %d\n", __FILE__, __LINE__);
+}
+
View
56 id3graph.h
@@ -18,6 +18,8 @@
#ifndef _ID3GRAPH_H
#define _ID3GRAPH_H
+#define TABS 2 /**< @brief Tab stop for ASCII art display */
+
/**
* @brief Bootstraps the outputing process.
*
@@ -28,5 +30,59 @@
*/
int id3_output_graph(FILE *id3, FILE *out, int graph_mode);
+/**
+ * @brief Outputs a classifier to graph form (depending on output type).
+ *
+ * Only a dispatch to the good method.
+ *
+ * @param descr The description to use
+ * @param cls The classifier
+ * @param graph_mode Mode of output
+ * @param out Where to output
+ */
+static void graph_classifier(const struct description *descr,
+ const struct classifier *cls, int graph_mode, FILE* out);
+
+/**
+ * @brief Outputs the classifier to ASCII art.
+ *
+ * @param descr The description to use
+ * @param cls The classifier
+ * @param out Where to output
+ * @param level Level of indentation
+ */
+static void graph_ascii(const struct description *descr,
+ const struct classifier *cls, FILE* out, int level);
+
+/**
+ * @brief Outputs the classifier using Dot.
+ *
+ * @param descr The description to use
+ * @param cls The classifier
+ * @param out Where to output
+ */
+static void graph_dot(const struct description *descr,
+ const struct classifier *cls, FILE* out);
+
+/**
+ * @brief Outputs the classifier to Scheme/Lisp constructs.
+ *
+ * @param descr The description to use
+ * @param cls The classifier
+ * @param out Where to output
+ */
+static void graph_scheme(const struct description *descr,
+ const struct classifier *cls, FILE* out);
+
+/**
+ * @brief Outputs the classifier to if-then constructs
+ *
+ * @param descr The description to use
+ * @param cls The classifier
+ * @param out Where to output
+ */
+static void graph_ifthen(const struct description *descr,
+ const struct classifier *cls, FILE* out);
+
#endif
View
2  id3learn.c
@@ -460,7 +460,7 @@ void id3_attr_discr(const struct description *descr,
goto end;
}
- fprintf(stderr, "TODO %d\n", __LINE__);
+ fprintf(stderr, "TODO %s %d\n", __FILE__, __LINE__);
for (i = 0; i < ccount; i++)
fprintf(stderr, "%d(%5.3lf) ", candidates[i], entropies[i]);
Please sign in to comment.
Something went wrong with that request. Please try again.