From a74b7a96adecfd4381e6b785ae9dbe41ba1f63f1 Mon Sep 17 00:00:00 2001 From: komutan Date: Mon, 17 Nov 2014 18:21:24 +0900 Subject: [PATCH] =?utf8?q?MeCabNodeExtension=E3=81=AE=E3=83=9D=E3=82=A4?= =?utf8?q?=E3=83=B3=E3=82=BF=E5=8C=96?= MIME-Version: 1.0 Content-Type: text/plain; charset=utf8 Content-Transfer-Encoding: 8bit --- src/LibNMeCabMMF/Extension/MeCabNodeExtension.cs | 210 +++++++++++++---------- src/LibNMeCabTest/MeCabNodeExtensionTest.cs | 200 ++++++++++++++------- 2 files changed, 255 insertions(+), 155 deletions(-) diff --git a/src/LibNMeCabMMF/Extension/MeCabNodeExtension.cs b/src/LibNMeCabMMF/Extension/MeCabNodeExtension.cs index cbf1e77..907f904 100644 --- a/src/LibNMeCabMMF/Extension/MeCabNodeExtension.cs +++ b/src/LibNMeCabMMF/Extension/MeCabNodeExtension.cs @@ -1,91 +1,119 @@ -using System; -using System.Collections.Generic; -using System.Text; - -namespace NMeCab.Extension -{ - public static class MeCabNodeExtension - { - /// - /// 品詞を取得 - /// - public static string GetPartsOfSpeech(this MeCabNode node) - { - return GetCsvElement(node.Feature, 0); - } - - /// - /// 品詞細分類1を取得 - /// - public static string GetPartsOfSpeechSection1(this MeCabNode node) - { - return GetCsvElement(node.Feature, 1); - } - - /// - /// 品詞細分類2を取得 - /// - public static string GetPartsOfSpeechSection2(this MeCabNode node) - { - return GetCsvElement(node.Feature, 2); - } - - /// - /// 品詞細分類3を取得 - /// - public static string GetPartsOfSpeechSection3(this MeCabNode node) - { - return GetCsvElement(node.Feature, 3); - } - - /// - /// 活用形を取得 - /// - public static string GetConjugatedForm(this MeCabNode node) - { - return GetCsvElement(node.Feature, 4); - } - - /// - /// 活用型を取得 - /// - public static string GetInflection(this MeCabNode node) - { - return GetCsvElement(node.Feature, 5); - } - - /// - /// 活用型を取得 - /// - public static string GetOriginalForm(this MeCabNode node) - { - return GetCsvElement(node.Feature, 6); - } - - /// - /// 読みを取得 - /// - public static string GetReading(this MeCabNode node) - { - return GetCsvElement(node.Feature, 7); - } - - /// - /// 発音を取得 - /// - public static string GetPronounciation(this MeCabNode node) - { - return GetCsvElement(node.Feature, 8); - } - - private static string GetCsvElement(string csvRow, int index) - { - if (string.IsNullOrEmpty(csvRow)) return null; - - string[] items = csvRow.Split(','); - if (items.Length <= index) return null; - - return items[index]; - } - } -} +using System; +using System.Collections.Generic; +using System.Text; + +namespace NMeCab.Extension +{ + public static class MeCabNodeExtension + { + /// + /// 品詞を取得 + /// + public static string GetPartsOfSpeech(this MeCabNode node) + { + return GetCsvElement(node.Feature, 0); + } + + /// + /// 品詞細分類1を取得 + /// + public static string GetPartsOfSpeechSection1(this MeCabNode node) + { + return GetCsvElement(node.Feature, 1); + } + + /// + /// 品詞細分類2を取得 + /// + public static string GetPartsOfSpeechSection2(this MeCabNode node) + { + return GetCsvElement(node.Feature, 2); + } + + /// + /// 品詞細分類3を取得 + /// + public static string GetPartsOfSpeechSection3(this MeCabNode node) + { + return GetCsvElement(node.Feature, 3); + } + + /// + /// 活用形を取得 + /// + public static string GetConjugatedForm(this MeCabNode node) + { + return GetCsvElement(node.Feature, 4); + } + + /// + /// 活用型を取得 + /// + public static string GetInflection(this MeCabNode node) + { + return GetCsvElement(node.Feature, 5); + } + + /// + /// 活用型を取得 + /// + public static string GetOriginalForm(this MeCabNode node) + { + return GetCsvElement(node.Feature, 6); + } + + /// + /// 読みを取得 + /// + public static string GetReading(this MeCabNode node) + { + return GetCsvElement(node.Feature, 7); + } + + /// + /// 発音を取得 + /// + 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); + } + } +} diff --git a/src/LibNMeCabTest/MeCabNodeExtensionTest.cs b/src/LibNMeCabTest/MeCabNodeExtensionTest.cs index de37df2..fcca786 100644 --- a/src/LibNMeCabTest/MeCabNodeExtensionTest.cs +++ b/src/LibNMeCabTest/MeCabNodeExtensionTest.cs @@ -1,64 +1,136 @@ -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(node.GetPartsOfSpeechSection1(), "品詞細分類1"); - Assert.AreEqual(node.GetPartsOfSpeechSection2(), "品詞細分類2"); - Assert.AreEqual(node.GetPartsOfSpeechSection3(), "品詞細分類3"); - 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.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 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()); - } - } -} +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()); + } + } +} -- 2.11.0