Exemple #1
0
 private IEnumerable <KeyWord> search(IBox box, KeyWord kw, KeyWord condition, bool asWord)
 {
     if (kw is KeyWordE)
     {
         if (condition == null)
         {
             return(Index2KeyWord <KeyWordE> (box.Select <object> ("from E where K==?", kw.KWord)));
         }
         else
         {
             return(Index2KeyWord <KeyWordE> (box.Select <object> ("from E where K==? &  I==?",
                                                                   kw.KWord, condition.ID)));
         }
     }
     else if (condition == null)
     {
         return(Index2KeyWord <KeyWordN> (box.Select <object> ("from N where K==?", kw.KWord)));
     }
     else if (asWord)
     {
         return(Index2KeyWord <KeyWordN> (box.Select <object> ("from N where K==? &  I==?",
                                                               kw.KWord, condition.ID)));
     }
     else
     {
         return(Index2KeyWord <KeyWordN> (box.Select <object> ("from N where K==? & I==? & P==?",
                                                               kw.KWord, condition.ID, (condition.Position + 1))
                                          ));
     }
 }
Exemple #2
0
        static void insertToBox(IBox box, KeyWord kw, bool isRemove)
        {
            Binder binder;

            if (kw is KeyWordE)
            {
                binder = box["/E", kw.getKeyWord(), kw.getID(), kw.getPosition()];
            }
            else
            {
                binder = box["/N", kw.getKeyWord(), kw.getID(), kw.getPosition()];
            }
            if (isRemove)
            {
                binder.Delete();
            }
            else
            {
                if (binder.TableName == "/E")
                {
                    binder.Insert((KeyWordE)kw);
                }
                else
                {
                    binder.Insert((KeyWordN)kw);
                }
            }
        }
Exemple #3
0
        static void insertToBox(IBox box, KeyWord kw, HashSet <String> insertedWords, bool isRemove)
        {
            Binder binder;

            if (kw is KeyWordE)
            {
                if (insertedWords.Contains(kw.KWord.ToString()))
                {
                    return;
                }
                insertedWords.Add(kw.KWord.ToString());
                binder = box ["/E", kw.KWord, kw.ID, kw.Position];
            }
            else
            {
                binder = box ["/N", kw.KWord, kw.ID, kw.Position];
            }
            if (isRemove)
            {
                binder.Delete();
            }
            else
            {
                if (binder.TableName == "/E")
                {
                    binder.Insert((KeyWordE)kw);
                }
                else
                {
                    binder.Insert((KeyWordN)kw);
                }
            }
        }
Exemple #4
0
        public IEnumerable <KeyWord> search(IBox box, String str)
        {
            char[]         cs  = sUtil.clear(str);
            List <KeyWord> map = util.fromString(-1, cs, false);

            sUtil.correctInput(map);

            if (map.Count > KeyWord.MAX_WORD_LENGTH || map.Count == 0)
            {
                return(new List <KeyWord> ());
            }

            List <KeyWord> kws = new List <KeyWord> ();

            for (int i = 0; i < map.Count; i++)
            {
                KeyWord kw = map [i];
                if (kw is KeyWordE)
                {
                    String s = kw.KWord.ToString();
                    if ((s.Length > 2) && (!sUtil.mvends.Contains(s)))
                    {
                        kws.Add(kw);
                        map [i] = null;
                    }
                }
                else
                {
                    KeyWordN kwn = (KeyWordN)kw;
                    if (kwn.size() >= 2)
                    {
                        kws.Add(kw);
                        map [i] = null;
                    }
                    else if (kws.Count > 0)
                    {
                        KeyWord p = kws [kws.Count - 1];
                        if (p is KeyWordN)
                        {
                            if (kwn.Position == (p.Position + ((KeyWordN)p).size()))
                            {
                                kws.Add(kw);
                                map [i] = null;
                            }
                        }
                    }
                }
            }
            for (int i = 0; i < map.Count; i++)
            {
                KeyWord kw = map [i];
                if (kw != null)
                {
                    kws.Add(kw);
                }
            }
            MaxID maxId = new MaxID(this.maxSearchTime);

            return(search(box, kws.ToArray(), maxId));
        }
Exemple #5
0
 private static IEnumerable <KeyWord> lessMatch(IBox box, KeyWord kw)
 {
     if (kw is KeyWordE)
     {
         return(new Index2KeyWordIterable <KeyWordE> (box.Select <object> ("from /E where K<=? limit 0, 50", kw.KWord), null, null, null, true, new MaxID(long.MaxValue)));
     }
     else
     {
         return(new Index2KeyWordIterable <KeyWordN> (box.Select <object> ("from /N where K<=? limit 0, 50", kw.KWord), null, null, null, true, new MaxID(long.MaxValue)));
     }
 }
Exemple #6
0
 private static IEnumerable <KeyWord> lessMatch(IBox box, KeyWord kw)
 {
     if (kw is KeyWordE)
     {
         return(box.Select <KeyWordE>("from /E where K<=? limit 0, 50", kw.getKeyWord()));
     }
     else
     {
         return(box.Select <KeyWordN>("from /N where K<=? limit 0, 50", kw.getKeyWord()));
     }
 }
Exemple #7
0
 private void setLinkEnd(ArrayList <KeyWord> kws)
 {
     if (kws.size() > 1)
     {
         KeyWord last = kws.get(kws.size() - 1);
         if (last.isLinked)
         {
             last.isLinkedEnd = true;
         }
     }
 }
Exemple #8
0
 // Base
 private IEnumerable <KeyWord> search(IBox box, KeyWord[] kws)
 {
     if (kws.Length == 1)
     {
         return(search(box, kws [0], (KeyWord)null, false));
     }
     KeyWord[] condition = new KeyWord[kws.Length - 1];
     Array.Copy(kws, 0, condition, 0, condition.Length);
     return(search(box, kws [kws.Length - 1],
                   search(box, condition),
                   kws [kws.Length - 1].Position
                   != (kws [kws.Length - 2].Position + 1)));
 }
Exemple #9
0
        private IEnumerable <KeyWord> search(IBox box, KeyWord nw,
                                             IEnumerable <KeyWord> condition, MaxID maxId)
        {
            IEnumerator <KeyWord> cd = condition.GetEnumerator();

            IEnumerator <KeyWord> r1 = null;

            KeyWord r1_con = null;
            long    r1_id  = -1;

            return(new Iterable <KeyWord>()
            {
                iterator = new EngineIterator <KeyWord>()
                {
                    hasNext = () =>
                    {
                        if (r1 != null && r1.MoveNext())
                        {
                            return true;
                        }
                        while (cd.MoveNext())
                        {
                            r1_con = cd.Current;

                            if (r1_id == r1_con.getID())
                            {
                                continue;
                            }
                            if (!nw.isLinked)
                            {
                                r1_id = r1_con.getID();
                            }

                            r1 = search(box, nw, r1_con, maxId).GetEnumerator();
                            if (r1.MoveNext())
                            {
                                return true;
                            }
                        }
                        return false;
                    },

                    next = () =>
                    {
                        KeyWord k = r1.Current;
                        k.previous = r1_con;
                        return k;
                    }
                }
            });
        }
Exemple #10
0
 private static IEnumerable <KeyWord> search(IBox box, KeyWord kw, KeyWord con, bool asWord, MaxID maxId)
 {
     if (kw is KeyWordE)
     {
         asWord = true;
         return(new Index2KeyWordIterable <KeyWordE> (
                    box.Select <Object> ("from /E where K==? & I<=?",
                                         kw.KWord, maxId.id), box, kw, con, asWord, maxId));
     }
     else
     {
         if (con is KeyWordE)
         {
             asWord = true;
         }
         if (con == null || asWord)
         {
             asWord = true;
             return(new Index2KeyWordIterable <KeyWordN> (
                        box.Select <Object> ("from /N where K==? & I<=?", kw.KWord, maxId.id), box, kw, con, asWord, maxId));
         }
         else
         {
             Object[] os = (Object[])box ["/N", kw.KWord,
                                          con.ID, (con.Position + ((KeyWordN)con).size())]
                           .Select <Object> ();
             if (os != null)
             {
                 KeyWordN cache = new KeyWordN();
                 cache.KWord = os [0];
                 cache.I     = (long)os [1];
                 cache.P     = (int)os [2];
                 List <KeyWord> r = new List <KeyWord> (1);
                 r.Add(cache);
                 return(r);
             }
             else
             {
                 return(emptySearch);
             }
         }
     }
 }
Exemple #11
0
        private IEnumerable <KeyWord> search(IBox box, KeyWord[] kws, MaxID maxId)
        {
            if (kws.Length == 1)
            {
                return(search(box, kws [0], (KeyWord)null, false, maxId));
            }
            bool    asWord = true;
            KeyWord kwa    = kws [kws.Length - 2];
            KeyWord kwb    = kws [kws.Length - 1];

            if ((kwa is KeyWordN) && (kwb is KeyWordN))
            {
                asWord = kwb.Position != (kwa.Position + ((KeyWordN)kwa).size());
            }

            KeyWord[] condition = new KeyWord[kws.Length - 1];
            Array.Copy(kws, 0, condition, 0, condition.Length);
            return(search(box, kws [kws.Length - 1],
                          search(box, condition, maxId), asWord, maxId));
        }
Exemple #12
0
        public String getDesc(String str, KeyWord kw, int length)
        {
            ArrayList <KeyWord> list = new ArrayList <KeyWord>();

            while (kw != null)
            {
                list.add(kw);
                kw = kw.previous;
            }

            KeyWord[] ps = list.toArray();
            Array.Sort(ps, (KeyWord o1, KeyWord o2) =>
            {
                return(o1.getPosition() - o2.getPosition());
            });


            int           start = -1;
            int           end   = -1;
            StringBuilder sb    = new StringBuilder();

            for (int i = 0; i < ps.Length; i++)
            {
                int len = ps[i] is KeyWordE ? ps[i].getKeyWord()
                          .ToString().length() : ((KeyWordN)ps[i]).size();
                if ((ps[i].getPosition() + len) <= end)
                {
                    continue;
                }
                start = ps[i].getPosition();
                end   = ps[i].getPosition() + length;
                if (end > str.length())
                {
                    end = str.length();
                }
                sb.append(str.substring(start, end))
                .append("...");
            }
            return(sb.ToString());
        }
Exemple #13
0
        public String getDesc(String str, KeyWord kw, int length)
        {
            List <KeyWord> list = new List <KeyWord> ();

            while (kw != null)
            {
                list.Add(kw);
                kw = kw.previous;
            }
            list.Sort(
                (o1, o2) => {
                return(o1.Position - o2.Position);
            }
                );
            KeyWord[] ps = list.ToArray();

            int           start = -1;
            int           end   = -1;
            StringBuilder sb    = new StringBuilder();

            for (int i = 0; i < ps.Length; i++)
            {
                int len = ps [i] is KeyWordE ? ps [i].KWord
                          .ToString().Length : ((KeyWordN)ps [i]).size();
                if ((ps [i].Position + len) <= end)
                {
                    continue;
                }
                start = ps [i].Position;
                end   = ps [i].Position + length;
                if (end > str.Length)
                {
                    end = str.Length;
                }
                sb.Append(str.Substring(start, end - start))
                .Append("...");
            }
            return(sb.ToString());
        }
Exemple #14
0
        private IEnumerable <KeyWord> search(IBox box, KeyWord nw,
                                             IEnumerable <KeyWord> condition, bool isWord)
        {
            long r1_id = -1;

            foreach (KeyWord r1_con in condition)
            {
                if (isWord)
                {
                    if (r1_id == r1_con.ID)
                    {
                        continue;
                    }
                }
                r1_id = r1_con.ID;
                foreach (KeyWord k in search(box, nw, r1_con, isWord))
                {
                    k.previous = r1_con;
                    yield return(k);
                }
            }
        }
Exemple #15
0
        public void correctInput(List <KeyWord> kws)
        {
            for (int i = 0; i < kws.Count; i++)
            {
                KeyWord kw = (KeyWord)kws [i];
                if (kw is KeyWordE)
                {
                    String str = kw.KWord.ToString();
                    if (correctKW.TryGetValue(str, out str))
                    {
                        if (isWord(str [0]))
                        {
                            kw.KWord = str;
                        }
                        else
                        {
                            KeyWordN kwn = new KeyWordN();
                            kwn.I = kw.I;
                            kwn.P = kw.P + 1;
                            switch (str.Length)
                            {
                            case 1:
                                kwn.longKeyWord(str [0], (char)0, (char)0);
                                break;

                            case 2:
                                kwn.longKeyWord(str [0], str [1], (char)0);
                                break;

                            default:
                                continue;
                            }
                            kws [i] = kwn;
                        }
                    }
                }
            }
        }
Exemple #16
0
            internal Index2KeyWordIterable(IEnumerable <Object> findex,
                                           IBox box, KeyWord kw, KeyWord con, bool asWord, MaxID maxId)
            {
                this.index    = findex.GetEnumerator();
                this.iterator = new KWIterator();

                long    currentMaxId = maxId.id;
                KeyWord cache        = null;

                this.iterator.moveNext = () => {
                    if (maxId.id == -1)
                    {
                        return(false);
                    }
                    if (con != null)
                    {
                        if (con.I != maxId.id)
                        {
                            return(false);
                        }
                    }
                    if (currentMaxId > (maxId.id + 1) && currentMaxId != long.MaxValue)
                    {
                        currentMaxId = maxId.id;

                        IEnumerable <KeyWord> tmp = search(box, kw, con, asWord, maxId);
                        if (tmp is IIndex2KeyWordIterable)
                        {
                            index = ((IIndex2KeyWordIterable)tmp).GetIndex();
                        }
                    }
                    if (index.MoveNext())
                    {
                        if (--maxId.maxTime < 0)
                        {
                            maxId.id = -1;
                            return(false);
                        }

                        Object[] os = (Object[])index.Current;

                        long osid = (long)os [1];
                        maxId.id     = osid;
                        currentMaxId = maxId.id;

                        if (con != null)
                        {
                            if (con.I != maxId.id)
                            {
                                return(false);
                            }
                        }

                        cache       = typeof(T) == typeof(KeyWordE) ? (KeyWord) new KeyWordE() : new KeyWordN();
                        cache.KWord = os [0];
                        cache.I     = (long)os [1];
                        cache.P     = (int)os [2];

                        return(true);
                    }
                    maxId.id = -1;
                    return(false);
                };

                this.iterator.current = () => {
                    return(cache);
                };
            }
Exemple #17
0
        private static IEnumerable <KeyWord> search(IBox box,
                                                    KeyWord kw, KeyWord con, MaxID maxId)
        {
            String ql = kw is KeyWordE
                ? "from /E where K==? & I<=?"
                    : "from /N where K==? & I<=?";


            int linkPos = kw.isLinked ? (con.getPosition() + con.size()
                                         + (kw is KeyWordE ? 1 : 0)) : -1;

            long    currentMaxId       = long.MaxValue;
            KeyWord cache              = null;
            IEnumerator <KeyWord> iter = null;
            bool isLinkEndMet          = false;

            return(new Iterable <KeyWord>()
            {
                iterator = new EngineIterator <KeyWord>()
                {
                    hasNext = () =>
                    {
                        if (maxId.id == -1)
                        {
                            return false;
                        }

                        if (currentMaxId > (maxId.id + 1))
                        {
                            currentMaxId = maxId.id;
                            iter = kw is KeyWordE ?
                                   (IEnumerator <KeyWord>)box.Select <KeyWordE>(ql, kw.getKeyWord(), maxId.id).GetEnumerator() :
                                   box.Select <KeyWordN>(ql, kw.getKeyWord(), maxId.id).GetEnumerator();
                        }

                        while (iter.MoveNext())
                        {
                            cache = iter.Current;

                            maxId.id = cache.getID();
                            currentMaxId = maxId.id;
                            if (con != null && con.I != maxId.id)
                            {
                                return false;
                            }

                            if (isLinkEndMet)
                            {
                                continue;
                            }

                            if (linkPos == -1)
                            {
                                return true;
                            }

                            int cpos = cache.getPosition();
                            if (cpos > linkPos)
                            {
                                continue;
                            }
                            if (cpos == linkPos)
                            {
                                if (kw.isLinkedEnd)
                                {
                                    isLinkEndMet = true;
                                }
                                return true;
                            }
                            return false;
                        }

                        maxId.id = -1;
                        return false;
                    },

                    next = () =>
                    {
                        return cache;
                    }
                }
            });
        }
Exemple #18
0
 public String getDesc(String str, KeyWord kw, int length)
 {
     return(sUtil.getDesc(str, kw, length));
 }
Exemple #19
0
 public void Config(DatabaseConfig config)
 {
     KeyWord.config(config);
 }