Newer
Older
scim-wnn / honoka / src / skkdic.cpp
/***************************************************************************
 *   Copyright (C) 2005 by TAM(Teppei Tamra)                               *
 *   tam-t@par.odn.ne.jp                                                   *
 *                                                                         *
 *   This program is free software; you can redistribute it and/or modify  *
 *   it under the terms of the GNU General Public License as published by  *
 *   the Free Software Foundation; either version 2 of the License, or     *
 *   (at your option) any later version.                                   *
 *                                                                         *
 *   This program is distributed in the hope that it will be useful,       *
 *   but WITHOUT ANY WARRANTY; without even the implied warranty of        *
 *   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the         *
 *   GNU General Public License for more details.                          *
 *                                                                         *
 *   You should have received a copy of the GNU General Public License     *
 *   along with this program; if not, write to the                         *
 *   Free Software Foundation, Inc.,                                       *
 *   59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.             *
 ***************************************************************************/

#include "skkdic.h"

SKKDic::SKKDic(String file)
{
    filename = file;
    iconv.set_encoding("EUC-JP");
    init();
}


SKKDic::~SKKDic()
{
}




/*!
    \fn SKKDic::init()
 */
void SKKDic::init()
{
    bool okuri = true;
    FILE *f = fopen(filename.c_str(),"r");
    if (!f) return;
    while(-1) {
        char s[1024];
        if(fgets(s,1024,f) == NULL) break;
        if (String(s) == String(";; okuri-ari entries.")) {
            okuri = true;
            continue;
        } else if (String(s) == String(";; okuri-nasi entries.")) {
            okuri = false;
            continue;
        }
        if (String(s).length() >= 2)
            if (String(s).substr(0,2) == ";;") continue;
        vector<WideString> sList;
        WideString wstr;
        iconv.convert(wstr,String(s));
        if (!wstr.length()) continue;
        uint pos = 0,count = 0;
        // "/"でsplit。
        while(-1) {
            if ((pos + count) >= wstr.length()) break;
            if (wstr.at(pos + count) == utf8_mbstowcs(String("/"))[0]) {
                if (count) sList.push_back(wstr.substr(pos,count));
                pos += (count + 1);
                count = 0;
                continue;
            }
            count ++;
        }
        // 読みの空白を削除。
        // @todo okuri-ari
        wstr = WideString();
        if (sList.size() < 2) continue;
        for(unsigned int i = 0;i < sList[0].length();i ++) {
            if (sList[0][i] != utf8_mbstowcs(String(" "))[0]) {
                wstr = wstr + sList[0][i];
            }
        }
        sList[0] = wstr;
        // アノテーション/候補摘出。
        SKKDicEntry dic;
        for(unsigned int i = 1;i < sList.size();i ++) {
            SKKDicEntryData e;
            e.cache = false;
            wstr = sList[i];
            /*if (!wstr.length()) continue;
            for(unsigned int j = 1;j < (wstr.length() - 1);j ++) {
                if (wstr.at(j) == utf8_mbstowcs(String(";"))[0]) {
                    e.annotation = wstr.substr(j + 1);
                    wstr = wstr.substr(0,j);
                    break;
                }
            }
            //if (!e.annotation.length()) 
            */
            e.kouho = wstr;
            dic.data.push_back(e);
        }
        dic_data.insert(pair<WideString,SKKDicEntry>(sList[0],dic));
    }
    fclose(f);
}


/*!
    \fn SKKDic::find(WideString text)
 */
const vector<SKKDicEntryData> SKKDic::find(WideString text)
{
    map<WideString,SKKDicEntry>::iterator it = dic_data.find(text);
    if (it == dic_data.end()) return vector<SKKDicEntryData>();
    //return it->second.data;
    vector<SKKDicEntryData> d;
    for(unsigned int i = 0;i < it->second.data.size();i ++) {
        if (!it->second.data[i].cache) {
            d.push_back(annotationParser(it->second.data[i].kouho));
        } else d.push_back(it->second.data[i]);
    }
    return d;
}


/*!
    \fn SKKDic::annotationParser(WideString)
 */
SKKDicEntryData SKKDic::annotationParser(WideString l)
{
    SKKDicEntryData e;
    e.cache = true;
    if (l.length() <= 2) {
        e.kouho = l;
        return e;
    }
    for(unsigned int j = 1;j < (l.length() - 1);j ++) {
        if (l.at(j) == utf8_mbstowcs(String(";"))[0]) {
            e.annotation = l.substr(j + 1);
            e.kouho = l.substr(0,j);
            break;
        }
    }
    if (!e.kouho.length()) e.kouho = l;
    return e;

}