Skip to content

Instantly share code, notes, and snippets.

@hecomi hecomi/str2kana.cpp

Created Feb 7, 2012
Embed
What would you like to do?
文章をカタカナに変換(transformed ver.)
#include <iostream>
#include <string>
#include <vector>
#include <boost/shared_ptr.hpp>
#include <boost/spirit/include/qi.hpp>
#include <boost/range/adaptor/transformed.hpp>
#include <mecab.h>
namespace qi = boost::spirit::qi;
using qi::standard_wide::char_;
using qi::_1;
using boost::adaptors::transformed;
struct mecab_result_of
{
typedef std::string result_type;
int n_;
mecab_result_of(int n) : n_(n) {}
result_type operator() (const result_type& str) const {
std::vector<result_type> v;
result_type::const_iterator
first = str.begin(),
last = str.end();
qi::parse(first, last, +(char_-',')%',', v);
return v[n_];
}
};
int main(int argc, char* argv[])
{
// MeCab による形態素解析
std::string input = "今日は良い天気ですなぁ。";
boost::shared_ptr<MeCab::Tagger> tagger(MeCab::createTagger(""));
const MeCab::Node* node = tagger->parseToNode(input.c_str());
// 結果をコンテナに突っ込む
std::vector<std::string> features;
for (node = node->next; node->next; node = node->next) {
features.push_back(node->feature);
}
// 発音箇所だけ取り出す
std::string s = "";
for ( const std::string& x : features | transformed(mecab_result_of(8)) ) {
s += x;
}
std::cout << s << std::endl;
return 0;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.