C#识别文字内容并分类输出0和1
这个代码最开始是为了处理在调查问卷的结果,问卷星等调查问卷有一部分是用汉字描述的多选题,问卷系统在输出问卷的时候直接就是将汉字进行输出,而我们在进行数据分析(二元logistic或多项logistics)的时侯需将每个字符串拆开,这就需要对每个字符串进行比较输入。
这段代码最核心的就是IndexOf的使用,为进行字符串的比较。
namespace SInmul { public class Mulity { private int h, o; public int m, n; public string[] b; public string[] Arrm; public void FORM() { b = new string[20]; for (int h = 0; h < m; h++) { b[h] = Console.ReadLine(); } } public void FORINM() { Arrm = new string[1000]; for (h = 0; h < n; h++) { Arrm[h] = Console.ReadLine(); } } public void MUL() { Console.WriteLine("结果为:"); for (o = 0; o < n; o++) { for (h = 0; h< m; h++) { var index = Arrm[o].IndexOf(b[h]); if (index != -1) Console.WriteLine(h); }; }; } } public class Single { private int i,j; public int k, l; public string[] a ; public string[] Arr; public void FOR() { a=new string[20]; for (i = 0; i < l; i++) a[i] = Console.ReadLine(); } public void FORIN() { Arr = new string[1000]; for (j = 0; j < k; j++) Arr[j] = Console.ReadLine(); } public void ONE() { for (i = 0; i < l; i++) { Console.WriteLine("第{0}组", i + 1); for (j = 0; j < k; j++) { var index = Arr[j].IndexOf(a[i]); if (index != -1) Console.WriteLine("1"); else Console.WriteLine("0"); } } } } internal class Program { static void Main(string[] args) { int c; Single single = new Single(); Mulity mulity = new Mulity(); Console.WriteLine("需要处理的是二元logistic还是多元?二元请输入1"); c = Convert.ToInt32(Console.ReadLine()); Console.WriteLine("数据的总量为"); if (c == 1) { single.k = Convert.ToInt32(Console.ReadLine()); Console.WriteLine("请输入要比较的字符数量:"); single.l = Convert.ToInt32(Console.ReadLine()); Console.WriteLine("请输入要比较的字符:"); single.FOR(); Console.WriteLine("please input the data:"); single.FORIN(); single.ONE(); } else { mulity.n = Convert.ToInt32(Console.ReadLine()); Console.WriteLine("请输入要比较的字符数量:"); mulity.m = Convert.ToInt32(Console.ReadLine()); Console.WriteLine("请输入要比较的字符:"); mulity.FORM(); Console.WriteLine("please input the data:"); mulity.FORINM(); mulity.MUL(); } Console.ReadKey(); }
输出示例:
需要处理的是二元logistic还是多元?二元请输入1 1 数据的总量为 6 请输入要比较的字符数量: 3 请输入要比较的字符: 对 false 随便 please input the data: 对 错 true false 随便 可能对 第1组 1 0 0 0 0 1 第2组 0 0 0 1 0 0 第3组 0 0 0 0 1 0
在这个示例中,也可以比较多项,这个代码的main函数部分确实十分粗糙,我自己也看的有点难受,但还是能有效解决问题的。
P.S.用C#是因为vs可以输入中文,之前用的dev没办法用中文输入输出,很麻烦。
做SPSS分析的同学可以直接拿去用,分类确实方便很多。

浙公网安备 33010602011771号