OSDN Git Service

MeCabNodeExtensionのポインタ化
authorkomutan <t_komuta@nifty.com>
Mon, 17 Nov 2014 09:21:24 +0000 (18:21 +0900)
committerkomutan <t_komuta@nifty.com>
Mon, 17 Nov 2014 09:21:24 +0000 (18:21 +0900)
src/LibNMeCabMMF/Extension/MeCabNodeExtension.cs
src/LibNMeCabTest/MeCabNodeExtensionTest.cs

index cbf1e77..907f904 100644 (file)
-using System;\r
-using System.Collections.Generic;\r
-using System.Text;\r
-\r
-namespace NMeCab.Extension\r
-{\r
-    public static class MeCabNodeExtension\r
-    {\r
-        /// <summary>\r
-        /// 品詞を取得\r
-        /// </summary>\r
-        public static string GetPartsOfSpeech(this MeCabNode node)\r
-        {\r
-            return GetCsvElement(node.Feature, 0);\r
-        }\r
-\r
-        /// <summary>\r
-        /// 品詞細分類1を取得\r
-        /// </summary>\r
-        public static string GetPartsOfSpeechSection1(this MeCabNode node)\r
-        {\r
-            return GetCsvElement(node.Feature, 1);\r
-        }\r
-\r
-        /// <summary>\r
-        /// 品詞細分類2を取得\r
-        /// </summary>\r
-        public static string GetPartsOfSpeechSection2(this MeCabNode node)\r
-        {\r
-            return GetCsvElement(node.Feature, 2);\r
-        }\r
-\r
-        /// <summary>\r
-        /// 品詞細分類3を取得\r
-        /// </summary>\r
-        public static string GetPartsOfSpeechSection3(this MeCabNode node)\r
-        {\r
-            return GetCsvElement(node.Feature, 3);\r
-        }\r
-\r
-        /// <summary>\r
-        /// 活用形を取得\r
-        /// </summary>\r
-        public static string GetConjugatedForm(this MeCabNode node)\r
-        {\r
-            return GetCsvElement(node.Feature, 4);\r
-        }\r
-\r
-        /// <summary>\r
-        /// 活用型を取得\r
-        /// </summary>\r
-        public static string GetInflection(this MeCabNode node)\r
-        {\r
-            return GetCsvElement(node.Feature, 5);\r
-        }\r
-\r
-        /// <summary>\r
-        /// 活用型を取得\r
-        /// </summary>\r
-        public static string GetOriginalForm(this MeCabNode node)\r
-        {\r
-            return GetCsvElement(node.Feature, 6);\r
-        }\r
-\r
-        /// <summary>\r
-        /// 読みを取得\r
-        /// </summary>\r
-        public static string GetReading(this MeCabNode node)\r
-        {\r
-            return GetCsvElement(node.Feature, 7);\r
-        }\r
-\r
-        /// <summary>\r
-        /// 発音を取得\r
-        /// </summary>\r
-        public static string GetPronounciation(this MeCabNode node)\r
-        {\r
-            return GetCsvElement(node.Feature, 8);\r
-        }\r
-\r
-        private static string GetCsvElement(string csvRow, int index)\r
-        {\r
-            if (string.IsNullOrEmpty(csvRow)) return null;\r
-\r
-            string[] items = csvRow.Split(',');\r
-            if (items.Length <= index) return null;\r
-\r
-            return items[index];\r
-        }\r
-    }\r
-}\r
+using System;
+using System.Collections.Generic;
+using System.Text;
+
+namespace NMeCab.Extension
+{
+    public static class MeCabNodeExtension
+    {
+        /// <summary>
+        /// 品詞を取得
+        /// </summary>
+        public static string GetPartsOfSpeech(this MeCabNode node)
+        {
+            return GetCsvElement(node.Feature, 0);
+        }
+
+        /// <summary>
+        /// 品詞細分類1を取得
+        /// </summary>
+        public static string GetPartsOfSpeechSection1(this MeCabNode node)
+        {
+            return GetCsvElement(node.Feature, 1);
+        }
+
+        /// <summary>
+        /// 品詞細分類2を取得
+        /// </summary>
+        public static string GetPartsOfSpeechSection2(this MeCabNode node)
+        {
+            return GetCsvElement(node.Feature, 2);
+        }
+
+        /// <summary>
+        /// 品詞細分類3を取得
+        /// </summary>
+        public static string GetPartsOfSpeechSection3(this MeCabNode node)
+        {
+            return GetCsvElement(node.Feature, 3);
+        }
+
+        /// <summary>
+        /// 活用形を取得
+        /// </summary>
+        public static string GetConjugatedForm(this MeCabNode node)
+        {
+            return GetCsvElement(node.Feature, 4);
+        }
+
+        /// <summary>
+        /// 活用型を取得
+        /// </summary>
+        public static string GetInflection(this MeCabNode node)
+        {
+            return GetCsvElement(node.Feature, 5);
+        }
+
+        /// <summary>
+        /// 活用型を取得
+        /// </summary>
+        public static string GetOriginalForm(this MeCabNode node)
+        {
+            return GetCsvElement(node.Feature, 6);
+        }
+
+        /// <summary>
+        /// 読みを取得
+        /// </summary>
+        public static string GetReading(this MeCabNode node)
+        {
+            return GetCsvElement(node.Feature, 7);
+        }
+
+        /// <summary>
+        /// 発音を取得
+        /// </summary>
+        public static string GetPronounciation(this MeCabNode node)
+        {
+            return GetCsvElement(node.Feature, 8);
+        }
+
+
+        private unsafe static string GetCsvElement(string csvRow, int index)
+        {
+            if (string.IsNullOrEmpty(csvRow)) return null;
+
+            fixed (char* pCsvRow = csvRow)
+                return GetCsvElement(pCsvRow, csvRow.Length, index);
+
+            //string[] items = csvRow.Split(',');
+            //if (items.Length <= index) return null;
+
+            //return items[index];
+        }
+
+        private unsafe static string GetCsvElement(char* csvRow, int rowLength, int index)
+        {
+            char* end = csvRow + rowLength;
+
+            for (int i = 0; i < index; i++)
+            {
+                while (*csvRow != ',')
+                {
+                    if (csvRow == end) return null;
+                    csvRow++;
+                }
+                csvRow++;
+            }
+
+            int len = 0;
+            while (csvRow != end && *csvRow != ',')
+            {
+                len++;
+                csvRow++;
+            }
+
+            return new string(csvRow - len, 0, len);
+        }
+    }
+}
index de37df2..fcca786 100644 (file)
-using System;\r
-using Microsoft.VisualStudio.TestTools.UnitTesting;\r
-using NMeCab.Extension;\r
-\r
-namespace LibNMeCabTest\r
-{\r
-    [TestClass]\r
-    public class MeCabNodeExtensionTest\r
-    {\r
-        [TestMethod]\r
-        public void TestMethod1()\r
-        {\r
-            var node = new NMeCab.MeCabNode()\r
-            {\r
-                Feature = "品詞,品詞細分類1,品詞細分類2,品詞細分類3,活用形,活用型,原形,読み,発音"\r
-            };\r
-            Assert.AreEqual(node.GetPartsOfSpeech(), "品詞");\r
-            Assert.AreEqual(node.GetPartsOfSpeechSection1(), "品詞細分類1");\r
-            Assert.AreEqual(node.GetPartsOfSpeechSection2(), "品詞細分類2");\r
-            Assert.AreEqual(node.GetPartsOfSpeechSection3(), "品詞細分類3");\r
-            Assert.AreEqual(node.GetConjugatedForm(), "活用形");\r
-            Assert.AreEqual(node.GetInflection(), "活用型");\r
-            Assert.AreEqual(node.GetOriginalForm(), "原形");\r
-            Assert.AreEqual(node.GetReading(), "読み");\r
-            Assert.AreEqual(node.GetPronounciation(), "発音");\r
-        }\r
-\r
-        [TestMethod]\r
-        public void TestMethod2()\r
-        {\r
-            var node = new NMeCab.MeCabNode()\r
-            {\r
-                Feature = "品詞"\r
-            };\r
-            Assert.AreEqual(node.GetPartsOfSpeech(), "品詞");\r
-            Assert.IsNull(node.GetPartsOfSpeechSection1());\r
-            Assert.IsNull(node.GetPartsOfSpeechSection2());\r
-            Assert.IsNull(node.GetPartsOfSpeechSection3());\r
-            Assert.IsNull(node.GetConjugatedForm());\r
-            Assert.IsNull(node.GetInflection());\r
-            Assert.IsNull(node.GetOriginalForm());\r
-            Assert.IsNull(node.GetReading());\r
-            Assert.IsNull(node.GetPronounciation());\r
-        }\r
-\r
-        [TestMethod]\r
-        public void TestMethod3()\r
-        {\r
-            var node = new NMeCab.MeCabNode()\r
-            {\r
-                Feature = null\r
-            };\r
-            Assert.IsNull(node.GetPartsOfSpeech());\r
-            Assert.IsNull(node.GetPartsOfSpeechSection1());\r
-            Assert.IsNull(node.GetPartsOfSpeechSection2());\r
-            Assert.IsNull(node.GetPartsOfSpeechSection3());\r
-            Assert.IsNull(node.GetConjugatedForm());\r
-            Assert.IsNull(node.GetInflection());\r
-            Assert.IsNull(node.GetOriginalForm());\r
-            Assert.IsNull(node.GetReading());\r
-            Assert.IsNull(node.GetPronounciation());\r
-        }\r
-    }\r
-}\r
+using System;
+using Microsoft.VisualStudio.TestTools.UnitTesting;
+using NMeCab.Extension;
+
+namespace LibNMeCabTest
+{
+    [TestClass]
+    public class MeCabNodeExtensionTest
+    {
+        [TestMethod]
+        public void TestMethod1()
+        {
+            var node = new NMeCab.MeCabNode()
+            {
+                Feature = "品詞,品詞細分類1,品詞細分類2,品詞細分類3,活用形,活用型,原形,読み,発音"
+            };
+            Assert.AreEqual("品詞", node.GetPartsOfSpeech());
+            Assert.AreEqual("品詞細分類1", node.GetPartsOfSpeechSection1());
+            Assert.AreEqual("品詞細分類2", node.GetPartsOfSpeechSection2());
+            Assert.AreEqual("品詞細分類3", node.GetPartsOfSpeechSection3());
+            Assert.AreEqual("活用形", node.GetConjugatedForm());
+            Assert.AreEqual("活用型", node.GetInflection());
+            Assert.AreEqual("原形", node.GetOriginalForm());
+            Assert.AreEqual("読み", node.GetReading());
+            Assert.AreEqual("発音", node.GetPronounciation());
+        }
+
+        [TestMethod]
+        public void TestMethod2()
+        {
+            var node = new NMeCab.MeCabNode()
+            {
+                Feature = ",,,,,,,,"
+            };
+            Assert.AreEqual("", node.GetPartsOfSpeech());
+            Assert.AreEqual("", node.GetPartsOfSpeechSection1());
+            Assert.AreEqual("", node.GetPartsOfSpeechSection2());
+            Assert.AreEqual("", node.GetPartsOfSpeechSection3());
+            Assert.AreEqual("", node.GetConjugatedForm());
+            Assert.AreEqual("", node.GetInflection());
+            Assert.AreEqual("", node.GetOriginalForm());
+            Assert.AreEqual("", node.GetReading());
+            Assert.AreEqual("", node.GetPronounciation());
+        }
+
+        [TestMethod]
+        public void TestMethod3()
+        {
+            var node = new NMeCab.MeCabNode()
+            {
+                Feature = null
+            };
+            Assert.IsNull(node.GetPartsOfSpeech());
+            Assert.IsNull(node.GetPartsOfSpeechSection1());
+            Assert.IsNull(node.GetPartsOfSpeechSection2());
+            Assert.IsNull(node.GetPartsOfSpeechSection3());
+            Assert.IsNull(node.GetConjugatedForm());
+            Assert.IsNull(node.GetInflection());
+            Assert.IsNull(node.GetOriginalForm());
+            Assert.IsNull(node.GetReading());
+            Assert.IsNull(node.GetPronounciation());
+        }
+
+        [TestMethod]
+        public void TestMethod4()
+        {
+            var node = new NMeCab.MeCabNode()
+            {
+                Feature = ""
+            };
+            Assert.IsNull(node.GetPartsOfSpeech());
+            Assert.IsNull(node.GetPartsOfSpeechSection1());
+            Assert.IsNull(node.GetPartsOfSpeechSection2());
+            Assert.IsNull(node.GetPartsOfSpeechSection3());
+            Assert.IsNull(node.GetConjugatedForm());
+            Assert.IsNull(node.GetInflection());
+            Assert.IsNull(node.GetOriginalForm());
+            Assert.IsNull(node.GetReading());
+            Assert.IsNull(node.GetPronounciation());
+        }
+
+        [TestMethod]
+        public void TestMethod5()
+        {
+            var node = new NMeCab.MeCabNode()
+            {
+                Feature = "品詞"
+            };
+            Assert.AreEqual("品詞", node.GetPartsOfSpeech());
+            Assert.IsNull(node.GetPartsOfSpeechSection1());
+            Assert.IsNull(node.GetPartsOfSpeechSection2());
+            Assert.IsNull(node.GetPartsOfSpeechSection3());
+            Assert.IsNull(node.GetConjugatedForm());
+            Assert.IsNull(node.GetInflection());
+            Assert.IsNull(node.GetOriginalForm());
+            Assert.IsNull(node.GetReading());
+            Assert.IsNull(node.GetPronounciation());
+        }
+
+        [TestMethod]
+        public void TestMethod6()
+        {
+            var node = new NMeCab.MeCabNode()
+            {
+                Feature = "品詞,品詞細分類1"
+            };
+            Assert.AreEqual("品詞", node.GetPartsOfSpeech());
+            Assert.AreEqual("品詞細分類1", node.GetPartsOfSpeechSection1());
+            Assert.IsNull(node.GetPartsOfSpeechSection2());
+            Assert.IsNull(node.GetPartsOfSpeechSection3());
+            Assert.IsNull(node.GetConjugatedForm());
+            Assert.IsNull(node.GetInflection());
+            Assert.IsNull(node.GetOriginalForm());
+            Assert.IsNull(node.GetReading());
+            Assert.IsNull(node.GetPronounciation());
+        }
+
+        [TestMethod]
+        public void TestMethod7()
+        {
+            var node = new NMeCab.MeCabNode()
+            {
+                Feature = "品詞,品詞細分類1,"
+            };
+            Assert.AreEqual("品詞", node.GetPartsOfSpeech());
+            Assert.AreEqual("品詞細分類1", node.GetPartsOfSpeechSection1());
+            Assert.AreEqual("", node.GetPartsOfSpeechSection2());
+            Assert.IsNull(node.GetPartsOfSpeechSection3());
+            Assert.IsNull(node.GetConjugatedForm());
+            Assert.IsNull(node.GetInflection());
+            Assert.IsNull(node.GetOriginalForm());
+            Assert.IsNull(node.GetReading());
+            Assert.IsNull(node.GetPronounciation());
+        }
+    }
+}