Imports System.Runtime.InteropServices
Public Class Form1
Dim TextBox1 As New TextBox
Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
Me.TextBox1.Multiline = True
Me.TextBox1.ScrollBars = ScrollBars.Vertical
Me.TextBox1.Dock = DockStyle.Fill
Me.Controls.Add(Me.TextBox1)
Using Mecab As New MeCab(" --rcfile=.\dic\ipadic\dicrc --dicdir=.\dic\ipadic")
Me.TextBox1.Text = Mecab.Parse("和布蕪を使って日本語文字列を形態素分析する。").Replace(vbLf, vbCrLf)
End Using
End Sub
End Class
Class MeCab
Implements IDisposable
<DllImport("libmecab.dll", CallingConvention:=CallingConvention.Cdecl)>
Public Shared Function mecab_new2(ByVal arg As String) As IntPtr
End Function
<DllImport("libmecab.dll", CallingConvention:=CallingConvention.Cdecl)>
Public Shared Function mecab_sparse_tostr(ByVal m As IntPtr, ByVal str As String) As IntPtr
End Function
<DllImport("libmecab.dll", CallingConvention:=CallingConvention.Cdecl)>
Public Shared Sub mecab_destroy(ByVal m As IntPtr)
End Sub
Private ptrMeCab As IntPtr
Sub New()
Me.New(String.Empty)
End Sub
Sub New(ByVal Arg As String)
ptrMeCab = mecab_new2(Arg)
End Sub
Public Function Parse(ByVal [String] As String) As String
Dim ptrResult As IntPtr = mecab_sparse_tostr(ptrMeCab, [String])
Dim strResult As String = Marshal.PtrToStringAnsi(ptrResult)
Return strResult
End Function
Public Overloads Sub Dispose() Implements IDisposable.Dispose
mecab_destroy(ptrMeCab)
GC.SuppressFinalize(Me)
End Sub
Protected Overrides Sub Finalize()
Dispose()
End Sub
End Class