您的位置:首页 > 其它

电话号码对应的英文单词 手机数字短信翻译小工具 数字输入法初型 需求“ya tou”咋写程序实现翻译成“丫头”,有哪位有思路的请指教。

2010-04-21 22:38 741 查看
   在看“编程之美”的电话号码对应的英文单词那章时,想起了大学的时候有时候会收到数字短信如“43 43”,其实翻译成汉字就是“呵呵”的意思,但你得凭借着你的大脑去分析,所以我花了点时间写了个小程序,完成了“43 43”到“he he”的转化工作,至于更进一步的“he he”到“呵呵”的代码还没有写,觉得后面的程序会涉及到输入法的一些知识了,自己不是很熟悉了,现在把我的代码copy如下:

   
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using System.Text.RegularExpressions;
namespace ExamExplainPhoneShortMessage
{
public partial class Form1 : Form
{
#region   属性
/// <summary>
/// 0-9 每个数字包含的字符数
/// </summary>
int[] charCountOfNumber = { 0, 0, 3, 3, 3, 3, 3, 4, 3, 4 };
/// <summary>
/// 有效的数字
/// </summary>
int[] validNumber = { 0, 1, 2, 3, 4, 5, 6, 7, 8, 9 };
/// <summary>
/// 0-9 每个数字可以表示的字母
/// </summary>
string[] charValueOfNumber = { "", "", "abc", "def", "ghi", "jki", "mno", "pqrs", "tuv", "wxyz" };

/// <summary>
/// 所有  有效的拼音组合
/// </summary>
IList<string> pinyinTable = new List<string>();
DataTable dt = new DataTable();
/// <summary>
/// 测试用例
/// 结果为 "ya tou"
/// </summary>
string strTestText = "92 868";
/// <summary>
/// 无效拼音
/// </summary>
string blackStr = "null";
#endregion
public Form1()
{
InitializeComponent();
initPinyinTable();
}
/// <summary>
/// Inits the pinyin table.
/// </summary>
private void initPinyinTable()
{
string filePath = Environment.CurrentDirectory + @"/pinyinTable.txt";
System.IO.StreamReader streamReader = new System.IO.StreamReader(filePath, System.Text.Encoding.GetEncoding("gb2312"));
while (true)
{
string strLine = streamReader.ReadLine();
if (string.IsNullOrEmpty(strLine))
{
break;
}
string[] strs = strLine.Split("/t".ToCharArray());
foreach (string item in strs)
{
if (string.IsNullOrEmpty(item) == false && "".Equals(item) == false)
{
pinyinTable.Add(item);
}
}
}
}
/// <summary>
/// Gets the probable spell from number.
/// </summary>
/// <param name="strLine">The STR line.</param>
/// <returns></returns>
private IList<string> GetProbableSpellFromNumber(string strLine)
{
IList<string> probableResults = new List<string>();
if (strLine.Contains("0") || strLine.Contains("1"))
{
return probableResults;
}
strLine.Replace("  ", " ");
strLine.Replace("/t", " ");
strLine.Replace(",", " ");
// 正则表达式验证 字符串串中是否有非法字符(排除空格在外)
// 暂时没写
string[] words = strLine.Split(" ".ToCharArray());
IList<IList<string>> allWordCharCollection = new List<IList<string>>();
foreach (string word in words)
{
IList<string> wordCharCollection = getWordAllCharCollection(word);
allWordCharCollection.Add(wordCharCollection);
}
int totalCombination = 1;
foreach (IList<string> item in allWordCharCollection)
{
if (item.Count > 0)
{
totalCombination = totalCombination * item.Count;
}
}
StringBuilder[] args = new StringBuilder[totalCombination];
for (int i = 0; i < totalCombination; i++)
{
args[i] = new StringBuilder("");
}
foreach (IList<string> item in allWordCharCollection)
{
if (item.Count > 0)
{
for (int i = 0; i < totalCombination; i++)
{
int index = i % item.Count;
args[i].Append(item[index]);
args[i].Append(" ");
}
}
}
// 显示可能的结果组合
string[] strArgs = new string[totalCombination];
for (int i = 0; i < totalCombination; i++)
{
strArgs[i] = args[i].ToString();
}
this.richTextBox.Lines = strArgs;
return probableResults;
}
/// <summary>
///获得一个数字的  所有字母组合
/// </summary>
/// <param name="word">The word.</param>
private IList<string> getWordAllCharCollection(string word)
{
int n = word.Length;
int[] number = new int
;
for (int i = 0; i < n; i++)
{
number[i] = Int32.Parse(word[i].ToString());
}
int[] answer = { 0, 0, 0, 0 };
// 返回的结果
IList<string> wordCharCollection = new List<string>();
while (true)
{
StringBuilder strBuilder = new StringBuilder("");
for (int i = 0; i < n; i++)
{
strBuilder.Append(charValueOfNumber[number[i]][answer[i]].ToString());
}
// 一个有效的拼音
if (pinyinTable.Contains(strBuilder.ToString()))
{
wordCharCollection.Add(strBuilder.ToString());
}
int index = n - 1;
while (index >= 0)
{
if (answer[index] < charCountOfNumber[number[index]] - 1)
{
answer[index]++;
break;
}
else
{
answer[index] = 0;
index--;
}
}
if (index < 0)
break;
}

return wordCharCollection;
}
/// <summary>
/// Gets the probable chinese chars from spells.
/// </summary>
/// <param name="spells">The spells.</param>
/// <returns></returns>
private IList<string> GetProbableChineseCharsFromSpells(IList<string> spells)
{
IList<string> ProbableChineseChars = new List<string>();
return ProbableChineseChars;
}
/// <summary>
/// Gets the probable chinese spell through filter all spells.
/// </summary>
/// <param name="spells">The spells.</param>
/// <returns></returns>
private IList<string> getProbableChineseSpellThroughFilterAllSpells(IList<string> spells)
{
IList<string> ProbableChineseChars = new List<string>();

ProbableChineseChars = spells;
return ProbableChineseChars;
}
/// <summary>
///  Number-ChineseSpell
/// </summary>
/// <param name="sender">The source of the event.</param>
/// <param name="e">The <see cref="System.EventArgs"/> instance containing the event data.</param>
private void button3_Click(object sender, EventArgs e)
{
try
{
strTestText = this.txtNumber.Text.Trim();
IList<string> allSpells = GetProbableSpellFromNumber(strTestText);
}
catch (Exception ex)
{
MessageBox.Show("程序出现异常:"+ex.Message, "提示",MessageBoxButtons.OK,MessageBoxIcon.Error);
}
}
}
} 


实际运行效果如下:

  
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息