My Project
machinelearning.cc
Go to the documentation of this file.
1 #include <stdio.h>
2 
3 #include "kernel/mod2.h"
4 #if defined(HAVE_READLINE) && defined(HAVE_READLINE_READLINE_H)
5 #include <readline/history.h>
6 #include "Singular/ipid.h"
7 #include "Singular/mod_lib.h"
8 
9 #ifdef HAVE_PYTHON
10 
11 #include "mlpredict.h"
12 
13 static BOOLEAN predictHelp(leftv result, leftv arg)
14 {
15  char *buffer[5];
16  int lengths[5];
17  char *filename = getenv("SINGULARHIST");
18  if (filename==NULL) filename=(char*)"~/.history";
19  int i;
21 
22  buffer[0] = NULL;
23  buffer[1] = NULL;
24  buffer[2] = NULL;
25  buffer[3] = NULL;
26  buffer[4] = NULL;
27 
28  if (write_history(filename))
29  {
30  printf("Failed to write history\n");
31  return TRUE;
32  }
33 
34  if (!ml_initialise())
35  {
36  WerrorS("Initialise of ml failed.");
37  /* Notify singular that an error occured */
38  return TRUE;
39  }
40  #ifdef HAVE_OMALLOC
41  ml_make_prediction(filename, buffer, lengths, _omStrDup);
42  #else /*xalloc*/
43  ml_make_prediction(filename, buffer, lengths, omStrDup);
44  #endif
45 
46  L->Init(5);
47 
48  for (i = 0; i < 5; i++) {
49  //printf("prediction %d: %s\n", i, buffer[i]);
50  L->m[i].rtyp = STRING_CMD;
51  L->m[i].data = buffer[i];
52  }
53 
54  // pass the resultant list to the res datastructure
55  result->rtyp=LIST_CMD;
56  result->data=(void *)L;
57 
58  ml_finalise();
59  return FALSE;
60 
61  //result->rtyp=NONE; // set the result type
62 }
63 
64 // initialisation of the module
65 extern "C" int SI_MOD_INIT(machinelearning)(SModulFunctions* psModulFunctions)
66 {
67  char *filename = getenv("SINGULARHIST");
68  if (filename==NULL)
69  {
70  WarnS("SINGULARHIST is not set - no history available, using ~/.history");
71  }
72  // this is the initialization routine of the module
73  // adding the routine predictHelp:
74  psModulFunctions->iiAddCproc(
75  (currPack->libname? currPack->libname: ""),
76  "predictHelp",// for the singular interpreter
77  FALSE, // should enter the global name space
78  predictHelp); // the C/C++ routine
79  return MAX_TOK;
80 }
81 #endif
82 #endif
int BOOLEAN
Definition: auxiliary.h:87
#define TRUE
Definition: auxiliary.h:100
#define FALSE
Definition: auxiliary.h:96
int i
Definition: cfEzgcd.cc:132
Class used for (list of) interpreter objects.
Definition: subexpr.h:83
int rtyp
Definition: subexpr.h:91
void * data
Definition: subexpr.h:88
Definition: lists.h:24
sleftv * m
Definition: lists.h:46
INLINE_THIS void Init(int l=0)
#define WarnS
Definition: emacs.cc:78
return result
Definition: facAbsBiFact.cc:75
void WerrorS(const char *s)
Definition: feFopen.cc:24
char * getenv()
int write_history()
VAR package currPack
Definition: ipid.cc:57
VAR omBin slists_bin
Definition: lists.cc:23
Function definitions for using python to do machine learning in Singular.
slists * lists
Definition: mpr_numeric.h:146
#define omStrDup(s)
Definition: omAllocDecl.h:263
#define omAllocBin(bin)
Definition: omAllocDecl.h:205
OM_INLINE_DECL char * _omStrDup(const char *s)
#define NULL
Definition: omList.c:12
@ LIST_CMD
Definition: tok.h:118
@ STRING_CMD
Definition: tok.h:185
@ MAX_TOK
Definition: tok.h:218