2012-09-11 32 views
8

我正在創建將文本轉換爲盲文的應用程序。轉換爲盲文不是問題,但我不知道如何將其轉換回來。替換特定字符後的字符數

實施例1:將數字轉換盲文

1  = #a 
123 = #abc 
12 45 = #ab #de 

實施例2:轉換大寫盲文

Jonas = ,jonas 
JONAS = ,,jonas 

我有一個問題轉換盲文恢復正常。我不能只將每個a轉換爲1等等。數字可以通過#進行檢查,然後將字符更改爲下一個空格,但我不知道如何。信件前的逗號更難與文本中的其他逗號分開。

這裏是我轉換成盲文類:

using System; 
using System.Collections.Generic; 
using System.Text; 
using System.Drawing; 

namespace BrailleConverter 
{ 
    class convertingBraille 
    { 
     public Font getIndexBrailleFont() 
     { 
      return new Font("Index Braille Font", (float)28.5, FontStyle.Regular); 
     } 

     public Font getPrintableFontToEmbosser() 
     { 
      return new Font("Lucida Console", (float)28.5, FontStyle.Regular); 
      //return new Font("Index Black Text Font", (float)28.5, FontStyle.Regular); 
     } 

     public string convertCapitalsToUnderscore(string text) 
     { 
      if (string.IsNullOrEmpty(text)) 
      { 
       return ""; 
      } 

      text = " " + text; 

      text = text.Replace('.', '\''); 
      text = text.Replace(',', '1'); 
      text = text.Replace('?', '5'); 
      text = text.Replace('!', '6'); 
      text = text.Replace(':', '3'); 
      text = text.Replace('=', '7'); 
      text = text.Replace('+', '4'); 
      text = text.Replace('*', '9'); 
      text = text.Replace('é', '='); 

      StringBuilder newText = new StringBuilder(text.Length * 2); 
      newText.Append(text[0]); 

      bool firstCapLetterInWord = true; 

      for (int i = 1; i < text.Length; i++) 
      { 
       char letter = text[i]; // Aktuell bokstav 
       char nextLetter = ' '; // Nästa bokstav 

       try 
       { 
        nextLetter = text[i + 1]; 
       } 
       catch 
       { 

       } 

       // Är det stor bokstav? 
       if (char.IsUpper(letter)) 
       { 
        // Är nästa bokstav stor? 
        if (char.IsUpper(nextLetter)) 
        { 
         // Är det början av ett helt ord med caps? 
         if (firstCapLetterInWord) 
         { 
          newText.Append(",,"); // 2 st understräck framför ordet 

          firstCapLetterInWord = false; // Ändra så att inte nästa bokstav får 2 st understräck 
         } 
        } 
        else // Annars bara ett understräck 
        { 
         if (firstCapLetterInWord) 
         { 
          newText.Append(","); // Sätt understräck framför bokstav 
         } 

         firstCapLetterInWord = true; // Förbereda för nästa capsord 
        } 
       } 

       newText.Append(text[i]); 
      } 

      string finishedText = newText.ToString().TrimStart(); // Ta bort mellanslaget i början 

      finishedText = finishedText.ToLower(); 

      finishedText = finishedText.Replace('å', '*'); 
      finishedText = finishedText.Replace('ä', '>'); 
      finishedText = finishedText.Replace('ö', '['); 

      return finishedText; 
     } 

     public string convertNumbersToBrailleNumbers(string text) 
     { 
      if (string.IsNullOrEmpty(text)) 
      { 
       return ""; 
      } 

      text = " " + text; 

      StringBuilder newText = new StringBuilder(text.Length * 2); 
      newText.Append(text[0]); 

      bool firstNumberInNumber = true; 

      for (int i = 1; i < text.Length; i++) 
      { 
       char letter = text[i]; // Aktuell tecken 
       char nextLetter = ' '; // Nästa tecken 

       try 
       { 
        nextLetter = text[i + 1]; 
       } 
       catch 
       { 

       } 

       char convertedChar = text[i]; 

       // Är tecknet en siffra? 
       if (char.IsNumber(letter)) 
       { 
        // Är nästa tecken en siffra? 
        if (char.IsNumber(nextLetter)) 
        { 
         // Är det början av ett flertaligt nummer? 
         if (firstNumberInNumber) 
         { 
          newText.Append('#'); // Brädkors framför nummret 

          firstNumberInNumber = false; // Ändra så att inte nästa siffra får brädkors 
         } 
        } 
        else // Annars bara ett understräck 
        { 
         if (firstNumberInNumber) 
         { 
          newText.Append('#'); // Sätt brädkors framför siffran 

         } 

         firstNumberInNumber = true; // Förbereda för nästa flertaliga nummer 
        } 
       } 

       newText.Append(convertedChar); 
      } 

      string finishedText = newText.ToString().TrimStart(); 

      finishedText = finishedText.Replace('1', 'a'); 
      finishedText = finishedText.Replace('2', 'b'); 
      finishedText = finishedText.Replace('3', 'c'); 
      finishedText = finishedText.Replace('4', 'd'); 
      finishedText = finishedText.Replace('5', 'e'); 
      finishedText = finishedText.Replace('6', 'f'); 
      finishedText = finishedText.Replace('7', 'g'); 
      finishedText = finishedText.Replace('8', 'h'); 
      finishedText = finishedText.Replace('9', 'i'); 
      finishedText = finishedText.Replace('0', 'j'); 

      return finishedText; 
     } 

     public string convertBackToPrint(string oldText) 
     { 
      string newText = oldText.Replace(",", ""); 
      newText = newText.Replace("#", ""); 
      newText = newText.Replace("*", "å"); 
      newText = newText.Replace(">", "ä"); 
      newText = newText.Replace("[", "ö"); 
      newText = newText.Replace('\'', '.'); 
      newText = newText.Replace('1', ','); 
      newText = newText.Replace('5', '?'); 
      newText = newText.Replace('6', '!'); 
      newText = newText.Replace('3', ':'); 
      newText = newText.Replace('7', '='); 
      newText = newText.Replace('4', '+'); 
      newText = newText.Replace('9', '*'); 
      newText = newText.Replace('=', 'é'); 

      return newText; 
     } 
    } 
} 
+0

你能粘貼示例代碼?也是一個完整的陳述不是單個詞的例子。 – VIRA

+4

請原諒我的無知,但是,是不是盲文在紙上形成盲文? – Jodrell

+0

也許你應該使用其他的東西而不是逗號 - 一些符號序列,這在文本中很少遇到:Jonas => @#%^ jonas。比你可以使逆操作更容易 – horgh

回答

1

想到這裏,也許,你真正想要做的是實現自己的編碼方式,叫做財產以後像PrintableSwedishBrailleAsciiEncodingEncoding基類繼承。

using System.Text; 

public sealed PrintableSwedishBrailleAsciiEncoding : Encoding 
{ 
    ... 
} 

這將最大限度地提高你的代碼resuability,使您能夠簡單地使用框架的其他部分做你的工作。


針對我現在刪除回答您的意見,我想你是問,

我如何替換某個字符,然後任意數量的非空格字符的,直到第一個空白字符。或者更一般地說,以某個字符開始的整個單詞?

所以,你可以使用一個Regex這樣的事情,我認爲這將匹配#後跟數字的非空格字符。

var numberMatcher = new Regex(@"#\w+") 
var firstMatch = numberMatcher.Match(yourText) 

var alteredMatch = SomeTextAlteringFunction(firstMatch); 

var yourNewText = numberMatcher.Replace(yourText, alteredMatch); 
+0

是的,當我達到編程水平時,它將是完美的:) 我知道我可以做很多來改進我的轉換類,我會在稍後再做。但是現在我需要快速解決這個問題。我的轉換課程工作正常,我可以打印沒有問題。我的問題是將文本返回到其原始屬性。 –

+0

是的,這是正確的。因爲我將每個數字都改爲前面帶#的字母。 2012年前將改爲#bjab。然後我想將其轉換回數字。 –

+1

@JonasLöfkvist,我想我已經回答了您的具體問題,但是在我的編輯中,但是,如果您正確實施了'Encoding',它將允許您在'Unicode'和您的專用編碼之間來回轉換。 – Jodrell

0

這是我的解決方案(很多Jodrell感謝)

 public string convertBackToPrint(string oldText) 
     { 
      string newText = oldText.Replace(",", ""); 
      newText = newText.Replace("*", "å"); 
      newText = newText.Replace(">", "ä"); 
      newText = newText.Replace("[", "ö"); 
      newText = newText.Replace('\'', '.'); 
      newText = newText.Replace('1', ','); 
      newText = newText.Replace('5', '?'); 
      newText = newText.Replace('6', '!'); 
      newText = newText.Replace('3', ':'); 
      newText = newText.Replace('7', '='); 
      newText = newText.Replace('4', '+'); 
      newText = newText.Replace('9', '*'); 
      newText = newText.Replace('=', 'é'); 

      var numberMatcher = new Regex(@"#\w+"); 
      var firstMatch = numberMatcher.Match(newText); 
      string alteredMatch = convertToNum(firstMatch.ToString()); 
      string yourNewText = numberMatcher.Replace(newText, alteredMatch); 

      return yourNewText; 
     } 

     private string convertToNum(string oldText) 
     { 
      string newText = ""; 

      newText = oldText.Replace("a", "1"); 
      newText = newText.Replace("b", "2"); 
      newText = newText.Replace("c", "3"); 
      newText = newText.Replace("d", "4"); 
      newText = newText.Replace("e", "5"); 
      newText = newText.Replace("f", "6"); 
      newText = newText.Replace("g", "7"); 
      newText = newText.Replace("h", "8"); 
      newText = newText.Replace("i", "9"); 
      newText = newText.Replace("j", "0"); 
      newText = newText.Replace("#", ""); 

      return newText; 
     }