从句子中删除帮助词

时间:2015-11-25 08:19:23

标签: c# wpf

我制作了一个简单的程序,其中写了一个短语,它显示了与单个单词匹配的视频。让我说我进入了#34;我去了学校"。在这里,它应该从句子中删除单词"并返回三个单词。 这是我试过的代码!!它工作正常,但当我输入一些短语时,它删除了帮助动词,除此之外,它还替换了一个产生问题的空字符串。请提出任何建议

代码

 class MyPlayer
        {
        string complete_name;
        string root;
        string[] supportedExtensions;
        string videoname;
         public MyPlayer(string snt)
            {
                videoname = snt;
            }
        public List<VideosDetail> test()
            {
                complete_name = videoname.ToLower() + ".wmv";
                root = System.IO.Path.GetDirectoryName(@"C:\Users\Administrator\Desktop\VideosFrame\VideosFrame\Model\");
              supportedExtensions = new[] { ".wmv" };
                var files = Directory.GetFiles(Path.Combine(root, "Videos"), "*.*").Where(s => supportedExtensions.Contains(Path.GetExtension(s).ToLower()));

            List<VideosDetail> videos = new List<VideosDetail>();
            VideosDetail id;
            bool flagefilefound = false;
            foreach (var file in files)
                {
             id = new VideosDetail()
                    {

                        Path = file,
                        FileName = Path.GetFileName(file),
                        Extension = Path.GetExtension(file)
                    };
                    FileInfo fi = new FileInfo(file);
                 if (id.FileName == complete_name)
                    {

                        id.FileName = fi.Name;
                        id.Size = fi.Length;
                        videos.Add(id);
                        flagefilefound = true;
                    }


                    if (flagefilefound)
                        break;
                }

                if (!flagefilefound)
                {
                   MessageBox.Show("no such video is available. ");
                }
               return  videos;
            }

        }

        private void play_Click(object sender, RoutedEventArgs e)
        {
            List<string> chk = new List<string>();
            chk.Add("is");
            chk.Add("am");
            chk.Add("are");
            chk.Add("were");
            chk.Add("was");
            chk.Add("do");
            chk.Add("does");
            chk.Add("has");
            chk.Add("have");

            chk.Add("an");
            chk.Add("the");
            chk.Add("to");
            chk.Add("of");
           string sen = vdo.Text;
           List<string> tmp = new List<string>();
            string[] split = sen.Split(' ');
            foreach (var item in split)
            {
                tmp.Add(item);
            }
            foreach (var item in chk)
            {
                if( sen.Contains(item) )
                {
                    int index = sen.IndexOf(item);
                    sen = sen.Remove(index,item.Length);
                };

            }
         foreach (var i in tmp)
            {

                MyPlayer player = new MyPlayer(i);
                VideoList.ItemsSource = player.test();

            }

        }

1 个答案:

答案 0 :(得分:3)

你真正在做的是消除所谓的停用词,并且可能创建词袋

private static HashSet<String> s_StopWords = 
  new HashSet<String>(StringComparer.OrdinalIgnoreCase) {
    "is", "am", "are", "were", "was", "do", "does", "to", "from", // etc.
};

private static Char[] s_Separators = new Char[] {
  '\r', '\n', ' ', '\t', '.', ',', '!', '?', '"', //TODO: check this list 
};

...

String source = "I go to school";

// ["I", "go", "school"] - "to" being a stop word is removed
String[] words = source
  .Split(s_Separators, StringSplitOptions.RemoveEmptyEntries)
  .Where(word => !s_StopWords.Contains(word))
  .ToArray();

// Combine back: "I go school"
String result = String.Join(" ", words);