OSDN Git Service

907f904c94b37873abae8208d7201ef1ca69ee0c
[nmecab/NMeCabRepo2.git] / src / LibNMeCabMMF / Extension / MeCabNodeExtension.cs
1 using System;
2 using System.Collections.Generic;
3 using System.Text;
4
5 namespace NMeCab.Extension
6 {
7     public static class MeCabNodeExtension
8     {
9         /// <summary>
10         /// 品詞を取得
11         /// </summary>
12         public static string GetPartsOfSpeech(this MeCabNode node)
13         {
14             return GetCsvElement(node.Feature, 0);
15         }
16
17         /// <summary>
18         /// 品詞細分類1を取得
19         /// </summary>
20         public static string GetPartsOfSpeechSection1(this MeCabNode node)
21         {
22             return GetCsvElement(node.Feature, 1);
23         }
24
25         /// <summary>
26         /// 品詞細分類2を取得
27         /// </summary>
28         public static string GetPartsOfSpeechSection2(this MeCabNode node)
29         {
30             return GetCsvElement(node.Feature, 2);
31         }
32
33         /// <summary>
34         /// 品詞細分類3を取得
35         /// </summary>
36         public static string GetPartsOfSpeechSection3(this MeCabNode node)
37         {
38             return GetCsvElement(node.Feature, 3);
39         }
40
41         /// <summary>
42         /// 活用形を取得
43         /// </summary>
44         public static string GetConjugatedForm(this MeCabNode node)
45         {
46             return GetCsvElement(node.Feature, 4);
47         }
48
49         /// <summary>
50         /// 活用型を取得
51         /// </summary>
52         public static string GetInflection(this MeCabNode node)
53         {
54             return GetCsvElement(node.Feature, 5);
55         }
56
57         /// <summary>
58         /// 活用型を取得
59         /// </summary>
60         public static string GetOriginalForm(this MeCabNode node)
61         {
62             return GetCsvElement(node.Feature, 6);
63         }
64
65         /// <summary>
66         /// 読みを取得
67         /// </summary>
68         public static string GetReading(this MeCabNode node)
69         {
70             return GetCsvElement(node.Feature, 7);
71         }
72
73         /// <summary>
74         /// 発音を取得
75         /// </summary>
76         public static string GetPronounciation(this MeCabNode node)
77         {
78             return GetCsvElement(node.Feature, 8);
79         }
80
81
82         private unsafe static string GetCsvElement(string csvRow, int index)
83         {
84             if (string.IsNullOrEmpty(csvRow)) return null;
85
86             fixed (char* pCsvRow = csvRow)
87                 return GetCsvElement(pCsvRow, csvRow.Length, index);
88
89             //string[] items = csvRow.Split(',');
90             //if (items.Length <= index) return null;
91
92             //return items[index];
93         }
94
95         private unsafe static string GetCsvElement(char* csvRow, int rowLength, int index)
96         {
97             char* end = csvRow + rowLength;
98
99             for (int i = 0; i < index; i++)
100             {
101                 while (*csvRow != ',')
102                 {
103                     if (csvRow == end) return null;
104                     csvRow++;
105                 }
106                 csvRow++;
107             }
108
109             int len = 0;
110             while (csvRow != end && *csvRow != ',')
111             {
112                 len++;
113                 csvRow++;
114             }
115
116             return new string(csvRow - len, 0, len);
117         }
118     }
119 }