Skip to content

Instantly share code, notes, and snippets.

@EbiseLutica
Last active August 29, 2015 13:57
Show Gist options
  • Save EbiseLutica/9872609 to your computer and use it in GitHub Desktop.
Save EbiseLutica/9872609 to your computer and use it in GitHub Desktop.
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using NMeCab;
namespace ConsoleApplication2
{
class Program
{
static void Main(string[] args)
{
while (true)
{
Console.WriteLine("文を入力してください>>");
Console.WriteLine(RussianReversalMachine.RussianReversal(Console.ReadLine()));
}
}
}
static class RussianReversalMachine
{
public static string RussianReversal(string source)
{
MeCabTagger tagger = MeCabTagger.Create();
MeCabNode node = tagger.ParseToNode(source);
string location = "";
string person = "";
string obj = "";
string verb = "";
string joshi = "";
List<MeCabNode> nodes = new List<MeCabNode>();
int cnt = 0;
while (node != null)
{
if (cnt == 0)
{
node = node.Next;
cnt++;
continue;
}
nodes.Add(node);
node = node.Next;
cnt++;
}
List<MeCabNode> temp = new List<MeCabNode>();
for (int i = 0; i < nodes.Count; i++)
{
MeCabNode n = nodes[i];
if (i < nodes.Count - 1 && n.Surface == "で" && nodes[i + 1].Surface == "は")
{
temp.Add(n);
temp.Add(nodes[i + 1]);
i++;
location = AddNodesToString(location, temp);
temp.Clear();
continue;
}
if (n.Surface == "が" || n.Surface == "は")
{
person = AddNodesToString(person, temp);
joshi = n.Surface;
temp.Clear();
continue;
}
if (n.Surface == "を")
{
obj = AddNodesToString(obj, temp);
person += n.Surface;
temp.Clear();
continue;
}
if (n.Feature.Split(',')[0] == "動詞" && n.Feature.Split(',')[5] == "基本形")
{
temp.Add(n);
verb = AddNodesToString(verb, temp);
temp.Clear();
continue;
}
temp.Add(n);
}
obj += joshi;
verb = AddNodesToString(verb, temp);
return location + obj + person + verb;
}
public static string AddNodesToString(string target, List<MeCabNode> nodes)
{
string lastdata = target;
foreach (MeCabNode node in nodes)
lastdata += node.Surface;
return lastdata;
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment