电话号码对应的英文单词 手机数字短信翻译小工具 数字输入法初型 需求“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); } } } }
实际运行效果如下:
相关文章推荐
- 编程之美:电话号码对应英文单词( 程序实现)
- Java实现将数字日期翻译成英文单词的工具类实例
- PAT程序设计考题——甲级1005 数字按位累加(非负数字按位累加和转化成对应英文单词)
- 英文数字写法翻译阿拉伯数字 [java实现]
- 程序实现发送手机短信的例子
- 编程题:输入英文月份单词,输出对应月的数字形式。
- 编程之美题之电话号码对应英文单词
- java实现手机短信验证的基本思路
- 电话号码对应英文单词
- 编程之美--3.2电话号码对应英文单词
- java实现的一个发送手机短信的程序
- 每天一个小程序之python实现统计英文单词出现个数
- 编程之美-电话号码对应的英文单词
- python实现将英文单词表示的数字转换成阿拉伯数字的方法
- C现代实现方法 数字转换为英文单词
- 读入两个小于100的正整数A和B,计算A+B.A和B的每一位数字由对应的英文单词给出.
- 《编程之美》 - 3.2 电话号码对应英文单词 (发现书上递归解法的错误)
- 汇编程序:输出对应的英文单词
- 实现一个控制台程序,给定一段英文字符串,统计其中各个英文单词(4字符以上含4字符)的出现频率。
- 【小程序】JAVA实现从10~50中随机生成50个数,统计出现的数字及次数,输出出现最多的次数及对应的数字,按数字升序排列。