1. Lars Yencken
  2. gpalign-cpp

Source

gpalign-cpp / src / kanjidic.cpp

//--------------------------------------------------------------------------//
// kanjidic.cpp
// Lars Yencken <lars.yencken@gmail.com>
// vim: ts=4 sw=4 sts=4 expandtab:
// Sat Oct  6 16:04:00 EST 2007
//--------------------------------------------------------------------------//

#include "kanjidic.hpp"
#include "io.hpp"
#include "platform.hpp"

#include <iostream>
#include <string>

using namespace std;

Kanjidic g_kanjidic("/home/lars/Documents/Research/alignment/hg.trunk/src/kanji.readings");

//--------------------------------------------------------------------------//

Kanjidic::Kanjidic(const char* filename)
{
    wifstream iStream(filename);
#ifndef BSD
    iStream.imbue(locale("en_US.UTF8"));
#endif
    wchar_t* reading;
    wchar_t* kanji;
    wchar_t* state;
    set<wstring> readings;
    wchar_t buffer[BUFFER_SIZE+1];
    int lineNo = 1;
    while (iStream.getline(buffer, BUFFER_SIZE)) {
        readings.clear();
        kanji = wcstok(buffer, L" ", &state);
        if (wcslen(kanji) != 1 || kanji == NULL) {
            cerr << "Warning: bad kanji reading line " << lineNo << endl;
        }
        lineNo++;

        reading = wcstok(NULL, L" ", &state);
        while (reading != NULL) {
            readings.insert(wstring(reading));
            reading = wcstok(NULL, L" ", &state);
        }
        m_table[*kanji] = readings;
    }
    iStream.close();
}

//--------------------------------------------------------------------------//

const ReadingSet* Kanjidic::operator[](wchar_t kanji) const
{
    map<wchar_t, ReadingSet>::const_iterator iter = m_table.find(kanji);
    if (iter != m_table.end()) {
        return &(iter->second);
    } else {
        return NULL;
    }
}

//--------------------------------------------------------------------------//

bool Kanjidic::hasReading(wchar_t kanji, const wstring& reading) const
{
    const ReadingSet* r = (*this)[kanji];
    if (r == NULL) {
        return false;
    }
    ReadingSet::const_iterator iter = r->find(reading);
    return (iter != r->end());
}

//--------------------------------------------------------------------------//