Sample Swift project for MeCab, a Japanese tokenizer/morphological analyzer. Updated for iOS 12.x and Swift 5.
Use the following command to clone and get the mecab-ios submodule as well:
git clone --recurse-submodules https://github.com/landonepps/mecab-ios-sample.git
-
Create a new Xcode Swift project and make it a git repository (or let Xcode do it for you).
-
Add
mecab-iosas a submodule by executing the following command in your project's repository:
git submodule add https://github.com/landonepps/mecab-ios.git
-
Select
Add Files to "ProjectName"...from the Xcode menu and choosemecab.xcodeprojin themecab-iosfolder created by adding the submodule. This will import the project and all the necessary files. -
Drag
mecabrcfrom the top directory of this repository into your project. Make sure to selectCopy items if needed. -
Open the
mecab-iosfolder in your project's folder and drag theipadicfolder into your project. (The files are already in your project's directory. so it doesn't matter if you haveCopy items if neededselected or not.) -
Click on your project file, go to the target's
Generaltab, and addmecab.frameworkin theEmbedded Binariessection near the bottom. -
Make sure to
import mecabwherever you need to use it. -
You can now call the MeCab library's functions directly from Swift – e.g.
let mecab = mecab_new2("-d \(Bundle.main.resourcePath!)")-
Dealing with MeCab directly is not ideal, so feel free to use the Swift wrappers that I made for this sample project:
Tokenizer.swiftandToken.swift. -
Clean and build.
These instrucations are based on the ones from https://github.com/lxmmxl56/iPhone-libmecab
Special thanks to Joseph Toronto on Stack Overflow for coming up with the original steps.