1 // MeCab -- Yet Another Part-of-Speech and Morphological Analyzer
\r
3 // Copyright(C) 2001-2006 Taku Kudo <taku@chasen.org>
\r
4 // Copyright(C) 2004-2006 Nippon Telegraph and Telephone Corporation
\r
6 using System.Collections.Generic;
\r
12 public class MeCabTagger : IDisposable
\r
14 private readonly Viterbi viterbi = new Viterbi();
\r
15 private readonly Writer writer = new Writer();
\r
24 get { this.ThrowIfDisposed(); return this.viterbi.Partial; }
\r
25 set { this.ThrowIfDisposed(); this.viterbi.Partial = value; }
\r
29 /// ソフト分かち書きの温度パラメータ
\r
33 get { this.ThrowIfDisposed(); return this.viterbi.Theta; }
\r
34 set { this.ThrowIfDisposed(); this.viterbi.Theta = value; }
\r
38 /// ラティスレベル(どの程度のラティス情報を解析時に構築するか)
\r
41 /// 0: 最適解のみが出力可能なレベル (デフォルト, 高速)
\r
42 /// 1: N-best 解が出力可能なレベル (中速)
\r
43 /// 2: ソフトわかち書きが可能なレベル (低速)
\r
45 public MeCabLatticeLevel LatticeLevel
\r
47 get { this.ThrowIfDisposed(); return this.viterbi.LatticeLevel; }
\r
48 set { this.ThrowIfDisposed(); this.viterbi.LatticeLevel = value; }
\r
58 public bool AllMorphs
\r
60 get { this.ThrowIfDisposed(); return this.viterbi.AllMorphs; }
\r
61 set { this.ThrowIfDisposed(); this.viterbi.AllMorphs = value; }
\r
67 public string OutPutFormatType
\r
69 get { this.ThrowIfDisposed(); return this.writer.OutputFormatType; }
\r
70 set { this.ThrowIfDisposed(); this.writer.OutputFormatType = value; }
\r
80 private MeCabTagger()
\r
91 /// <param name="param">初期化パラメーター</param>
\r
92 private void Open(MeCabParam param)
\r
94 this.viterbi.Open(param);
\r
96 this.writer.Open(param);
\r
100 /// MeCabTaggerのインスタンスを生成する
\r
102 /// <returns>MeCabTaggerのインスタンス</returns>
\r
103 public static MeCabTagger Create()
\r
105 MeCabParam param = new MeCabParam();
\r
107 return MeCabTagger.Create(param);
\r
111 /// MeCabTaggerのインスタンスを生成する
\r
113 /// <param name="param">初期化パラメーター</param>
\r
114 /// <returns>MeCabTaggerのインスタンス</returns>
\r
115 public static MeCabTagger Create(MeCabParam param)
\r
117 MeCabTagger tagger = new MeCabTagger();
\r
118 tagger.Open(param);
\r
129 /// <param name="str">解析対象の文字列</param>
\r
130 /// <returns>解析結果の文字列</returns>
\r
131 public unsafe string Parse(string str)
\r
133 fixed (char* pStr = str)
\r
134 return this.Parse(pStr, str.Length);
\r
140 /// <param name="str">解析対象の文字列へのポインタ</param>
\r
141 /// <param name="len">解析対象の文字列の長さ</param>
\r
142 /// <returns>解析結果の文字列</returns>
\r
143 public unsafe string Parse(char* str, int len)
\r
145 MeCabNode n = this.ParseToNode(str, len);
\r
146 if (n == null) return null;
\r
147 StringBuilder os = new StringBuilder();
\r
148 this.writer.Write(os, n);
\r
149 return os.ToString();
\r
155 /// <param name="str">解析対象の文字列</param>
\r
156 /// <returns>文頭の形態素</returns>
\r
157 public unsafe MeCabNode ParseToNode(string str)
\r
159 if (str == null) throw new ArgumentNullException("str");
\r
161 fixed (char* pStr = str)
\r
162 return this.ParseToNode(pStr, str.Length);
\r
168 /// <param name="str">解析対象の文字列へのポインタ</param>
\r
169 /// <param name="len">解析対象の文字列の長さ</param>
\r
170 /// <returns>文頭の形態素</returns>
\r
171 public unsafe MeCabNode ParseToNode(char* str, int len)
\r
173 this.ThrowIfDisposed();
\r
174 if (len < 0) throw new ArgumentOutOfRangeException("len", "Please set one or more to length of string.");
\r
176 return this.viterbi.Analyze(str, len);
\r
184 /// 解析を行い結果を確からしいものから順番に取得する
\r
186 /// <param name="str">解析対象の文字列</param>
\r
187 /// <returns>文頭の形態素を、確からしい順に取得する列挙子</returns>
\r
188 public unsafe IEnumerable<MeCabNode> ParseNBestToNode(string str)
\r
190 fixed (char* pStr = str)
\r
191 return this.ParseNBestToNode(pStr, str.Length);
\r
195 /// 解析を行い結果を確からしいものから順番に取得する
\r
197 /// <param name="str">解析対象の文字列へのポインタ</param>
\r
198 /// <returns>文頭の形態素を、確からしい順に取得する列挙子</returns>
\r
199 public unsafe IEnumerable<MeCabNode> ParseNBestToNode(char* str, int len)
\r
201 if (this.LatticeLevel == 0)
\r
202 throw new InvalidOperationException("Please set one or more to LatticeLevel.");
\r
204 MeCabNode n = this.ParseToNode(str, len);
\r
205 NBestGenerator nBest = new NBestGenerator();
\r
207 return nBest.GetEnumerator();
\r
211 /// ParseのN-Best解出力version
\r
213 /// <param name="n">必要な解析結果の個数</param>
\r
214 /// <param name="str">解析対象の文字列</param>
\r
215 /// <returns>解析結果の文字列</returns>
\r
216 public unsafe string ParseNBest(int n, string str)
\r
218 fixed (char* pStr = str)
\r
219 return this.ParseNBest(n, pStr, str.Length);
\r
223 /// ParseのN-Best解出力version
\r
225 /// <param name="n">必要な解析結果の個数</param>
\r
226 /// <param name="str">解析対象の文字列へのポインタ</param>
\r
227 /// <param name="len">解析対象の文字列の長さ</param>
\r
228 /// <returns>解析結果の文字列</returns>
\r
229 public unsafe string ParseNBest(int n, char* str, int len)
\r
231 if (n <= 0) throw new ArgumentOutOfRangeException("n", "");
\r
233 if (n == 1) return this.Parse(str, len);
\r
235 StringBuilder os = new StringBuilder();
\r
236 foreach (MeCabNode node in this.ParseNBestToNode(str, len))
\r
238 this.writer.Write(os, node);
\r
239 if (--n == 0) break;
\r
241 return os.ToString();
\r
248 private bool disposed;
\r
253 public void Dispose()
\r
255 this.Dispose(true);
\r
256 GC.SuppressFinalize(this);
\r
259 protected virtual void Dispose(bool disposing)
\r
261 if (disposed) return;
\r
265 this.viterbi.Dispose(); //Nullチェック不要
\r
268 this.disposed = true;
\r
273 this.Dispose(false);
\r
276 private void ThrowIfDisposed()
\r
279 throw new ObjectDisposedException(this.GetType().FullName);
\r