//
//  AWFastCheck.hpp
//  aho-corasick
//
//  Created by chenguanglv on 15/12/20.
//  Copyright © 2015年 ibrain. All rights reserved.
//

#ifndef AWFastCheck_hpp
#define AWFastCheck_hpp

#include <vector>
#include <string>
#include <memory>

namespace ac
{
    template<typename T,typename D>
    class Trie;
}

namespace AIWriter
{
    class AWFastCheck
    {
    public:
        struct CheckItem
        {
            std::string wrong;
            std::string right;
            std::string wrongType;
            CheckItem(const char *w,const char *r,const char *t)
            : wrong(w) , right(r),wrongType(t) {}
        };
        
        struct CheckResult : public CheckItem
        {
            size_t begin;
            size_t end;
            CheckResult(const char *w,const char *r,const char *t,size_t b,size_t e)
            : CheckItem(w,r,t), begin(b),end(e) {}
        };
        
        typedef std::vector<CheckItem> CheckItemVector;
        typedef std::vector<CheckResult> CheckResultVector;
        typedef std::unique_ptr<ac::Trie<char,CheckItem>> TriePtrS;
        
    public:
        static AWFastCheck& getInstance()
        {
            static AWFastCheck s_instance;
            return s_instance;
        }
        ~AWFastCheck();
        
        void reset();
        bool addDictionary(const char *dicfile);
        bool addCheckItem(const char *wrong,const char *right,const char *wrongType);
        void buildChecker();
        void checkText(const char *text,CheckResultVector& result);
        void checkTextFile(const char *textfile,CheckResultVector& result);
        
    private:
        AWFastCheck();
        
        bool parseDicLine(std::string& line,std::string& wrong,std::string& right,std::string& wrongType);
        void checkTextContent(const char *text,CheckResultVector& result);
    private:
        CheckItemVector _checkItems;
        TriePtrS _trie;
        bool _hasBuild;
    };
}

#define GetAWCheck() AIWriter::AWFastCheck::getInstance()

#endif /* AWFastCheck_hpp */
