天天看點

C#實作的知識搜尋工具 - KBCruiser

平時通過搜尋引擎去搜答案,總是覺得有些地方用起來不順手,總結一下有這麼幾點

  1. 搜尋引擎範圍太寬,有的時候隻想在官方文檔和論壇裡面找答案
  2. 并非所有的論壇内容和部落格内容都被搜尋引擎索引
  3. 點開的連結發現不是想要的内容,需要關掉目前視窗回到搜尋頁面繼續找
  4. 點開連結沒有關鍵字高亮

于是自己動手寫了個知識搜尋的用戶端小工具,原理就是向不同的官方網站相應的搜尋引擎發送請求,分析顯示傳回結果,同時在浏覽器中實作關鍵字高亮。目前包含的功能主要有以下幾點,

  1. 知識搜尋
    • 論壇搜尋 - Msdn, Asp.net, IIS.net, StackOverflow, Csdn
    • 部落格搜尋 - Msdn blog, AspNetBlog, IISBlog, Blogspot, WordPress, CsdnBlog
    • 知識庫搜尋 - Msdn library, Technet, MSKB, Wikipedia, RFC, W3C, IEEE, Ecma
    • 引擎搜尋 - Bing, Google, Yahoo, Baidu
    • 代碼搜尋 - Code project, Sourceforge, Codeplex, Github
    • 下載下傳搜尋 - Ms download, Vs gallery, IIS Community, Softpedia, CNET
  2. 內建浏覽器
  3. 相關查詢
  4. 關鍵字高亮

安裝包和代碼下載下傳放在了codeplex上,

https://kbcruiser.codeplex.com/

以下是效果圖

C#實作的知識搜尋工具 - KBCruiser

實作中幾個有趣的地方,

1. 下載下傳網頁通過不同網站對應的正規表達式把标題和相應連結摳出來,每個網站對應的表達式都放在一個叫Profiles.xml的配置檔案中,例如msdn的

<SearcherConfig>
            <Default>true</Default>
            <Name>MSDNForum</Name>
            <ResultPageNumber>1</ResultPageNumber>
            <RequestTemplate>http://social.msdn.microsoft.com/Search/en-US/?query={0}&refinement=112&ac=8</RequestTemplate>
            <SearchTargetSeparator>%20</SearchTargetSeparator>
            <StartPageIndex>0</StartPageIndex>
            <DescriptionMatchPattern />
            <TitleStartIndex>9</TitleStartIndex>
            <UrlStartIndex>7</UrlStartIndex>
            <BaseUrl />
            <ReferenceMatchPattern>\{"title":[\s\S]*?"\}</ReferenceMatchPattern>
            <TitleMatchPattern>"title":"[\s\S]*?"</TitleMatchPattern>
            <UrlMatchPattern>"url":"[\s\S]*?"</UrlMatchPattern>
          </SearcherConfig>           

2. 關鍵字高亮的實作也是通過正規表達式簡單的替換一下顯示style

if (triggerHighlight)
            {
                IHTMLDocument2 doc2 = wbContent.Document.DomDocument as IHTMLDocument2;
                string result = doc2.body.outerHTML;
                string substitution = null;
                string pattern = null;
                foreach (string key in keywords)
                {
                    pattern = string.Format(@"(>[^<]*?)(\b{0}\b)",key);
                    substitution = "$1<span style='background-color: rgb(255, 255, 0);'>$2</span>";
                    result = Regex.Replace(result, pattern, substitution, RegexOptions.IgnoreCase);
                }
                doc2.body.innerHTML = result;
                triggerHighlight = false;
            }           

3. 搜尋提示直接用google或bing的,但是這個功能不穩定,因為google或bing的搜尋提示連結經常變化

繼續閱讀