[英]Inputs a sentence from user, determines and displays nonduplicate words in alphabetical order
[英]Removing the helping words from sentence
我編寫了一個簡單的程序,其中寫了一個短語,並顯示匹配單個單詞的視頻。假設我輸入了“我去上學”。 在這里,它應該從句子中刪除單詞“ to”,並僅返回三個單詞。 這是我嘗試過的代碼! 它可以正常工作,但是當我輸入一些短語時,它會刪除幫助動詞,除此之外,它還會替換一個空字符串,這會造成問題。 有什么建議嗎
碼
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();
}
}
您實際上正在做的是消除所謂的停用詞 ,並可能創建一堆單詞 :
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);
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.