共用方式為


ISmartTagExtension.Recognize 方法

搜尋文件中的文字,找出認識的詞彙。

命名空間:  Microsoft.Office.Tools.Word
組件:  Microsoft.Office.Tools.Word (在 Microsoft.Office.Tools.Word.dll 中)

語法

'宣告
Sub Recognize ( _
    text As String, _
    site As ISmartTagRecognizerSite, _
    tokenList As ISmartTagTokenList, _
    context As SmartTagRecognizeContext _
)
void Recognize(
    string text,
    ISmartTagRecognizerSite site,
    ISmartTagTokenList tokenList,
    SmartTagRecognizeContext context
)

參數

  • text
    型別:System.String
    用來搜尋認識之詞彙的文字。
  • site
    型別:Microsoft.Office.Interop.SmartTag.ISmartTagRecognizerSite
    文字在活頁簿或文件中的位置。
  • tokenList
    型別:Microsoft.Office.Interop.SmartTag.ISmartTagTokenList
    要用來搜尋認識之詞彙的文字,拆解為語彙基元 (Token) 的清單。

備註

Visual Studio Tools for Office Runtime 會呼叫這個方法來搜尋文字,以找出認識的詞彙。 除了標準的辨識器之外,如果您還想要加入自己的搜尋演算法,請實作這個方法。

範例

下列程式碼範例將示範如何實作 Recognize 方法。 這項實作會將每個智慧標籤詞彙與段落內容相互比較。 程式碼會針對段落中的每個智慧標籤詞彙分別加入自訂智慧標籤屬性,然後使用 PersistTag 方法辨認智慧標籤。 本範例假設您已從 [加入參考] 對話方塊的 [.NET] 索引標籤,新增 [Microsoft.Office.Interop.SmartTag] 的參考。 這個程式碼範例是 ISmartTagExtension 介面完整範例的一部分。

Private Sub Recognize(ByVal text As String,
    ByVal site As ISmartTagRecognizerSite,
    ByVal tokenList As ISmartTagTokenList,
    ByVal context As SmartTagRecognizeContext) Implements ISmartTagExtension.Recognize

    For Each term As String In smartTagDemo.Terms
        ' Search the text for the current smart tag term.
        Dim index As Integer = text.IndexOf(term, 0)

        While (index >= 0)
            ' Create a smart tag token and a property bag for the recognized term.
            Dim propertyBag As ISmartTagProperties = site.GetNewPropertyBag()

            ' Write a new property value.
            Dim key As String = "Key1"
            propertyBag.Write(key, DateTime.Now.ToString())

            ' Attach the smart tag to the term in the document
            context.PersistTag(index, term.Length, propertyBag)

            ' Increment the index and then find the next instance of the smart tag term.
            index += term.Length
            index = text.IndexOf(term, index)
        End While
    Next
End Sub
void ISmartTagExtension.Recognize(string text, ISmartTagRecognizerSite site, ISmartTagTokenList tokenList, 
    SmartTagRecognizeContext context)
{

    foreach (string term in smartTagDemo.Terms)
    {
        // Search the text for the current smart tag term.
        int index = text.IndexOf(term, 0);

        while (index >= 0)
        {
            // Create a smart tag token and a property bag for the recognized term.
            ISmartTagProperties propertyBag = site.GetNewPropertyBag();

            // Write a new property value.
            string key = "Key1";
            propertyBag.Write(key, DateTime.Now.ToString());

            // Attach the smart tag to the term in the document
            context.PersistTag(index, term.Length, propertyBag);

            // Increment the index and then find the next instance of the smart tag term.
            index += term.Length;
            index = text.IndexOf(term, index);
        }
    }
}

.NET Framework 安全性

請參閱

參考

ISmartTagExtension 介面

Microsoft.Office.Tools.Word 命名空間