استفاده از پیکره متنی برای خطایابی متن فارسی - هفت خط کد انجمن پرسش و پاسخ برنامه نویسی

استفاده از پیکره متنی برای خطایابی متن فارسی

0 امتیاز
با سلام

   یک سری متن فارسی دارم(بصورت تعدادی فایل ورد) ، یک برنامه WinForm C# .net2  باید بنویسم که با استفاده از پیکره متنی و.. متن را خطایابی و غلط گیری کند، یه برنامه مثل افزونه ویراستیار در ورد اما بصورت وین فرم

    نمیدونم چجوری باید از پیکره متنی استفاده کنم و یا چجوری بحث های غلط یابی را انجام بدهم؟

   لطفا راهنمایی بفرمایید

با تشکر
سوال شده آبان 19, 1396  بوسیله ی FirstLine (امتیاز 9)   1 2

1 پاسخ

+1 امتیاز

جهت خواندن فایل ورد از کد زیر استفاده کنید.

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Runtime.InteropServices.ComTypes;
 
namespace readDOC
{
    class Program
    {
        static void Main(string[] args)
        {
            Microsoft.Office.Interop.Word.Application word = new Microsoft.Office.Interop.Word.Application();
            object miss = System.Reflection.Missing.Value;
            object path = @"C:\DOC\myDocument.docx";
            object readOnly = true;
            Microsoft.Office.Interop.Word.Document docs = word.Documents.Open(ref path, ref miss, ref readOnly, ref miss, ref miss, ref miss, ref miss, ref miss, ref miss, ref miss, ref miss, ref miss, ref miss, ref miss, ref miss, ref miss);
            string totaltext = "";
            for (int i = 0; i < docs.Paragraphs.Count; i++)
            {
                   totaltext += " \r\n "+ docs.Paragraphs[i+1].Range.Text.ToString();
            }
            Console.WriteLine(totaltext);
            docs.Close();
            word.Quit();
        }
    }
}

 

 

واسه check spelling هم از کد زیر استفاده کنید:

using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Text.RegularExpressions;

namespace SpellingCorrector
{
  	/// <summary>
	/// Conversion from http://norvig.com/spell-correct.html by C.Small
	/// </summary>
	public class Spelling
	{
		private Dictionary<String, int> _dictionary = new Dictionary<String, int>();
		private static Regex _wordRegex = new Regex("[a-z]+", RegexOptions.Compiled);

		public Spelling()
		{
			string fileContent = File.ReadAllText("big.txt");
			List<string> wordList = fileContent.Split(new string[] {"\n"} , StringSplitOptions.RemoveEmptyEntries).ToList();

			foreach (var word in wordList)
			{
				string trimmedWord = word.Trim().ToLower();
				if (_wordRegex.IsMatch(trimmedWord))
				{
					if (_dictionary.ContainsKey(trimmedWord))
						_dictionary[trimmedWord]++;
					else
						_dictionary.Add(trimmedWord, 1);
				}
			}
		}

		public string Correct(string word)
		{
			if (string.IsNullOrEmpty(word))
				return word;
			
			word = word.ToLower();

			// known()
			if (_dictionary.ContainsKey(word))
				return word;

			List<String> list = Edits(word);
			Dictionary<string, int> candidates = new Dictionary<string, int>();

			foreach (string wordVariation in list)
			{
				if (_dictionary.ContainsKey(wordVariation) && !candidates.ContainsKey(wordVariation))
					candidates.Add(wordVariation, _dictionary[wordVariation]);
			}

			if (candidates.Count > 0)
				return candidates.OrderByDescending(x => x.Value).First().Key;

			// known_edits2()
			foreach (string item in list)
			{
				foreach (string wordVariation in Edits(item))
				{
					if (_dictionary.ContainsKey(wordVariation) && !candidates.ContainsKey(wordVariation))
						candidates.Add(wordVariation, _dictionary[wordVariation]);
				}
			}

			return (candidates.Count > 0) ? candidates.OrderByDescending(x => x.Value).First().Key : word;
		}

		private List<string> Edits(string word)
		{
			var splits = new List<Tuple<string, string>>();
			var transposes = new List<string>();
			var deletes = new List<string>();
			var replaces = new List<string>();
			var inserts = new List<string>();

			// Splits
			for (int i = 0; i < word.Length; i++)
			{
				var tuple = new Tuple<string, string>(word.Substring(0, i), word.Substring(i));
				splits.Add(tuple);
			}

			// Deletes
			for (int i = 0; i < splits.Count; i++)
			{
				string a = splits[i].Item1;
				string b = splits[i].Item2;
				if (!string.IsNullOrEmpty(b))
				{
					deletes.Add(a + b.Substring(1));
				}
			}

			// Transposes
			for (int i = 0; i < splits.Count; i++)
			{
				string a = splits[i].Item1;
				string b = splits[i].Item2;
				if (b.Length > 1)
				{
					transposes.Add(a + b[1] + b[0] + b.Substring(2));
				}
			}

			// Replaces
			for (int i = 0; i < splits.Count; i++)
			{
				string a = splits[i].Item1;
				string b = splits[i].Item2;
				if (!string.IsNullOrEmpty(b))
				{
					for (char c = 'a'; c <= 'z'; c++)
					{
						replaces.Add(a + c + b.Substring(1));
					}
				}
			}

			// Inserts
			for (int i = 0; i < splits.Count; i++)
			{
				string a = splits[i].Item1;
				string b = splits[i].Item2;
				for (char c = 'a'; c <= 'z'; c++)
				{
					inserts.Add(a + c + b);
				}
			}

			return deletes.Union(transposes).Union(replaces).Union(inserts).ToList();
		}
	}
}


class Program
{
	static void Main(string[] args)
	{
		Spelling spelling = new Spelling();
		string word = "";

		word = "speling"; 
		Console.WriteLine("{0} => {1}", word, spelling.Correct(word));

		word = "korrecter"; // 'correcter' is not in the dictionary file so this doesn't work
		Console.WriteLine("{0} => {1}", word, spelling.Correct(word));

		word = "korrect";
		Console.WriteLine("{0} => {1}", word, spelling.Correct(word));

		word = "acess";
		Console.WriteLine("{0} => {1}", word, spelling.Correct(word));

		word = "supposidly";
		Console.WriteLine("{0} => {1}", word, spelling.Correct(word));

		// A sentence
		string sentence = "I havve speled thes woord wwrong"; // sees speed instead of spelled (see notes on norvig.com)
		string correction = "";
		foreach (string item in sentence.Split(' '))
		{
			correction += " " +spelling.Correct(item);
		}
		Console.WriteLine("Did you mean:" + correction);

		Console.Read();
	}
}

 

پاسخ داده شده آبان 19, 1396 بوسیله ی farnoosh (امتیاز 8,362)   20 44 59
با سلام
   از راهنمایی و لطف شما سپاسگزارم
   من باید از پیکره متنی همشهری یا بی جن خان (البته پیکره متنی کاملش نیازی نیست ولی باید از یکی از این دو پیکره حتما استفاده کنم) برای خطایابی متن فارسی در برنامه  استفاده کنم
   لطفا  
با تشکر
...