从txtfile读取时,我的循环忽略一些匹配值 我想做的是从视图上传文件后立即读取文件。但是在将其返回到视图之前,我需要在txtfile中找到最常用的单词并对其进行操作。
请记住,C#是我的新手,所以我想我的代码不太漂亮!
[HttpPost]
public IActionResult UploadFile(IFormFile file)
{
var result = string.Empty;
using (var reader = new StreamReader(file.OpenReadStream()))
{
result = reader.ReadToEnd();
}
List<string> fileContent = string.IsNullOrEmpty(result)
? null
: result.Split(' ', '.', ',', ';', '\\').ToList();
return View("../Home/Index", findMostUsedWord(fileContent));
}
public string findMostUsedWord(List<string> fileContent)
{
fileContent = fileContent.Where(str => !string.IsNullOrWhiteSpace(str)).ToList();
fileContent = fileContent.Where(str => !string.IsNullOrEmpty(str)).ToList();
fileContent = fileContent.Where(str => str.Length >= 4).ToList();
var mostUsedWord = fileContent.GroupBy(item => item).OrderByDescending(g => g.Count()).Select(g => g.Key).First();
return processMostUsedWord(mostUsedWord, fileContent);
}
public string processMostUsedWord(string mostUsedWord, List<string> fileContent)
{
int wordCount = 0;
for (int i = 0; i < fileContent.Count(); i++)
{
Debug.WriteLine(fileContent[i].ToLowerInvariant());
if (fileContent[i].ToLowerInvariant() == mostUsedWord.ToLowerInvariant())
{
fileContent[i] = "foo" + fileContent[i] + "bar";
Debug.WriteLine("FOUND MATCH");
wordCount++;
}
}
ViewData["mostUsedWord"] = "The most common word is " + mostUsedWord + " and it appears " + wordCount + " times";
return prettyPrint(fileContent);
}
public string prettyPrint(List<string> fileContent)
{
StringBuilder sb = new StringBuilder();
for (int i = 0; i < fileContent.Count(); i++)
{
sb.Append(fileContent[i] + " ");
}
return sb.ToString();
}
我知道预期的输出应该是88,但我只得到73个匹配项
有什么想法吗?
答案 0 :(得分:1)
您可以通过执行以下操作来简化所有代码(并使用不区分大小写的比较):
// Split the file text on whitespace and punctuation characters
var fileContent = File
.ReadAllText(@"f:\private\temp\aladdin.txt")
.Split(new[] {' ', '\r', '\n', '\t', '.', ',', ';', '\'', '\\', '/'},
StringSplitOptions.RemoveEmptyEntries)
.Where(str => str.Length > 3);
// Get the word and it's count in an anonymous object
var mostUsedWord = fileContent
.GroupBy(item => item, StringComparer.OrdinalIgnoreCase) // case-insensitive compare
.OrderByDescending(g => g.Count())
.Select(g => new {Word = g.Key, Count = g.Count()}) // select the word and it's count
.First();
ViewData["mostUsedWord"] = $"The most common word is '{mostUsedWord.Word}', " +
$"which appears {mostUsedWord.Count} times";
// Put the list of strings together, joining them with a space character (prettyPrint):
return string.Join(" ", fileContent);