Index

程式作品

C 語言

Java

C#

JavaScript

常用函數

文字處理

遊戲程式

衛星定位

系統程式

資料結構

網路程式

自然語言

人工智慧

機率統計

資訊安全

等待完成

訊息

相關網站

參考文獻

最新修改

簡體版

English

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace ccc
{
    class ObjList : List<Object>
    {
        public override String ToString() {
            return this.toStr(";");
        }
    }

    class Substr
    {
        public String str;
        public int pos, len;
        public override string ToString()
        {
            int start = Math.Max(pos - 3, 0), end = Math.Min(pos + len + 3, str.Length);
            return String.Format("substr({0}:{1})", pos, str.Substring(start, end-start))
                         .Replace('\r',' ').Replace('\n', ' ');
        }
    }

    class Dict<OBJ> : Dictionary<String, OBJ> 
    {
        public Dict<OBJ> indexText(String text, Index index)
        {
            Dict<OBJ> dict = this;
            for (int ti = 0; ti < text.Length; )
            {
                char ch = Char.ToLower(text[ti]);
                String token = "" + text[ti];
                if ('a' <= ch && ch <= 'z')
                {
                    token = REGEX.matchAt(text, ti, @"[A-Za-z]+");
                }
                else if (Char.IsDigit(ch))
                {
                    token = REGEX.matchAt(text, ti, @"[0-9]+");
                }
                else if (CharSet.isChinese(ch))
                {
                    String ctext = REGEX.matchAt(text, ti, @"[^\x4E00-\x9FFF]+");
                    for (int len = 7; len >= 1; len--)
                    {
                        if (len >= ctext.Length)
                            continue;
                        token = ctext.Substring(0, len).ToLower();
                        if (dict.ContainsKey(token))
                            break;
                    }
                }
                String ltoken = token.ToLower();
                if (token.Length >= 1 && dict.ContainsKey(ltoken))
                {
                    Substr substr = new Substr { str = text, pos = ti, len = token.Length };
                    index.add(ltoken, substr);
                }
                ti += token.Length;
            }
            return this;
        }
    }

    class Index : Dict<ObjList>
    {
        public Index indexAll(IEnumerable<Object> c, String spliters)
        {
            foreach (Object o in c)
                indexObj(o, spliters);
            return this;
        }

        public Index indexObj(Object o, String spliters)
        {
            String[] list = o.ToString().Split(spliters.ToCharArray());
            foreach (String token in list)
            {
                String key = token.Trim();
                if (key.Length > 0)
                    add(key, o);
            }
            return this;
        }

        public Index add(String key, Object o)
        {
            ObjList hits = null;
            if (this.ContainsKey(key))
                hits = this[key];
            else
                hits = new ObjList();

            if (!hits.Contains(o))
                hits.Add(o);
            this[key] = hits;
            return this;
        }
    }
}

Facebook

Unless otherwise stated, the content of this page is licensed under Creative Commons Attribution-NonCommercial-ShareAlike 3.0 License