Today, I will explain how to use SPMF from Visual Basic .Net. Previously, I have explained how to call SPMF from C#, from R, from C++ (on Windows) and from Python.
Requirements
Let me first describe the requirements. It is important to have installed Visual Basic (VB) (for the .NET platform) and Java on your computer.
Second, you should download the spmf.jar file from the SPMF website and put it in the same directory as your VB program.
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 of your computer. 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 VB program
Now that I have described the basic requirements, I will first show you how to launch the GUI of SPMF from a VB program. For this, it is very simple. Here I give you the code of a simple VB program that calls the Jar file of SPMF to launch the GUI of SPMF:
Imports System Module Program Sub Main(args As String()) Process.Start("java", "-jar spmf.jar") End Sub End Module
What this program does? It basically just runs the command
java -jar spmf.jar
Running this Visual Basic program will launch the SPMF user interface as shown below:
2) Executing an algorithm from SPMF from a Visual Basic program
Now, let’s look at something more interesting. How can we run an algorithm from SPMF from VB? We just need to modify the above program a little bit. 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 here. 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.
Here is an example program that shows how to do this from Visual Basic:
Imports System Module Program Sub Main(args As String()) Process.Start("java", "-jar spmf.jar run Apriori contextPasquier99.txt output.txt 40%") End Sub End Module
Then, if we run this program in a folder that contains spmf.jar and contextPasquier99.txt, it will show the following information in the console indicating that the Apriori algorithm was run successfully:
And the program will write the output 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 beside Apriori, you can lookup the algorithm that you want to call in the SPMF documentation to see how to run it and then change the above program accordingly.
3) Executing an algorithm from SPMF from a VB program and then reading the output file
Now let me show you another example. I will explain how to call an algorithm from an SPMF and then read the output file from a VB program.
Generally, the output of algorithms from SPMF is a text file (such as in the above example). Thus, to read the output of an SPMF algorithm from VB, you just need to know how to read a text file from a VB program.
For example, I modified the previous VB program to run the Apriori algorithm, wait for the termination, and then read the content of the file “output.txt” that is produced by SPMF to show its content in the console.
This is the modified VB program:
Imports System Imports System.IO Module Program Sub Main(args As String()) 'Call SPMF To execute the Apriori algorithm' Dim psi As New ProcessStartInfo("java", "-jar spmf.jar run Apriori contextPasquier99.txt output.txt 40%") Dim p As New Process p.StartInfo = psi p.Start() p.WaitForExit() 'Read the output file' For Each line As String In File.ReadLines("output.txt") Console.WriteLine(line) Next line End Sub End Module
If we run the program, it will run the Apriori algorithm and then read the output file and write each line of the output file in the console, as expected:
We could further modify this program to do something more meaningful with the content of the output file. But at least, I wanted to show you the basic idea of how to read an output file from SPMF from a VB program.
3) Writing an input file for SPMF from a Visual Basic program, and then running an algorithm from SPMF
Lastly, you can also write the input file that is given to SPMF from a VB 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 VB. Here is the code:
Imports System Imports System.IO Module Program Sub Main(args As String()) 'Write an input file Using writer As New System.IO.StreamWriter("input.txt", True) writer.WriteLine("1 2 3 4") writer.WriteLine("2 3 4") writer.WriteLine("2 3 4 5 6") writer.WriteLine("1 2 4 5 6") End Using 'Call SPMF To execute the Apriori algorithm' Dim psi As New ProcessStartInfo("java", "-jar spmf.jar run Apriori input.txt output.txt 40%") Dim p As New Process p.StartInfo = psi p.Start() p.WaitForExit() 'Read the output file' For Each line As String In File.ReadLines("output.txt") Console.WriteLine(line) Next line End Sub End Module
After running this program, the file “input.txt” is successfully created:
And the content of the output file is shown in the console:
Conclusion
In this blog post, I have shown the basic idea of how to call SPMF from VB by calling SPMF as an external program. It is quite simple. It just require to know how to read/write files in VB. Hope that this information will be useful.
==
Philippe Fournier-Viger is a full professor and the founder of the open-source data mining software SPMF, offering more than 250 data mining algorithms.