I'm trying to count the number of words from a text file, namely this, to start.
我正在尝试从文本文件中计数单词的数量,也就是这个,开始。
This is a test of the word count program. This is only a test. If your program works successfully, you should calculate that there are 30 words in this file.
这是一个单词计数程序的测试。这只是一个测试。如果你的程序运行成功,你应该计算这个文件中有30个字。
I am using StreamReader to put everything from the file into a string, and then use the .Split method to get the number of individual words, but I keep getting the wrong value when I compile and run the program.
我使用StreamReader将文件中的所有内容放到一个字符串中,然后使用. split方法获取单个单词的数量,但是在编译和运行程序时,我总是得到错误的值。
using System;
using System.IO;
class WordCounter
{
static void Main()
{
string inFileName = null;
Console.WriteLine("Enter the name of the file to process:");
inFileName = Console.ReadLine();
StreamReader sr = new StreamReader(inFileName);
int counter = 0;
string delim = " ,.";
string[] fields = null;
string line = null;
while(!sr.EndOfStream)
{
line = sr.ReadLine();
}
fields = line.Split(delim.ToCharArray());
for(int i = 0; i < fields.Length; i++)
{
counter++;
}
sr.Close();
Console.WriteLine("The word count is {0}", counter);
}
}
5 个解决方案
#1
3
Try to use regular expression, e.g.:
尝试使用正则表达式,例如:
var count = Regex.Matches(input, @"\b\w+\b").Count();
#2
2
this should work for you:
这应该对你有用:
using System;
using System.IO;
class WordCounter
{
static void Main()
{
string inFileName = null;
Console.WriteLine("Enter the name of the file to process:");
inFileName = Console.ReadLine();
StreamReader sr = new StreamReader(inFileName);
int counter = 0;
string delim = " ,."; //maybe some more delimiters like ?! and so on
string[] fields = null;
string line = null;
while(!sr.EndOfStream)
{
line = sr.ReadLine();//each time you read a line you should split it into the words
line.Trim();
fields = line.Split(delim.ToCharArray(), StringSplitOptions.RemoveEmptyEntries);
counter+=fields.Length; //and just add how many of them there is
}
sr.Close();
Console.WriteLine("The word count is {0}", counter);
}
}
}
#3
1
A couple hints.
一些提示。
- What if you just have the sentence "hi" what would be your output?
- 如果你只有一句“嗨”,你的输出是什么?
- Your counter calculation is: from 0 through
fields.Length
, increment counter. How arefields.Length
and your counter related? - 您的计数器计算是:从0到字段。长度,增加计数器。如何字段。长度和你的柜台相关?
#4
0
you're probably getting a one off error, try something like this
你可能会得到一个1的误差,试试这个
counter = 0;
while(!sr.EndOfStream)
{
line = sr.ReadLine();
fields = line.Split(delim.ToCharArray());
counter += field.length();
}
there is no need to iterate over the array to count the elements when you can get the number directly
当您可以直接获得数值时,不需要对数组进行迭代以计数元素
#5
0
//Easy method using Linq to Count number of words in a text file
/// www.techhowdy.com
// Lyoid Lopes Centennial College 2018
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace FP_WK13
{
static class Util
{
public static IEnumerable<string> GetLines(string yourtextfile)
{
TextReader reader = new StreamReader(yourtextfile);
string result = string.Empty;
string line;
while ((line = reader.ReadLine()) != null)
{
yield return line;
}
reader.Close();
}
// Word Count
public static int GetWordCount(string str)
{
int words = 0;
string s = string.Empty;
var lines = GetLines(str);
foreach (var item in lines)
{
s = item.ToString();
words = words + s.Split(' ').Length;
}
return words;
}
}
}
#1
3
Try to use regular expression, e.g.:
尝试使用正则表达式,例如:
var count = Regex.Matches(input, @"\b\w+\b").Count();
#2
2
this should work for you:
这应该对你有用:
using System;
using System.IO;
class WordCounter
{
static void Main()
{
string inFileName = null;
Console.WriteLine("Enter the name of the file to process:");
inFileName = Console.ReadLine();
StreamReader sr = new StreamReader(inFileName);
int counter = 0;
string delim = " ,."; //maybe some more delimiters like ?! and so on
string[] fields = null;
string line = null;
while(!sr.EndOfStream)
{
line = sr.ReadLine();//each time you read a line you should split it into the words
line.Trim();
fields = line.Split(delim.ToCharArray(), StringSplitOptions.RemoveEmptyEntries);
counter+=fields.Length; //and just add how many of them there is
}
sr.Close();
Console.WriteLine("The word count is {0}", counter);
}
}
}
#3
1
A couple hints.
一些提示。
- What if you just have the sentence "hi" what would be your output?
- 如果你只有一句“嗨”,你的输出是什么?
- Your counter calculation is: from 0 through
fields.Length
, increment counter. How arefields.Length
and your counter related? - 您的计数器计算是:从0到字段。长度,增加计数器。如何字段。长度和你的柜台相关?
#4
0
you're probably getting a one off error, try something like this
你可能会得到一个1的误差,试试这个
counter = 0;
while(!sr.EndOfStream)
{
line = sr.ReadLine();
fields = line.Split(delim.ToCharArray());
counter += field.length();
}
there is no need to iterate over the array to count the elements when you can get the number directly
当您可以直接获得数值时,不需要对数组进行迭代以计数元素
#5
0
//Easy method using Linq to Count number of words in a text file
/// www.techhowdy.com
// Lyoid Lopes Centennial College 2018
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace FP_WK13
{
static class Util
{
public static IEnumerable<string> GetLines(string yourtextfile)
{
TextReader reader = new StreamReader(yourtextfile);
string result = string.Empty;
string line;
while ((line = reader.ReadLine()) != null)
{
yield return line;
}
reader.Close();
}
// Word Count
public static int GetWordCount(string str)
{
int words = 0;
string s = string.Empty;
var lines = GetLines(str);
foreach (var item in lines)
{
s = item.ToString();
words = words + s.Split(' ').Length;
}
return words;
}
}
}