OSDN Git Service

MMFへのアクセスをアンマネージドポインタ経由として高速化
[nmecab/NMeCabRepo2.git] / src / LibNMeCab / Core / CharProperty.cs
1 //  MeCab -- Yet Another Part-of-Speech and Morphological Analyzer
2 //
3 //  Copyright(C) 2001-2006 Taku Kudo <taku@chasen.org>
4 //  Copyright(C) 2004-2006 Nippon Telegraph and Telephone Corporation
5 using System;
6 using System.Collections.Generic;
7 using System.Text;
8 using System.IO;
9
10 namespace NMeCab.Core
11 {
12     public class CharProperty
13     {
14         #region Const/Field/Property
15
16         private const string CharPropertyFile = "char.bin";
17
18         private string[] cList;
19
20         private readonly CharInfo[] charInfoList = new CharInfo[0xFFFF];
21
22         public int Size
23         {
24             get { return this.cList.Length; }
25         }
26
27         #endregion
28
29         #region Open
30
31         public void Open(string dicDir)
32         {
33             string fileName = Path.Combine(dicDir, CharPropertyFile);
34
35             using (var stream = new FileStream(fileName, FileMode.Open, FileAccess.Read))
36             using (var reader = new BinaryReader(stream))
37             {
38                 this.Open(reader, fileName);
39             }
40         }
41
42         public void Open(BinaryReader reader, string fileName = null)
43         {
44             uint cSize = reader.ReadUInt32();
45
46             if (reader.BaseStream.CanSeek)
47             {
48                 long fSize = sizeof(uint) + 32 * cSize + sizeof(uint) * charInfoList.Length;
49                 if (reader.BaseStream.Length != fSize)
50                     throw new MeCabInvalidFileException("invalid file size", fileName);
51             }
52
53             this.cList = new string[cSize];
54             for (int i = 0; i < this.cList.Length; i++)
55             {
56                 this.cList[i] = StrUtils.GetString(reader.ReadBytes(32), Encoding.ASCII);
57             }
58
59             for (int i = 0; i < this.charInfoList.Length; i++)
60             {
61                 this.charInfoList[i] = new CharInfo(reader.ReadUInt32());
62             }
63         }
64
65         #endregion
66
67         #region Get Infometion
68
69         public string Name(int i)
70         {
71             return this.cList[i];
72         }
73
74         public unsafe char* SeekToOtherType(char* begin, char* end, CharInfo c, CharInfo* fail, int* cLen)
75         {
76             char* p = begin;
77             *cLen = 0;
78
79             *fail = this.GetCharInfo(*p);
80
81             while (p != end && c.IsKindOf(*fail))
82             {
83                 p++;
84                 (*cLen)++;
85                 c = *fail;
86
87                 *fail = this.GetCharInfo(*p);
88             }
89
90             return p;
91         }
92
93         public CharInfo GetCharInfo(char c)
94         {
95             return this.charInfoList[c];
96         }
97
98         #endregion
99     }
100 }