c# 寫了個正向匹配的分詞演算法

來源:互聯網
上載者:User

c# 寫了個正向匹配的分詞演算法,思路很簡單,每次從字串中取一個詞,至於詞的長度,能夠自己配置的,比如本文中的,

後偏差的意思是:當取到一個詞時不立即社區該串字元,而是順延Offset個字元,如該詞庫中的:共和,共和國 就是如此。

前偏差的意思是:同上當取到一個詞時,並不重新從新位置開始,而是從指定偏差值的位置開始,如該詞庫中的:中華,華人。

明天繼續完善,歡迎高手指定,謝謝! 

using System;using System.Collections.Generic;using System.Web;using System.Web.UI;using System.Web.UI.WebControls;using System.Collections;public partial class _Default : System.Web.UI.Page{    public ArrayList al = new ArrayList();    protected void Page_Load(object sender, EventArgs e)    {        string str = "中華人民共和國";        al.Add("人民");        al.Add("華人");        al.Add("中華");        al.Add("共和");        al.Add("共和國");        char[] chs = str.ToCharArray();        Response.Write(SplitString(str));        return;    }    int MinSize = 2;//最小詞長     int MaxSize = 4;//最大詞長    int Offset = 1;//後偏差
    int LOffset = 1;//前偏差
public string SplitString(string str) { string keys = string.Empty; char[] chs = str.ToCharArray(); int chsLen = chs.Length; string tempKey = string.Empty; for (int j = 0; j < chsLen; j++) { int CurrChLen = 0; int CurrOffset = 0; for (int i = j; i < chsLen; i++) { tempKey += chs[i].ToString(); CurrChLen++; if (CurrChLen < MinSize) { continue; } if (CurrChLen > MaxSize) { break; } if (al.Contains(tempKey)) { keys += tempKey + ","; if (CurrOffset < Offset) { j = i - 1; CurrOffset++; continue; } break; } if (CurrOffset > 0) { break; } } tempKey = string.Empty; } return keys; }}

 

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.