Export (0) Print
Expand All

RecognizedWordUnit.DisplayAttributes Property

Gets formatting information used to create the text output from the current RecognizedWordUnit instance.

Namespace:  System.Speech.Recognition
Assembly:  System.Speech (in System.Speech.dll)

public DisplayAttributes DisplayAttributes { get; }

Property Value

Type: System.Speech.Recognition.DisplayAttributes
Specifies the use of white space to display of the contents of a RecognizedWordUnit object.

The DisplayAttributes object returned by the DisplayAttributes property specifies the leading and trailing spaces to be used with a given word, if any.

For more information about how to use this formatting information, see the DisplayAttributes enumeration.

The following example shows a utility routine (stringFromWordArray) that generates a string that is formatted in one of three ways: lexically (using LexicalForm), normalized (using Text), or phonetically (using Pronunciation). The text output is obtained from the DisplayAttributes property on a ReadOnlyCollection of RecognizedWordUnit objects, which is obtained from the Words property on a RecognizedPhrase object.

internal enum WordType 
{
  Text,
  Normalized = Text,
  Lexical,
  Pronunciation
}
internal static string stringFromWordArray(
        ReadOnlyCollection<RecognizedWordUnit> words, 
        WordType type) 
{
  string text = "";
  foreach (RecognizedWordUnit word in words) 
  {
    string wordText = "";
    if (type == WordType.Text || type == WordType.Normalized) 
    {
      wordText = word.Text;
    } 
    else if (type == WordType.Lexical) 
    {
      wordText = word.LexicalForm;
    }
    else if (type == WordType.Pronunciation) 
    {
       wordText = word.Pronunciation;
    } 
    else 
    {
      throw new InvalidEnumArgumentException(
         String.Format("[0}: is not a valid input", type));
    }

    // Use display attribute
    if ((word.DisplayAttributes & DisplayAttributes.OneTrailingSpace) != 0) 
    {
      wordText += " ";
    }
    if ((word.DisplayAttributes & DisplayAttributes.TwoTrailingSpaces) != 0)
    {
      wordText += "  ";
    }
    if ((word.DisplayAttributes & DisplayAttributes.ConsumeLeadingSpaces) != 0) 
    {
      wordText = wordText.TrimStart();
    }
    if ((word.DisplayAttributes & DisplayAttributes.ZeroTrailingSpaces) != 0)
    {
      wordText = wordText.TrimEnd();
    }

    text += wordText;

  }
  return text;
}

.NET Framework

Supported in: 4.6, 4.5, 4, 3.5, 3.0

.NET Framework Client Profile

Supported in: 4
Show:
© 2015 Microsoft