刚刚自己实现了英语单词的查找功能,词义,音标,例句,自动纠正错误单词。

在此感谢华仔的核心算法,词典数据库我也是从网上下的。

下面这是完整的代码。由于时间仓促,可能有些地方写的不怎么优化。待以后完善,各位少侠有更好的主意请指点。

其实在这个程序中的主要算法里用到了一个"编辑距离"概念,在维基百科中是这样解释的:

编辑距离,又称Levenshtein距离,是指两个字串之间,由一个转成另一个所需的最少编辑操作次数。许可的编辑操作包括将一个字符替换成另一个字符,插入一个字符,删除一个字符。

例如将kitten一字转成sitting:

  1. sitten (k→s)
  2. sittin (e→i)
  3. sitting (→g)

当自己在写这个程序的时候,自己又在想,所谓的论文反抄袭的那个系统(具体术语我记不清了,大概就这个意思),它主要是以七个字为一个单元,进行计算,我按照我的思路想了一下,这个系统可以这样设计:将一片论文的所有字数分为:S = 7*n,同理将另一篇论文也这样(当然在这里自己电脑数据库里没有那么多的论文篇幅,只能将两篇进行比较),之后进行编辑距离的计算,然后统计每一个单元的编辑距离 L,最后计算相似度,当然这只是我的想法。具体的还要仔细的规划。

  1 using System;
  2 using System.Collections;
  3 using System.Collections.Generic;
  4 using System.Linq;
  5 using System.Text;
  6 using System.Data;
  7 using MySql.Data;
  8 using MySql.Data.MySqlClient;
  9 
 10 namespace GetDistense
 11 {
 12     class Program
 13     {
 14         static string fromPerson = null;
 15         public static int getDistance(string a,string b)
 16         {
 17             char[] arr1 = a.ToCharArray();
 18             char[] arr2 = b.ToCharArray();
 19             int[,] D = new int[arr1.Length + 1, arr2.Length + 1];
 20             for(int i = 0;i<=arr1.Length;i++)
 21             {
 22                 D[i, 0] = i;
 23             }
 24             for(int i = 0;i<=arr2.Length;i++)
 25             {
 26                 D[0, i] = i;
 27             }
 28             for(int i = 1;i<= arr1.Length;i++)
 29             {
 30                 for(int j = 1;j<=arr2.Length;j++)
 31                 {
 32                     D[i, j] = new int[]{
 33                         D[i - 1,j-1]+(arr1[i - 1] ==arr2[j - 1]?0:1),
 34                         D[i - 1,j]+1,
 35                         D[i,j-1]+1
 36                     }.Min();
 37                 }
 38             }
 39             return D[arr1.Length,arr2.Length];
 40         }
 41         
 42         public static void findLike()
 43         {
 44             string con = "Database = 'english';Data Source = 'localhost';User Id = 'root';Password = '123456'";
 45             using (MySqlConnection msc = new MySqlConnection(con))
 46             {
 47                 string cmdText = "select words,meaning from cetsix";
 48                 DataSet ds = new DataSet();
 49                 MySqlDataAdapter da = new MySqlDataAdapter(cmdText, msc);
 50                 da.Fill(ds);
 51                 DataTable dataTable = ds.Tables[0];
 52                 Console.WriteLine("您是否要找:");
 53                 for(int j = 0;j<dataTable.Rows.Count;j++)
 54                 {
 55                     int n = getDistance(fromPerson,dataTable.Rows[j][0].ToString());
 56                     if(n==1)
 57                     {
 58                         Console.Write(" 
"+dataTable.Rows[j][0].ToString()+" "+dataTable.Rows[j][1].ToString()+" 
");
 59                     }
 60                 }
 61                 Console.Write("继续查询请安1 退出按 0:");
 62                 
 63             }
 64         }
 65         static void Main(string[] args)
 66         {
 67            
 68         ID:
 69             string fromDB_Words = null, fromDB_meaning = null, fromDB_lx = null;
 70             Console.ForegroundColor = ConsoleColor.DarkCyan;
 71             Console.Write("请输入需要查询的单词:");
 72             fromPerson = Console.ReadLine();
 73             string con = "Database = 'english';Data Source = 'localhost';User Id = 'root';Password = '123456'";
 74             using (MySqlConnection msc = new MySqlConnection(con))
 75             {
 76                 string cmdText = string.Format("select words,meaning,lx from cetsix where words = '{0}'", fromPerson);
 77                 MySqlCommand cmd = new MySqlCommand(cmdText, msc);
 78                 msc.Open();
 79                 MySqlDataReader reader = cmd.ExecuteReader();
 80                 if (reader.HasRows)
 81                 {
 82                     while (reader.Read())
 83                     {
 84                         fromDB_Words = reader["words"].ToString();
 85                         fromDB_meaning = reader["meaning"].ToString();
 86                         fromDB_lx = reader["lx"].ToString();
 87                     }
 88                     reader.Close();
 89                     Console.ForegroundColor = ConsoleColor.Yellow;
 90                     Console.WriteLine("
单词:{0}
意思:{1}
例句:{2}", fromDB_Words, fromDB_meaning, fromDB_lx);
 91                     Console.Write("继续查询请安1 退出按 0:");
 92                     int i = int.Parse(Console.ReadLine());
 93                     Console.Clear();
 94                     if (i == 1)
 95                     {
 96                         goto ID;
 97                     }
 98                     else
 99                     {
100                         Environment.Exit(0);
101                     }
102 
103                 }
104                 else
105                 {
106                     Console.ForegroundColor = ConsoleColor.Cyan;
107                     Console.Write("
并没有找到您所需要的单词
╭(╯3╰)╮
继续查询请安1 退出按 0:");
108                     Console.WriteLine();
109                     findLike();
110                     int i = int.Parse(Console.ReadLine());
111                     Console.Clear();
112                     if (i == 1)
113                     {
114                         goto ID;
115                     }
116                     else
117                     {
118                         Environment.Exit(0);
119                     }
120                 }
121             }
122             Console.ReadKey();
123         }
124     }
125 }
View Code

 运行图片:

原文地址:https://www.cnblogs.com/struCoder/p/3462357.html