[英]Stopwords removing from string
我正在尝试从字符串中删除停用词,但是问题是,如果它再次出现在字符串中,则是从单个单词中删除字符。
例如,原始字符串是:“ 这部电影不错”。 结果字符串是:“ 这部电影很好。 ”。 工作正常。但是
如果字符串是:“ 这部电影很好。 ”
那么结果字符串将是:“ th movie good”。
正如在这个字符串,所以它在结果免除重复。
另一个说法是:“ 这个游戏太棒了。所以,我看了很多次。 ”
结果:“ gme fntstic。因此,拉伸了很多。 ”
作为此字符串的重复,因此,结果字符串显示了所有排除a的单词。
我在唱这段代码:
List<string> stopWordsList = new List<string>();
stopWordsList = stopWordsFilter();//funtion returning the list of stop words taking from file.
string propertyValue = "this game is fantastic. So, I watched and played a lot.";
foreach (string word1 in propertyValue.Split(' '))
{
foreach ( var word in stopWordsList)
{
if (word.Equals(word1) && word.Length == word1.Length)
{
propertyValue = propertyValue.Replace(word, "");
}
}
}
Console.WriteLine(propertyValue);
问题是您将停用词替换为String.Empty
。 String.Replace
不在乎单词而是子字符串。
您可以使用这种方法:
string propertyValue = "this game is fantastic. So, I watched and played a lot.";
var words = propertyValue.Split();
var newWords = words.Except(stopWordsFilter);
propertyValue = string.Join(" ", newWords);
如果您想忽略这种情况,请也省略"Is"
:
var newWords = words.Except(stopWordsFilter, StringComparer.InvariantCultureIgnoreCase);
我在这里提出使用linq的解决方案:
string result = propertyValue.Split(' ')
.Where(s => !stopWordsList.Contains(s))
.Aggregate((current, next) => current + " " + next);
Console.WriteLine(result);
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.