How to call SPMF from a C++ Program (Windows)?

I will explain how to call SPMF from a C++ program for the Windows platform. If you are interested by other programming languages, you can check my previous blog posts, where I give examples of how to call SPMF from Python and from C# and from R.

To call SPMF from C++ there are various ways. Here, I will show the most simple way, which is to call SPMF as an external program.

Requirements

As SPMF is a Java software, it is important to first install Java on your computer. Moreover, to compile the C++ program from this blog post, I will use Microsoft Visual Studio. If you are using other compilers for C++, the code might be a little different.

Second, you should download the spmf.jar file from the SPMF website.

Third, you should make sure that your Java installation is correct. In particular, you should be able to execute the java command from the command line (terminal) of your computer because we will use the java command to call SPMF. If you type “java -version” in the command line of your computer, you should see the version of Java:

If you see this, then it is OK.

If you do not see this but instead get an error that java.exe is not found, it means that you have not installed Java, or that the PATH to Java is not setup properly on your computer so you cannot use it from the command line. If you are using the Windows operating System and you have installed Java, you need to make sure that java.exe is in the PATH environment variable. On Windows 11, you can fix this problem as follows: 1) Press WINDOWS + R, 2) Run the command “sysdm.cpl“, 3) Click the Advanced system settings tab. 4) Click Environment Variables. 5) In the section System Variables find the PATH environment variable and select it. 6) Click Edit. Add the path to the folder containing java.exe, which will be something like : C:\Program Files\Java\jdk-17.0.1\bin (depending on your version of Java and where you have installed it). 7) Click OK and close all windows. Then, you can open a new command prompt and try running “java -version” again to see if the problem is fixed. If you are using another version of Windows or the Linux operating system, you can find similar steps online about how to setup Java on your computer.

1) Launching the GUI of SPMF from a C++ program

Now that I have explained the basic requirements, I will first show you how to launch the GUI of SPMF from C++. For this, it is very simple. Here I give you the code of a simple C++ program that calls the Jar file of SPMF to launch the GUI of SPMF.

#include <iostream>
using namespace std;

int main()
{
    // Run the graphical interface of SPMF
    const char* command = "java -jar spmf.jar";
    system(command);
}

What this program does? It basically just runs the command
java -jar spmf.jar

By running this program, SPMF is successfully launched:

spmf data mining interface

2) Executing an algorithm from SPMF from a C++ program

Next, I will explain something more useful, that is how to run an algorithm from SPMF from a C++ program? We will modify the above program to do this. Let’s say that we want to run the Apriori algorithm on an input file called contextPasquier99.txt (this file is included with SPMF and can be downloaded here).

To do this, we need to first check the documentation of SPMF to see how to run the Apriori algorithm from the command line. The documentation of SPMF is here. How to run Apriori is explained in this page of the documentation. We find that we can use this command

java -jar spmf.jar run Apriori contextPasquier99.txt output.txt 40%

to run Apriori on the file contextPasquier99.txt with the parameter minsup = 40% and to save the result to a file output.txt.

To do this from C++, we can write a simple C++ program like this:

 #include <iostream>
using namespace std;

int main()
{
 
    // Run Apriori on the text file
    const char* command = "java -jar spmf.jar run Apriori contextPasquier99.txt output.txt 40%";
    system(command);
}

If we execute this program, it will show that this in the console:

And the program will produce the file output.txt as result:

If we open the file “output.txt”, we can see the content:

Each line of this file is a frequent itemset found by the Apriori algorithm. To understand the input and output file format of Apriori, you can see the documentation of the Apriori algorithm.

If you want to call other algorithms that are offered in SPMF besides Apriori, you can lookup the algorithm that you want to call in the SPMF documentation. An example is provided for each algorithm in the SPMF documentation and explanation of how to run it.

3) Executing an algorithm from SPMF from a C++ program and then reading the output file

Now, I will explain how to read the output file produce by SPMF from a C++ program. When running an algorithm of SPMF such as in the previous example, the output is generally a text file. We can easily read an output file from C++ to obtain the content.

For instance, I modified the previous C++ program to read the content of the file “output.txt” that is produced by SPMF to show its content in the console. The new C++ program is below:

#include <iostream>
#include <fstream>
#include <string>
using namespace std;

int main()
{
 
    // Run Apriori on the text file
    const char* command = "java -jar spmf.jar run Apriori input.txt output.txt 40%";
    system(command);

    // Read and display the content of the output file
    fstream outputFile;
    outputFile.open("output.txt", ios::in); //open a file to perform read operation using file object
    if (outputFile.is_open()) {   //checking whether the file is open
        string sa;
        while (getline(outputFile, sa)) { //read data from the file object and put it into a string.
            cout << sa << "\n"; //print the data of the string
        }
        outputFile.close(); //close the file object.
    }

    system("pause");
}

If we execute this C++ program, it will first call the Apriori algorithm from SPMF. Then, the program will read the content of the output file output.txt line by line and display the content in the console:

We could further modify this program to do something more meaningful with the content of the output file such as reading the content in some data structures to do further processing. But at least, I wanted to show you the basic idea of how to read an output file from SPMF from a C++ program.

3) Writing an input file for SPMF from a C++ program, and then running an algorithm from SPMF

Lastly, you can also write the input file that is given to SPMF from a C++ program by using code to write a text file.

For example, I will modify the example above to write a new text file called “input.txt” that will contain the following data:

1 2 3 4
2 3 4
2 3 4 5 6
1 2 4 5 6

and then I will call SPMF to execute the Apriori algorithm on that file. Then, the program will read the output file “output.txt” from C++. Here is the code:

#include <iostream>
#include <fstream>
#include <string>
using namespace std;

int main()
{
    // Write a text file
    fstream inputFile;
    inputFile.open("xyz.txt", ios::out);  // open a file to perform write operation using file object
    if (inputFile.is_open()) //checking whether the file is open
    {
        inputFile << "1 2 3 4\n";   //inserting text
        inputFile << "2 3 4\n";   //inserting text
        inputFile << "2 3 4 5 6\n";   //inserting text
        inputFile << "1 2 4 5 6";   //inserting text
        inputFile.close();    //close the file object
    }

    // Run Apriori on the text file
    const char* command = "java -jar spmf.jar run Apriori input.txt output.txt 40%";
    system(command);

    // Read and display the content of the output file
    fstream outputFile;
    outputFile.open("output.txt", ios::in); //open a file to perform read operation using file object
    if (outputFile.is_open()) {   //checking whether the file is open
        string sa;
        while (getline(outputFile, sa)) { //read data from the file object and put it into a string.
            cout << sa << "\n"; //print the data of the string
        }
        outputFile.close(); //close the file object.
    }

    system("pause");
}

By running this program, the file “input.txt” is successfully created:

And the Apriori algorithm is applied which produces an output file output.txt. Then, the C++ program reads the content of that file and show it in the console:

Conclusion

In this blog post, I have shown the basic idea of how to call SPMF from C++ by calling SPMF as an external program. It is quite simple. It just require to know how to read/write files in C++, and call an external program.

Hope that this has been interesting.

==
Philippe Fournier-Viger is a full professor  and the founder of the open-source data mining software SPMF, offering more than 110 data mining algorithms. If you like this blog, you can tweet about it and/or subscribe to my twitter account @philfv to get notified about new posts.

This entry was posted in Data Mining, Data science, open-source, spmf and tagged , , , , , , , . Bookmark the permalink.

Leave a Reply

Your email address will not be published. Required fields are marked *