#include "ymfilterchildren.h"
#include <QRegExp>
#include <QStringList>
#include <QDebug>
YMFilterFactory::YMFilterFactory()
{

}


YMFilterFactory::~YMFilterFactory()
{

}


YMFilter* YMFilterFactory::getFilter(QMap<QString, QString> mapParam)
{
    if(mapParam.value("Algorithm").trimmed().compare("onedepth", Qt::CaseInsensitive) == 0)
        return new YMOneDepthFilter();
    else if(mapParam.value("Algorithm").trimmed().compare("twodepth", Qt::CaseInsensitive) == 0)
        return new YMTwoDepthFilter();
    else if(mapParam.value("Algorithm").trimmed().compare("twodeptha", Qt::CaseInsensitive) == 0)
        return new YMTwoDepthFilter();
    else if(mapParam.value("Algorithm").trimmed().compare("platformone", Qt::CaseInsensitive) == 0)
        return new YMPlatformOneFilter();
    else if(mapParam.value("Algorithm").trimmed().compare("platform", Qt::CaseInsensitive) == 0)
        return new YMPlatformFilter();
    else
        return NULL;
}


YMOneDepthFilter::YMOneDepthFilter()
{

}


YMOneDepthFilter::~YMOneDepthFilter()
{

}

QMap<QString, int> YMOneDepthFilter::Extractor(QMap<QString, int> mapResult)
{
    QMap<QString, int> totalResult;
    QStringList slExtractor;
    QRegExp rx("[\\s,.;]");
    slExtractor = m_mapParam.value("Extractor").split(rx);

    for(QMap<QString, int>::iterator iterPos = mapResult.begin(); iterPos != mapResult.end(); iterPos++)
    {
        QString strkey = iterPos.key();
        QStringList slkey = strkey.split("~!@", QString::SkipEmptyParts);
        int nLastIndex = slkey.length() - 1;

        foreach(QString str, slExtractor)
        {
            if(slkey.at(nLastIndex).compare(str.trimmed(), Qt::CaseInsensitive) == 0)
                totalResult.insert(iterPos.key(), iterPos.value());
        }
    }
    return totalResult;
}

QMap<QString, int> YMOneDepthFilter::KeywordLengthFilter(QMap<QString, int> mapResult)
{
    QMap<QString, int> totalResult;

    int nLength = m_mapParam.value("KeywordLengthFilter").toInt();

    for(QMap<QString, int>::iterator iterPos = mapResult.begin(); iterPos != mapResult.end(); iterPos++)
    {
        QString strkey = iterPos.key();
        QStringList slkey = strkey.split("~!@", QString::SkipEmptyParts);
        int nLastIndex = slkey.length() - 1;

        if(slkey.at(nLastIndex).trimmed().length() >= nLength)
        {
            totalResult.insert(iterPos.key(), iterPos.value());
            //qDebug() << iterPos.key() << " :" << iterPos.value();
        }

    }
    return totalResult;
}

QMap<QString, int> YMOneDepthFilter::KeywordFilter(QMap<QString, int> mapResult)
{
    QMap<QString, int> totalResult;
    QRegExp rx("[\\s,;]");
    QStringList slKeywordFilter = m_mapParam.value("KeywordFilter").split(rx);

    for(QMap<QString, int>::iterator iterPos = mapResult.begin(); iterPos != mapResult.end(); iterPos++)
    {
        QString strkey = iterPos.key();
        QStringList slkey = strkey.split("~!@", QString::SkipEmptyParts);
        int nLastIndex = slkey.length() - 1;
        //qDebug() << "nLastIndex: " << nLastIndex;
        bool bChecked = true;
        foreach(QString str, slKeywordFilter)
        {
            if(slkey.at(nLastIndex).compare(str.trimmed(), Qt::CaseInsensitive) == 0)
            {
                bChecked = false;
                break;
            }
        }
        if(bChecked)
            totalResult.insert(iterPos.key(), iterPos.value());
    }

    return totalResult;
}


QMap<QString, int> YMOneDepthFilter::RankFilterSeparated(QMap<QString, int> mapResult)
{
    QMap<QString, QMap<QString, QMap<int, QString> > > m_mapViewResult;
    int nRank = m_mapParam.value("RankFilterSeparated").toInt();

    //qDebug() << "nRank" <<nRank;

    for(QMap<QString, int>::iterator iterPos = mapResult.begin(); iterPos != mapResult.end(); iterPos++)
    {
        QString strkey = iterPos.key();
        int count = iterPos.value();
        QStringList strlistKey = strkey.split("~!@");
        QString strDate = strlistKey.at(0);
        QString strMorphere = strlistKey.at(1);
        QString strKeyword = strlistKey.at(2);

        if(m_mapViewResult.contains(strDate))
        {
            if(m_mapViewResult.value(strDate).contains(strMorphere))
            {
                m_mapViewResult[(strDate)][(strMorphere)].insertMulti(count, strKeyword);
            }
            else
            {
                QMap<int, QString> qLast;
                qLast.insert(count, strKeyword);
                m_mapViewResult[(strDate)].insert(strMorphere, qLast);
            }
        }
        else
        {
            QMap<int, QString> qLast;
            qLast.insert(count , strKeyword);
            QMap<QString, QMap<int, QString> > qMedium;
            qMedium.insert(strMorphere, qLast);
            m_mapViewResult.insert(strDate, qMedium);
        }
    }
    QMap<QString, int> totalResult;

    for(QMap<QString, QMap<QString, QMap<int, QString> > >::iterator iterPos1 = m_mapViewResult.begin(); iterPos1 != m_mapViewResult.end(); iterPos1++)
    {
        for(QMap<QString, QMap<int, QString> >::iterator iterPos2 = iterPos1.value().begin(); iterPos2 != iterPos1.value().end(); iterPos2++)
        {
            int i = 0;
            QMapIterator<int, QString> iter(m_mapViewResult[iterPos1.key()][iterPos2.key()]);
            iter.toBack();
            while(iter.hasPrevious())
            {
                if(i >= nRank)
                    break;
                iter.previous();
                QString key = iterPos1.key() + "~!@" + iterPos2.key() + "~!@" + iter.value();
                //totalResult.insert(QString(iterPos1.key() + "~!@" + iterPos2.key() + "~!@" + iter.value()), iter.key());
                totalResult.insert(key, iter.key());
                i++;
            }
        }
    }

    return totalResult;
}


/*
 * class YMTwoDepthFilter
 */



QMap<QString, int> YMTwoDepthFilter::Extractor(QMap<QString, int> mapResult)
{
    QMap<QString, int> totalResult;
    QStringList slExtractor;
    QRegExp rx("[\\s,.;]");
    slExtractor = m_mapParam.value("Extractor2").split(rx);

    for(QMap<QString, int>::iterator iterPos = mapResult.begin(); iterPos != mapResult.end(); iterPos++)
    {
        QString strkey = iterPos.key();
        QStringList slkey = strkey.split("~!@", QString::SkipEmptyParts);
        int nLastIndex = slkey.length() - 1;

        foreach(QString str, slExtractor)
        {
            if(slkey.at(nLastIndex).compare(str.trimmed(), Qt::CaseInsensitive) == 0)
                totalResult.insert(iterPos.key(), iterPos.value());
        }
    }
    return totalResult;
}

QMap<QString, int> YMTwoDepthFilter::KeywordLengthFilter(QMap<QString, int> mapResult)
{
    QMap<QString, int> totalResult;

    int nLength = m_mapParam.value("KeywordLengthFilter2").toInt();

    for(QMap<QString, int>::iterator iterPos = mapResult.begin(); iterPos != mapResult.end(); iterPos++)
    {
        QString strkey = iterPos.key();
        QStringList slkey = strkey.split("~!@", QString::SkipEmptyParts);
        int nLastIndex = slkey.length() - 1;

        if(slkey.at(nLastIndex).trimmed().length() >= nLength)
        {
            totalResult.insert(iterPos.key(), iterPos.value());
            //qDebug() << iterPos.key() << " :" << iterPos.value();
        }

    }
    return totalResult;
}

QMap<QString, int> YMTwoDepthFilter::KeywordFilter(QMap<QString, int> mapResult)
{
    QMap<QString, int> totalResult;
    QRegExp rx("[\\s,;]");
    QStringList slKeywordFilter = m_mapParam.value("KeywordFilter2").split(rx);

    for(QMap<QString, int>::iterator iterPos = mapResult.begin(); iterPos != mapResult.end(); iterPos++)
    {
        QString strkey = iterPos.key();
        QStringList slkey = strkey.split("~!@", QString::SkipEmptyParts);
        int nLastIndex = slkey.length() - 1;
        //qDebug() << "nLastIndex: " << nLastIndex;
        bool bChecked = true;
        foreach(QString str, slKeywordFilter)
        {
            if(slkey.at(nLastIndex).compare(str.trimmed(), Qt::CaseInsensitive) == 0)
            {
                bChecked = false;
                break;
            }
        }
        if(bChecked)
            totalResult.insert(iterPos.key(), iterPos.value());
    }

    return totalResult;
}


QMap<QString, int> YMTwoDepthFilter::RankFilterSeparated(QMap<QString, int> mapResult)
{
    QMap<QString, QMap<QString, QMap<int, QString> > > m_mapViewResult;
    int nRank = m_mapParam.value("RankFilterSeparated2").toInt();

    //qDebug() << "nRank" <<nRank;

    for(QMap<QString, int>::iterator iterPos = mapResult.begin(); iterPos != mapResult.end(); iterPos++)
    {
        QString strkey = iterPos.key();
        int count = iterPos.value();
        QStringList strlistKey = strkey.split("~!@");
        QString strDate = strlistKey.at(0);
        QString strMorphere = strlistKey.at(1);
        QString strKeyword = strlistKey.at(2);

        if(m_mapViewResult.contains(strDate))
        {
            if(m_mapViewResult.value(strDate).contains(strMorphere))
            {
                m_mapViewResult[(strDate)][(strMorphere)].insertMulti(count, strKeyword);
            }
            else
            {
                QMap<int, QString> qLast;
                qLast.insert(count, strKeyword);
                m_mapViewResult[(strDate)].insert(strMorphere, qLast);
            }
        }
        else
        {
            QMap<int, QString> qLast;
            qLast.insert(count , strKeyword);
            QMap<QString, QMap<int, QString> > qMedium;
            qMedium.insert(strMorphere, qLast);
            m_mapViewResult.insert(strDate, qMedium);
        }
    }
    QMap<QString, int> totalResult;

    for(QMap<QString, QMap<QString, QMap<int, QString> > >::iterator iterPos1 = m_mapViewResult.begin(); iterPos1 != m_mapViewResult.end(); iterPos1++)
    {
        for(QMap<QString, QMap<int, QString> >::iterator iterPos2 = iterPos1.value().begin(); iterPos2 != iterPos1.value().end(); iterPos2++)
        {
            int i = 0;
            QMapIterator<int, QString> iter(m_mapViewResult[iterPos1.key()][iterPos2.key()]);
            iter.toBack();
            while(iter.hasPrevious())
            {
                if(i >= nRank)
                    break;
                iter.previous();
                QString key = iterPos1.key() + "~!@" + iterPos2.key() + "~!@" + iter.value();
                //totalResult.insert(QString(iterPos1.key() + "~!@" + iterPos2.key() + "~!@" + iter.value()), iter.key());
                totalResult.insert(key, iter.key());
                i++;
            }
        }
    }

    return totalResult;
}


YMTwoDepthFilter::YMTwoDepthFilter()
{

}

YMTwoDepthFilter::~YMTwoDepthFilter()
{

}


QMap<QString, int> YMTwoDepthFilter::Exec(QMap<QString, int> mapResult)
{
    QMap<QString, int> totalResult;
    if(!m_mapParam.value("Extractor2").isEmpty())
    {
        AppendMap(totalResult, Extractor(mapResult));
    }
    if(!m_mapParam.value("KeywordFilter2").isEmpty())
    {
        mapResult = KeywordFilter(mapResult);
        //AppendMap(totalResult, KeywordFilter(mapResult));
    }
    if(!m_mapParam.value("KeywordLengthFilter2").isEmpty())
    {
        mapResult = KeywordLengthFilter(mapResult);
        //totalResult = KeywordLengthFilter(totalResult);
    }
    AppendMap(totalResult, mapResult);
    if(!m_mapParam.value("RankFilterAll2").isEmpty())
    {
        totalResult = RankFilterALL(totalResult);
    }
    else if(!m_mapParam.value("RankFilterSeparated2").isEmpty())
    {
        totalResult = RankFilterSeparated(totalResult);
    }
    if(!m_mapParam.value("CounterFilter2").isEmpty())
    {
        totalResult = RankFilterSeparated(totalResult);
    }
    return totalResult;
}

QMap<QString, int> YMTwoDepthFilter::RankFilterALL(QMap<QString, int> mapResult)
{
    QMap<QString, QMap<int, QString> > Result;
    //QMap<int, QString> Result;

    for(QMap<QString, int>::iterator iterPos = mapResult.begin(); iterPos != mapResult.end(); iterPos++)
    {
        QString strkey = iterPos.key();
        QStringList slkey = strkey.split("~!@", QString::SkipEmptyParts);
        QString key = slkey.at(0).trimmed();
        int nLength = slkey.length();

        for(int i = 1; i < nLength - 2 ; i++)
        {
            key += "~!@";
            key += slkey.at(i).trimmed();
        }


        //qDebug() << slkey.at(0);
        if(Result.contains(key))
        {
            Result[key].insertMulti(iterPos.value(), iterPos.key());
        }
        else
        {
            QMap<int, QString> qLast;
            qLast.insert(iterPos.value(), iterPos.key());
            Result.insert(key, qLast);
        }

    }

    int nRank = m_mapParam.value("RankFilterAll2").toInt();

    qDebug() << "TwoDepth nRank: " << nRank;

    QMap <QString, int> totalResult;
    //qDebug() << "rankfilter:" << nRank;
    for(QMap<QString, QMap<int, QString> >::iterator iterPos = Result.begin(); iterPos != Result.end(); iterPos++)
    {
        QMapIterator <int, QString> iter(Result[iterPos.key()]);
        iter.toBack();
        int i = 0;
        while(iter.hasPrevious())
        {
            if(i >= nRank)
                break;

            iter.previous();
            totalResult.insertMulti(iter.value(), iter.key());

            i++;
        }
    }

    return totalResult;
}



/*
 * class YMPlatformOneFilter
 */



YMPlatformOneFilter::YMPlatformOneFilter()
{

}

YMPlatformOneFilter::~YMPlatformOneFilter()
{

}

QMap<QString, int> YMPlatformOneFilter::Extractor(QMap<QString, int> mapResult)
{
    QMap<QString, int> totalResult;
    QStringList slExtractor;
    QRegExp rx("[\\s,.;]");
    slExtractor = m_mapParam.value("Extractor").split(rx);

    for(QMap<QString, int>::iterator iterPos = mapResult.begin(); iterPos != mapResult.end(); iterPos++)
    {
        QString strkey = iterPos.key();
        QStringList slkey = strkey.split("~!@", QString::SkipEmptyParts);
        int nLastIndex = slkey.length() - 1;

        foreach(QString str, slExtractor)
        {
            if(slkey.at(nLastIndex).compare(str.trimmed(), Qt::CaseInsensitive) == 0)
                totalResult.insert(iterPos.key(), iterPos.value());
        }
    }
    return totalResult;
}

QMap<QString, int> YMPlatformOneFilter::KeywordLengthFilter(QMap<QString, int> mapResult)
{
    QMap<QString, int> totalResult;

    int nLength = m_mapParam.value("KeywordLengthFilter").toInt();

    for(QMap<QString, int>::iterator iterPos = mapResult.begin(); iterPos != mapResult.end(); iterPos++)
    {
        QString strkey = iterPos.key();
        QStringList slkey = strkey.split("~!@", QString::SkipEmptyParts);
        int nLastIndex = slkey.length() - 1;

        if(slkey.at(nLastIndex).trimmed().length() >= nLength)
        {
            totalResult.insert(iterPos.key(), iterPos.value());
            //qDebug() << iterPos.key() << " :" << iterPos.value();
        }

    }
    return totalResult;
}

QMap<QString, int> YMPlatformOneFilter::KeywordFilter(QMap<QString, int> mapResult)
{
    QMap<QString, int> totalResult;
    QRegExp rx("[\\s,;]");
    QStringList slKeywordFilter = m_mapParam.value("KeywordFilter").split(rx);

    for(QMap<QString, int>::iterator iterPos = mapResult.begin(); iterPos != mapResult.end(); iterPos++)
    {
        QString strkey = iterPos.key();
        QStringList slkey = strkey.split("~!@", QString::SkipEmptyParts);
        int nLastIndex = slkey.length() - 1;
        //qDebug() << "nLastIndex: " << nLastIndex;
        bool bChecked = true;
        foreach(QString str, slKeywordFilter)
        {
            if(slkey.at(nLastIndex).compare(str.trimmed(), Qt::CaseInsensitive) == 0)
            {
                bChecked = false;
                break;
            }
        }
        if(bChecked)
            totalResult.insert(iterPos.key(), iterPos.value());
    }

    return totalResult;
}


QMap<QString, int> YMPlatformOneFilter::RankFilterSeparated(QMap<QString, int> mapResult)
{
    QMap<QString, QMap<QString, QMap<int, QString> > > m_mapViewResult;
    int nRank = m_mapParam.value("RankFilterSeparated").toInt();

    //qDebug() << "nRank" <<nRank;

    for(QMap<QString, int>::iterator iterPos = mapResult.begin(); iterPos != mapResult.end(); iterPos++)
    {
        QString strkey = iterPos.key();
        int count = iterPos.value();
        QStringList strlistKey = strkey.split("~!@");
        QString strDate = strlistKey.at(0);
        QString strMorphere = strlistKey.at(1);
        QString strKeyword = strlistKey.at(2);

        if(m_mapViewResult.contains(strDate))
        {
            if(m_mapViewResult.value(strDate).contains(strMorphere))
            {
                m_mapViewResult[(strDate)][(strMorphere)].insertMulti(count, strKeyword);
            }
            else
            {
                QMap<int, QString> qLast;
                qLast.insert(count, strKeyword);
                m_mapViewResult[(strDate)].insert(strMorphere, qLast);
            }
        }
        else
        {
            QMap<int, QString> qLast;
            qLast.insert(count , strKeyword);
            QMap<QString, QMap<int, QString> > qMedium;
            qMedium.insert(strMorphere, qLast);
            m_mapViewResult.insert(strDate, qMedium);
        }
    }
    QMap<QString, int> totalResult;

    for(QMap<QString, QMap<QString, QMap<int, QString> > >::iterator iterPos1 = m_mapViewResult.begin(); iterPos1 != m_mapViewResult.end(); iterPos1++)
    {
        for(QMap<QString, QMap<int, QString> >::iterator iterPos2 = iterPos1.value().begin(); iterPos2 != iterPos1.value().end(); iterPos2++)
        {
            int i = 0;
            QMapIterator<int, QString> iter(m_mapViewResult[iterPos1.key()][iterPos2.key()]);
            iter.toBack();
            while(iter.hasPrevious())
            {
                if(i >= nRank)
                    break;
                iter.previous();
                QString key = iterPos1.key() + "~!@" + iterPos2.key() + "~!@" + iter.value();
                //totalResult.insert(QString(iterPos1.key() + "~!@" + iterPos2.key() + "~!@" + iter.value()), iter.key());
                totalResult.insert(key, iter.key());
                i++;
            }
        }
    }

    return totalResult;
}



QMap<QString, int> YMPlatformOneFilter::RankFilterALL(QMap<QString, int> mapResult)
{
    QMap<QString, QMap<int, QString> > Result;
    //QMap<int, QString> Result;

    for(QMap<QString, int>::iterator iterPos = mapResult.begin(); iterPos != mapResult.end(); iterPos++)
    {
        QString strkey = iterPos.key();
        QStringList slkey = strkey.split("~!@", QString::SkipEmptyParts);
        QString key = slkey.at(0).trimmed();
        int nLength = slkey.length();

        for(int i = 1; i < nLength - 2 ; i++)
        {
            key += "~!@";
            key += slkey.at(i).trimmed();
        }


        //qDebug() << slkey.at(0);
        if(Result.contains(key))
        {
            Result[key].insertMulti(iterPos.value(), iterPos.key());
        }
        else
        {
            QMap<int, QString> qLast;
            qLast.insert(iterPos.value(), iterPos.key());
            Result.insert(key, qLast);
        }

    }

    int nRank = m_mapParam.value("RankFilterAll").toInt();

    qDebug() << "TwoDepth nRank: " << nRank;

    QMap <QString, int> totalResult;
    //qDebug() << "rankfilter:" << nRank;
    for(QMap<QString, QMap<int, QString> >::iterator iterPos = Result.begin(); iterPos != Result.end(); iterPos++)
    {
        QMapIterator <int, QString> iter(Result[iterPos.key()]);
        iter.toBack();
        int i = 0;
        while(iter.hasPrevious())
        {
            if(i >= nRank)
                break;

            iter.previous();
            totalResult.insertMulti(iter.value(), iter.key());

            i++;
        }
    }

    return totalResult;
}

/*
 *
 *  class YMPlatformFilter
 *
 */



QMap<QString, int> YMPlatformFilter::Extractor(QMap<QString, int> mapResult)
{
    QMap<QString, int> totalResult;
    QStringList slExtractor;
    QRegExp rx("[\\s,.;]");
    slExtractor = m_mapParam.value("Extractor2").split(rx);

    for(QMap<QString, int>::iterator iterPos = mapResult.begin(); iterPos != mapResult.end(); iterPos++)
    {
        QString strkey = iterPos.key();
        QStringList slkey = strkey.split("~!@", QString::SkipEmptyParts);
        int nLastIndex = slkey.length() - 1;

        foreach(QString str, slExtractor)
        {
            if(slkey.at(nLastIndex).compare(str.trimmed(), Qt::CaseInsensitive) == 0)
                totalResult.insert(iterPos.key(), iterPos.value());
        }
    }
    return totalResult;
}

QMap<QString, int> YMPlatformFilter::KeywordLengthFilter(QMap<QString, int> mapResult)
{
    QMap<QString, int> totalResult;

    int nLength = m_mapParam.value("KeywordLengthFilter2").toInt();

    for(QMap<QString, int>::iterator iterPos = mapResult.begin(); iterPos != mapResult.end(); iterPos++)
    {
        QString strkey = iterPos.key();
        QStringList slkey = strkey.split("~!@", QString::SkipEmptyParts);
        int nLastIndex = slkey.length() - 1;

        if(slkey.at(nLastIndex).trimmed().length() >= nLength)
        {
            totalResult.insert(iterPos.key(), iterPos.value());
            //qDebug() << iterPos.key() << " :" << iterPos.value();
        }

    }
    return totalResult;
}

QMap<QString, int> YMPlatformFilter::KeywordFilter(QMap<QString, int> mapResult)
{
    QMap<QString, int> totalResult;
    QRegExp rx("[\\s,;]");
    QStringList slKeywordFilter = m_mapParam.value("KeywordFilter2").split(rx);

    for(QMap<QString, int>::iterator iterPos = mapResult.begin(); iterPos != mapResult.end(); iterPos++)
    {
        QString strkey = iterPos.key();
        QStringList slkey = strkey.split("~!@", QString::SkipEmptyParts);
        int nLastIndex = slkey.length() - 1;
        //qDebug() << "nLastIndex: " << nLastIndex;
        bool bChecked = true;
        foreach(QString str, slKeywordFilter)
        {
            if(slkey.at(nLastIndex).compare(str.trimmed(), Qt::CaseInsensitive) == 0)
            {
                bChecked = false;
                break;
            }
        }
        if(bChecked)
            totalResult.insert(iterPos.key(), iterPos.value());
    }

    return totalResult;
}


QMap<QString, int> YMPlatformFilter::RankFilterSeparated(QMap<QString, int> mapResult)
{
    QMap<QString, QMap<QString, QMap<int, QString> > > m_mapViewResult;
    int nRank = m_mapParam.value("RankFilterSeparated2").toInt();

    //qDebug() << "nRank" <<nRank;

    for(QMap<QString, int>::iterator iterPos = mapResult.begin(); iterPos != mapResult.end(); iterPos++)
    {
        QString strkey = iterPos.key();
        int count = iterPos.value();
        QStringList strlistKey = strkey.split("~!@");
        QString strDate = strlistKey.at(0);
        QString strMorphere = strlistKey.at(1);
        QString strKeyword = strlistKey.at(2);

        if(m_mapViewResult.contains(strDate))
        {
            if(m_mapViewResult.value(strDate).contains(strMorphere))
            {
                m_mapViewResult[(strDate)][(strMorphere)].insertMulti(count, strKeyword);
            }
            else
            {
                QMap<int, QString> qLast;
                qLast.insert(count, strKeyword);
                m_mapViewResult[(strDate)].insert(strMorphere, qLast);
            }
        }
        else
        {
            QMap<int, QString> qLast;
            qLast.insert(count , strKeyword);
            QMap<QString, QMap<int, QString> > qMedium;
            qMedium.insert(strMorphere, qLast);
            m_mapViewResult.insert(strDate, qMedium);
        }
    }
    QMap<QString, int> totalResult;

    for(QMap<QString, QMap<QString, QMap<int, QString> > >::iterator iterPos1 = m_mapViewResult.begin(); iterPos1 != m_mapViewResult.end(); iterPos1++)
    {
        for(QMap<QString, QMap<int, QString> >::iterator iterPos2 = iterPos1.value().begin(); iterPos2 != iterPos1.value().end(); iterPos2++)
        {
            int i = 0;
            QMapIterator<int, QString> iter(m_mapViewResult[iterPos1.key()][iterPos2.key()]);
            iter.toBack();
            while(iter.hasPrevious())
            {
                if(i >= nRank)
                    break;
                iter.previous();
                QString key = iterPos1.key() + "~!@" + iterPos2.key() + "~!@" + iter.value();
                //totalResult.insert(QString(iterPos1.key() + "~!@" + iterPos2.key() + "~!@" + iter.value()), iter.key());
                totalResult.insert(key, iter.key());
                i++;
            }
        }
    }

    return totalResult;
}


YMPlatformFilter::YMPlatformFilter()
{

}

YMPlatformFilter::~YMPlatformFilter()
{

}


QMap<QString, int> YMPlatformFilter::Exec(QMap<QString, int> mapResult)
{
    QMap<QString, int> totalResult;
    if(!m_mapParam.value("Extractor2").isEmpty())
    {
        AppendMap(totalResult, Extractor(mapResult));
    }
    if(!m_mapParam.value("KeywordFilter2").isEmpty())
    {
        mapResult = KeywordFilter(mapResult);
        //AppendMap(totalResult, KeywordFilter(mapResult));
    }
    if(!m_mapParam.value("KeywordLengthFilter2").isEmpty())
    {
        mapResult = KeywordLengthFilter(mapResult);
        //totalResult = KeywordLengthFilter(totalResult);
    }
    AppendMap(totalResult, mapResult);
    if(!m_mapParam.value("RankFilterAll2").isEmpty())
    {
        totalResult = RankFilterALL(totalResult);
    }
    else if(!m_mapParam.value("RankFilterSeparated2").isEmpty())
    {
        totalResult = RankFilterSeparated(totalResult);
    }
    if(!m_mapParam.value("CounterFilter2").isEmpty())
    {
        totalResult = RankFilterSeparated(totalResult);
    }
    return totalResult;
}

QMap<QString, int> YMPlatformFilter::RankFilterALL(QMap<QString, int> mapResult)
{
    QMap<QString, QMap<int, QString> > Result;
    //QMap<int, QString> Result;

    for(QMap<QString, int>::iterator iterPos = mapResult.begin(); iterPos != mapResult.end(); iterPos++)
    {
        QString strkey = iterPos.key();
        QStringList slkey = strkey.split("~!@", QString::SkipEmptyParts);
        QString key = slkey.at(0).trimmed();
        int nLength = slkey.length();

        for(int i = 1; i < nLength - 2 ; i++)
        {
            key += "~!@";
            key += slkey.at(i).trimmed();
        }


        //qDebug() << slkey.at(0);
        if(Result.contains(key))
        {
            Result[key].insertMulti(iterPos.value(), iterPos.key());
        }
        else
        {
            QMap<int, QString> qLast;
            qLast.insert(iterPos.value(), iterPos.key());
            Result.insert(key, qLast);
        }

    }

    int nRank = m_mapParam.value("RankFilterAll2").toInt();

    qDebug() << "TwoDepth nRank: " << nRank;

    QMap <QString, int> totalResult;
    //qDebug() << "rankfilter:" << nRank;
    for(QMap<QString, QMap<int, QString> >::iterator iterPos = Result.begin(); iterPos != Result.end(); iterPos++)
    {
        QMapIterator <int, QString> iter(Result[iterPos.key()]);
        iter.toBack();
        int i = 0;
        while(iter.hasPrevious())
        {
            if(i >= nRank)
                break;

            iter.previous();
            totalResult.insertMulti(iter.value(), iter.key());

            i++;
        }
    }

    return totalResult;
}


