Esempio n. 1
0
 public Word(DistinctString surface, Hinshi hinshi, string reading = null, string roma = null)
 {
     _surface = surface;
     _reading = reading;
     _roma    = roma;
     _hinshi  = hinshi;
 }
Esempio n. 2
0
 public StringRebuilder(DistinctString ds)
 {
     DistinctStr = ds;
     isDeleted   = new bool[ds.Str.Length];
     addends     = new List <string> [ds.Str.Length + 1];
     for (int i = 0; i < ds.Str.Length + 1; i++)
     {
         addends[i] = new List <string>();
     }
 }
        public DistinctString[] Split(char[] separator)
        {
            string[]         strings = Str.Split(separator);
            DistinctString[] res     = new DistinctString[strings.Length];
            int cnt = 0;

            for (int i = 0; i < strings.Length; i++)
            {
                res[i] = new DistinctString(strings[i], Ids.GetRange(cnt, strings[i].Length));
                cnt   += strings[i].Length;
            }
            return(res);
        }
Esempio n. 4
0
        public Word(DistinctString surface, string reading, string hinshi)
        {
            _surface = surface;
            _reading = reading;

            switch (hinshi)
            {
            case "名詞":
                _hinshi = Hinshi.Meishi;
                break;

            case "動詞":
                _hinshi = Hinshi.Doshi;
                break;

            case "形容詞":
                _hinshi = Hinshi.Keiyoshi;
                break;

            case "形容動詞":
                _hinshi = Hinshi.Keiyodoshi;
                break;

            case "副詞":
                _hinshi = Hinshi.Fukushi;
                break;

            case "連体詞":
                _hinshi = Hinshi.Rentaishi;
                break;

            case "接続詞":
                _hinshi = Hinshi.Setsuzokushi;
                break;

            case "助動詞":
                _hinshi = Hinshi.Jodoshi;
                break;

            case "助詞":
                _hinshi = Hinshi.Joshi;
                break;

            default:
                _hinshi = Hinshi.Other;
                break;
            }
        }
Esempio n. 5
0
        //句点や読点などは、品詞「Other」として扱われる
        public static List <List <IWord> > ParseSentence(DistinctString sentence, out List <int> parentIndeces)
        {
            List <List <IWord> > chunks = new List <List <IWord> >();

            parentIndeces = new List <int>();


            if (string.IsNullOrEmpty(sentence.Str))
            {
                return(chunks);
            }

            string appid = ConfigurationManager.AppSettings["yahooApiKey"];
            //string appid = ConfigurationManager.AppSettings["yahooApiKey"];
            var uri = "https://jlp.yahooapis.jp/DAService/V1/parse?appid=" + appid + "&sentence=" +
                      sentence.Str;
            var request = (HttpWebRequest)WebRequest.Create(uri);

            try
            {
                WebResponse response   = request.GetResponse();
                var         respStream = response.GetResponseStream();
                int         cnt        = 0;

                using (StreamReader sr = new StreamReader(respStream))
                {
                    string      res      = sr.ReadToEnd();
                    XmlDocument document = new XmlDocument();
                    document.LoadXml(res);
                    XmlElement  root      = document.DocumentElement;
                    XmlNodeList chunkList = root.GetElementsByTagName("Chunk");
                    for (int i = 0; i < chunkList.Count; i++)
                    {
                        parentIndeces.Add(-1);
                        chunks.Add(new List <IWord>());
                    }
                    foreach (XmlElement chunk in chunkList)
                    {
                        int id = int.Parse(chunk["Id"].InnerText);
                        parentIndeces[id] = int.Parse(chunk["Dependency"].InnerText);
                        XmlNodeList morphemList = chunk.GetElementsByTagName("Morphem");
                        foreach (XmlNode morphem in morphemList)
                        {
                            string surface = morphem["Surface"].InnerText;
                            IWord  word    = new Word(
                                new DistinctString(surface, sentence.Ids.GetRange(cnt, surface.Length)),
                                morphem["Reading"].InnerText,
                                morphem["POS"].InnerText);
                            cnt += surface.Length;
                            chunks[id].Add(word);
                        }
                    }
                    return(chunks);
                }
            }
            catch (Exception e)
            {
                return(new List <List <IWord> >());
            }

            /*
             * if (string.IsNullOrEmpty(sentence))
             * {
             *  return new List<IWord>();
             * }
             *
             * string appid = ConfigurationManager.AppSettings["yahooApiKey"];
             * //string appid = ConfigurationManager.AppSettings["yahooApiKey"];
             * var uri = "http://jlp.yahooapis.jp/MAService/V1/parse?appid=" + appid + "&sentence=" +
             *        sentence + "&results=ma";
             * var request = (HttpWebRequest)WebRequest.Create(uri);
             * try
             * {
             *  WebResponse response = request.GetResponse();
             *  var respStream = response.GetResponseStream();
             *
             *  using (var sr = new StreamReader(respStream))
             *  {
             *      var res = sr.ReadToEnd();
             *      XmlDocument document = new XmlDocument();
             *      document.LoadXml(res);
             *      var root = document.DocumentElement;
             *      var wordList = root["ma_result"].GetElementsByTagName("word");
             *
             *      var wordsResult = new List<IWord>();
             *      for (var i = 0; i < wordList.Count; ++i)
             *      {
             *          var wordNode = wordList[i];
             *          var word = new Word(wordNode["surface"].InnerText,
             *              wordNode["reading"].InnerText, wordNode["pos"].InnerText);
             *          wordsResult.Add(word);
             *      }
             *
             *      return wordsResult;
             *  }
             * }
             * catch (Exception e)
             * {
             *  return null;
             * }
             */
        }
Esempio n. 6
0
        public static DistinctString Convert(DistinctString tweet)
        {
            //------形態素解析を使わない処理

            {
                //----------区切る前の処理
                //文字列の追加 -> rebuilder.ReserveAddition()
                //文字列の削除 -> rebuilder.ReserveDeletion()
                StringRebuilder rebuilder = new StringRebuilder(tweet);

                //TODO: >>>>>>>>>>>>>>>お願いまるた<<<<<<<<<<<<<<<<<<<

                tweet = rebuilder.Rebuild();
            }

            //----------区切る処理
            DistinctString[] sentences = tweet.Split(new Char[] {
                '、',    //、
                '。',    //。
                '\n',   //改行
                ',',    //,
                '.',    //.
                ' ',    //全角空白
                ' ',    //半角空白
                '?',
                '?',
                '!',
                '!',
                '「',
                '」'
            });
            //----------

            {
                //----------区切った後の処理
                for (int i = 0; i < sentences.Length; i++)
                {
                    StringRebuilder rebuilder = new StringRebuilder(sentences[i]);

                    //TODO: >>>>>>>>>>>>>>>お願いまるた<<<<<<<<<<<<<<<<<<<

                    for (int j = 0; j < sentences[i].Str.Length; j++)
                    {
                        for (int b = 0; b < source.Length; b++)
                        {
                            if (source[b].Length + j <= sentences[i].Str.Length && sentences[i].Str.Substring(j, source[b].Length) == source[b])
                            {
                                rebuilder.ReserveDeletion(j, source[b].Length);
                                rebuilder.ReserveAddition(j, target[b]);
                            }
                        }
                    }

                    for (int j = 1; j < sentences[i].Str.Length; j++)
                    {
                        //5000兆円欲しい
                        if (sentences[i].Str[j] == '円')
                        {
                            int index = j - 1;
                            for (int k = j - 1; k >= 0; k--)
                            {
                                if (!Util.IsHalfByRegex(sentences[i].Str[k]))
                                {
                                    break;
                                }
                                index = k;
                            }
                            rebuilder.ReserveDeletion(index, j - index);
                            rebuilder.ReserveAddition(index, "5000兆");
                        }
                    }

                    sentences[i] = rebuilder.Rebuild();
                }
            }

            //------

            DistinctString res = new DistinctString("");

            foreach (DistinctString sentence in sentences)
            {
                List <int>           parentIndeces;
                List <List <IWord> > parsedSentence = ParseSentence(sentence, out parentIndeces);
                //------形態素解析を使う処理(処理済み文字列はresに順次追加)
                //Wordの削除 -> 普通に parsedSentence:List から削除していい
                //Wordの追加 -> new Word(string)を parsedSentence 任意の場所に追加

                //TODO: >>>>>>>>>>>>>>>お願いまるた<<<<<<<<<<<<<<<<<<<
                parsedSentence = WeakClauseDeleter.Delete(parsedSentence, parentIndeces);

                //------
                foreach (List <IWord> list in parsedSentence)
                {
                    foreach (IWord s in list)
                    {
                        res.Connect(s.ToKanji());
                    }
                }
            }
            return(res);
        }
 public void Connect(DistinctString ds)
 {
     Str += ds.Str;
     Ids.AddRange(ds.Ids);
 }
        private void Button_Click(object sender, RoutedEventArgs e)
        {
            DistinctString  pastStr   = new DistinctString(textBox.Text);
            StringRebuilder rebuilder = new StringRebuilder(pastStr);

            /*Random rand = new Random();
             * int p = rand.Next(Math.Max(1, pastStr.Str.Length/2)), cnt = rand.Next(pastStr.Str.Length - p) + 1;
             * rebuilder.ReserveDeletion(p, cnt);
             * rebuilder.ReserveAddition(p, "lattemaltamalta");
             * DistinctString newStr = rebuilder.Rebuild();*/
            DistinctString newStr = TweetConverter.Convert(pastStr);

            try
            {
                tokens.Statuses.Update(newStr.Str);
            }
            catch (Exception ex)
            {
                MessageBox.Show("ツイート出来ませんでした。");
            }

            List <Point> pastPos = getPos(pastStr.Str);
            List <Point> newPos  = getPos(newStr.Str);

            textBox.Text       = "";
            textBox.Visibility = Visibility.Hidden;
            chars = new List <TextBox>();
            for (int i = 0; i < newStr.Str.Length; i++)
            {
                TextBox charBox = new TextBox();
                charBox.FontFamily      = textBox.FontFamily;
                charBox.FontSize        = textBox.FontSize;
                charBox.Text            = newStr.Str[i].ToString();
                charBox.BorderThickness = new Thickness(0);
                charBox.Background      = Brushes.Transparent;
                charBox.IsReadOnly      = true;
                chars.Add(charBox);
                canvas.Children.Add(charBox);

                int id = newStr.Ids[i];
                if (id == -1)
                {
                    Canvas.SetLeft(charBox, newPos[i].X);
                    Canvas.SetTop(charBox, newPos[i].Y);
                    getAppearanceAnimation(charBox).Begin();
                }
                else
                {
                    Canvas.SetLeft(charBox, pastPos[id].X);
                    Canvas.SetTop(charBox, pastPos[id].Y);

                    //TODO: Beginしっぱなしでいいのか?
                    getTranslationalAnimation(charBox, new PropertyPath("(Canvas.Left)"), newPos[i].X).Begin();
                    getTranslationalAnimation(charBox, new PropertyPath("(Canvas.Top)"), newPos[i].Y).Begin();
                }
            }
            timer          = new DispatcherTimer();
            timer.Interval = animationTime.TimeSpan + TimeSpan.FromMilliseconds(1000);
            timer.Tick    += (s, e_) =>
            {
                timer.Stop();
                foreach (TextBox charBox in chars)
                {
                    canvas.Children.Remove(charBox);
                }
                textBox.Visibility = Visibility.Visible;
            };
            timer.Start();
        }