昨天有幸拜读了洞庭散人的<基于朴素贝叶斯分类器的文本分类算法>,我正在学习这个,我从内心感谢洞庭散人的分享!随即我把它移植到了c#平台上。
该程序用到了Lucene.Net,用到了基于词典的ICTCLAS中文分词1.0.
ICTCLAS中文分词for Lucene.Net接口代码(实现Analyzer):
Code
1using System;
2using System.Collections.Generic;
3using System.Text;
4using System.IO;
5
6using Lucene.Net.Analysis;
7using Lucene.Net.Analysis.Standard;
8
9namespace AspxOn.Search.FenLei
10{
11
12 /**//// <summary>
13 /// ICTCLAS分词组件for Lucene.net接口
14 /// </summary>
15 public class ICTCLASAnalyzer : Analyzer
16 {
17 //定义要过滤的词
18 public static readonly System.String[] CHINESE_ENGLISH_STOP_WORDS = new string[428];
19 public string NoisePath = Environment.CurrentDirectory + "\\data\\stopwords.txt";
20
21 public ICTCLASAnalyzer()
22 {
23 StreamReader reader = new StreamReader(NoisePath, System.Text.Encoding.Default);
24 string noise = reader.ReadLine();
25 int i = 0;
26
27 while (!string.IsNullOrEmpty(noise))
28 {
29 CHINESE_ENGLISH_STOP_WORDS[i] = noise;
30 noise = reader.ReadLine();
31 i++;
32 }
33
34 }
35
36 /**//**//**//// Constructs a {@link StandardTokenizer} filtered by a {@link
37 /// StandardFilter}, a {@link LowerCaseFilter} and a {@link StopFilter}.
38 ///
39 public override TokenStream TokenStream(System.String fieldName, System.IO.TextReader reader)
40 {
41 TokenStream result = new ICTCLASTokenizer(reader);
42 result = new StandardFilter(result);
43 result = new LowerCaseFilter(result);
44 result = new StopFilter(result, CHINESE_ENGLISH_STOP_WORDS);
45 return result;
46 }
47
48
49 }
50}
1using System;
2using System.Collections.Generic;
3using System.Text;
4using System.IO;
5
6using Lucene.Net.Analysis;
7using Lucene.Net.Analysis.Standard;
8
9namespace AspxOn.Search.FenLei
10{
11
12 /**//// <summary>
13 /// ICTCLAS分词组件for Lucene.net接口
14 /// </summary>
15 public class ICTCLASAnalyzer : Analyzer
16 {
17 //定义要过滤的词
18 public static readonly System.String[] CHINESE_ENGLISH_STOP_WORDS = new string[428];
19 public string NoisePath = Environment.CurrentDirectory + "\\data\\stopwords.txt";
20
21 public ICTCLASAnalyzer()
22 {
23 StreamReader reader = new StreamReader(NoisePath, System.Text.Encoding.Default);
24 string noise = reader.ReadLine();
25 int i = 0;
26
27 while (!string.IsNullOrEmpty(noise))
28 {
29 CHINESE_ENGLISH_STOP_WORDS[i] = noise;
30 noise = reader.ReadLine();
31 i++;
32 }
33
34 }
35
36 /**//**//**//// Constructs a {@link StandardTokenizer} filtered by a {@link
37 /// StandardFilter}, a {@link LowerCaseFilter} and a {@link StopFilter}.
38 ///
39 public override TokenStream TokenStream(System.String fieldName, System.IO.TextReader reader)
40 {
41 TokenStream result = new ICTCLASTokenizer(reader);
42 result = new StandardFilter(result);
43 result = new LowerCaseFilter(result);
44 result = new StopFilter(result, CHINESE_ENGLISH_STOP_WORDS);
45 return result;
46 }
47
48
49 }
50}
ICTCLAS中文分词for Lucene.Net接口代码(实现Tokenizer):
Code
1using System;
2using System.Collections.Generic;
3using System.Text;
4
5using Lucene.Net.Analysis;
6using SharpICTCLAS;
7using System.IO;
8
9namespace AspxOn.Search.FenLei
10{
11 public class ICTCLASTokenizer : Tokenizer
12 {
13 int nKind = 1;
14 List<WordResult[]> result;
15 int startIndex = 0;
16 int endIndex = 0;
17 int i = 1;
18 /**//**/
19 /**////
20 /// 待分词的句子
21 ///
22 private string sentence;
23 /**//**/
24 /**//// Constructs a tokenizer for this Reader.
25 public ICTCLASTokenizer(System.IO.TextReader reader)
26 {
27 this.input = reader;
28 sentence = input.ReadToEnd();
29 sentence = sentence.Replace("\r\n", "");
30 string DictPath = Path.Combine(Environment.CurrentDirectory, "Data") + Path.DirectorySeparatorChar;
31 //Console.WriteLine("正在初始化字典库,请稍候");
32 WordSegment wordSegment = new WordSegment();
33 wordSegment.InitWordSegment(DictPath);
34 result = wordSegment.Segment(sentence, nKind);
35 }
36
37 /**//**/
38 /**//// 进行切词,返回数据流中下一个token或者数据流为空时返回null
39 ///
40 public override Token Next()
41 {
42 Token token = null;
43 while (i < result[0].Length - 1)
44 {
45 string word = result[0][i].sWord;
46 endIndex = startIndex + word.Length - 1;
47 token = new Token(word, startIndex, endIndex);
48 startIndex = endIndex + 1;
49
50 i++;
51 return token;
52
53 }
54 return null;
55 }
56
57 }
58}
1using System;
2using System.Collections.Generic;
3using System.Text;
4
5using Lucene.Net.Analysis;
6using SharpICTCLAS;
7using System.IO;
8
9namespace AspxOn.Search.FenLei
10{
11 public class ICTCLASTokenizer : Tokenizer
12 {
13 int nKind = 1;
14 List<WordResult[]> result;
15 int startIndex = 0;
16 int endIndex = 0;
17 int i = 1;
18 /**//**/
19 /**////
20 /// 待分词的句子
21 ///
22 private string sentence;
23 /**//**/
24 /**//// Constructs a tokenizer for this Reader.
25 public ICTCLASTokenizer(System.IO.TextReader reader)
26 {
27 this.input = reader;
28 sentence = input.ReadToEnd();
29 sentence = sentence.Replace("\r\n", "");
30 string DictPath = Path.Combine(Environment.CurrentDirectory, "Data") + Path.DirectorySeparatorChar;
31 //Console.WriteLine("正在初始化字典库,请稍候");
32 WordSegment wordSegment = new WordSegment();
33 wordSegment.InitWordSegment(DictPath);
34 result = wordSegment.Segment(sentence, nKind);
35 }
36
37 /**//**/
38 /**//// 进行切词,返回数据流中下一个token或者数据流为空时返回null
39 ///
40 public override Token Next()
41 {
42 Token token = null;
43 while (i < result[0].Length - 1)
44 {
45 string word = result[0][i].sWord;
46 endIndex = startIndex + word.Length - 1;
47 token = new Token(word, startIndex, endIndex);
48 startIndex = endIndex + 1;
49
50 i++;
51 return token;
52
53 }
54 return null;
55 }
56
57 }
58}
中文分词器代码:
Code
1using System;
2using System.Collections.Generic;
3using System.Text;
4using System.IO;
5
6using Lucene.Net.Analysis;
7using Lucene.Net.Analysis.Standard;
8using Lucene.Net.Documents;
9
10using Lucene.Net.Analysis.Cn;
11using Lucene.Net.Analysis.KTDictSeg;
12
13namespace AspxOn.Search.FenLei
14{
15 /**//// <summary>
16 /// 中文分词器
17 /// </summary>
18 public class ChineseSpliter
19 {
20 public static string Split(string text, string splitToken)
21 {
22 StringBuilder sb = new StringBuilder();
23
24 Analyzer an = new ICTCLASAnalyzer();
25
26 //TokenStream ts = an.ReusableTokenStream("", new StringReader(text));
27
28 TokenStream ts = an.TokenStream("", new StringReader(text));
29
30 Lucene.Net.Analysis.Token token;
31 while ((token = ts.Next()) != null)
32 {
33 sb.Append(splitToken + token.TermText());
34 }
35
36 return sb.ToString().Substring(1);
37 }
38 }
39}
1using System;
2using System.Collections.Generic;
3using System.Text;
4using System.IO;
5
6using Lucene.Net.Analysis;
7using Lucene.Net.Analysis.Standard;
8using Lucene.Net.Documents;
9
10using Lucene.Net.Analysis.Cn;
11using Lucene.Net.Analysis.KTDictSeg;
12
13namespace AspxOn.Search.FenLei
14{
15 /**//// <summary>
16 /// 中文分词器
17 /// </summary>
18 public class ChineseSpliter
19 {
20 public static string Split(string text, string splitToken)
21 {
22 StringBuilder sb = new StringBuilder();
23
24 Analyzer an = new ICTCLASAnalyzer();
25
26 //TokenStream ts = an.ReusableTokenStream("", new StringReader(text));
27
28 TokenStream ts = an.TokenStream("", new StringReader(text));
29
30 Lucene.Net.Analysis.Token token;
31 while ((token = ts.Next()) != null)
32 {
33 sb.Append(splitToken + token.TermText());
34 }
35
36 return sb.ToString().Substring(1);
37 }
38 }
39}
训练管理器代码:
Code
1using System;
2using System.Collections.Generic;
3using System.Text;
4using System.IO;
5
6using System.Text.RegularExpressions;
7
8namespace AspxOn.Search.FenLei
9{
10
11 /**//// <summary>
12 /// 训练管理器
13
1using System;
2using System.Collections.Generic;
3using System.Text;
4using System.IO;
5
6using System.Text.RegularExpressions;
7
8namespace AspxOn.Search.FenLei
9{
10
11 /**//// <summary>
12 /// 训练管理器
13