テキストアナラ​​イザー:著者名の認識(続き)

この記事は、プロジェクト「Text Analyzer」で実装された著者情報認識アルゴリズムに関するものです。 記事の続きでは、テキストをレベルに分割するための有限状態マシンについて検討します。 ( 開始終了 )。



記事の構造:

  1. 著者の分析
  2. コードを知る
  3. TAuthoringAnalyserの内部とテキストストレージ
  4. 戦略に関するステートマシンによるレベリング
  5. 周波数応答収集
  6. ハミングニューラルネットワークと著者分析


追加資料:







4.戦略に関する有限状態マシンによるレベリング


必要性:







結論:





KAは有限状態マシン( [1][2][3] )です。 そのアプリケーションの1つは、単語、複合句読点、関数、構造、メソッドとフィールドを持つクラス全体など、着信文字のチェーン内の特定の構造を認識することです。 これが、スペルチェッカー、ソースコードアナライザー、コンパイラー、構文強調ツールの動作、コンピューターなどの動作です。 宇宙船の適用可能性は巨大です。 たとえば、非決定的な宇宙船を使用して童話を生成するように生徒に依頼しましたが、これは可能です。 ここで、宇宙船は戦略( [1] )で使用されており、2003年にすでにセルゲイ・サツキー( [1] )によって書かれています。



Copy Source | Copy HTML template < class SState , class SEvent , class SFunctor = SEmptyFunctor< SState , SEvent >, class SUnknownEventStrategy = SThrowStrategy< SEvent > > class SFiniteStateMachine { public : typedef SState StateType ; typedef SEvent EventType ; private : StateType _CurrentState; // Current machine state std::vector< StateType > _States; // A list of the registered states std::vector< EventType > _Events; // A list of the registered events std::vector< std::vector< StateType > > _Transitions; // A table of transitions between states SFunctor _Functor; // Transition function SUnknownEventStrategy _UnknownEventStrategy; // Unknown event strategy std::deque< EventType > _EventsQueue; // Internal events queue to support events // that were generated in the transition functions bool _InProcess; // To be sure that we are in the process of events int _CurrentStateIndex; // Index of column in a transition table (0 - based) StateType _InitialState; // Start machine state // ......



  1. Copy Source | Copy HTML template < class SState , class SEvent , class SFunctor = SEmptyFunctor< SState , SEvent >, class SUnknownEventStrategy = SThrowStrategy< SEvent > > class SFiniteStateMachine { public : typedef SState StateType ; typedef SEvent EventType ; private : StateType _CurrentState; // Current machine state std::vector< StateType > _States; // A list of the registered states std::vector< EventType > _Events; // A list of the registered events std::vector< std::vector< StateType > > _Transitions; // A table of transitions between states SFunctor _Functor; // Transition function SUnknownEventStrategy _UnknownEventStrategy; // Unknown event strategy std::deque< EventType > _EventsQueue; // Internal events queue to support events // that were generated in the transition functions bool _InProcess; // To be sure that we are in the process of events int _CurrentStateIndex; // Index of column in a transition table (0 - based) StateType _InitialState; // Start machine state // ......



  2. Copy Source | Copy HTML template < class SState , class SEvent , class SFunctor = SEmptyFunctor< SState , SEvent >, class SUnknownEventStrategy = SThrowStrategy< SEvent > > class SFiniteStateMachine { public : typedef SState StateType ; typedef SEvent EventType ; private : StateType _CurrentState; // Current machine state std::vector< StateType > _States; // A list of the registered states std::vector< EventType > _Events; // A list of the registered events std::vector< std::vector< StateType > > _Transitions; // A table of transitions between states SFunctor _Functor; // Transition function SUnknownEventStrategy _UnknownEventStrategy; // Unknown event strategy std::deque< EventType > _EventsQueue; // Internal events queue to support events // that were generated in the transition functions bool _InProcess; // To be sure that we are in the process of events int _CurrentStateIndex; // Index of column in a transition table (0 - based) StateType _InitialState; // Start machine state // ......



  3. Copy Source | Copy HTML template < class SState , class SEvent , class SFunctor = SEmptyFunctor< SState , SEvent >, class SUnknownEventStrategy = SThrowStrategy< SEvent > > class SFiniteStateMachine { public : typedef SState StateType ; typedef SEvent EventType ; private : StateType _CurrentState; // Current machine state std::vector< StateType > _States; // A list of the registered states std::vector< EventType > _Events; // A list of the registered events std::vector< std::vector< StateType > > _Transitions; // A table of transitions between states SFunctor _Functor; // Transition function SUnknownEventStrategy _UnknownEventStrategy; // Unknown event strategy std::deque< EventType > _EventsQueue; // Internal events queue to support events // that were generated in the transition functions bool _InProcess; // To be sure that we are in the process of events int _CurrentStateIndex; // Index of column in a transition table (0 - based) StateType _InitialState; // Start machine state // ......



  4. Copy Source | Copy HTML template < class SState , class SEvent , class SFunctor = SEmptyFunctor< SState , SEvent >, class SUnknownEventStrategy = SThrowStrategy< SEvent > > class SFiniteStateMachine { public : typedef SState StateType ; typedef SEvent EventType ; private : StateType _CurrentState; // Current machine state std::vector< StateType > _States; // A list of the registered states std::vector< EventType > _Events; // A list of the registered events std::vector< std::vector< StateType > > _Transitions; // A table of transitions between states SFunctor _Functor; // Transition function SUnknownEventStrategy _UnknownEventStrategy; // Unknown event strategy std::deque< EventType > _EventsQueue; // Internal events queue to support events // that were generated in the transition functions bool _InProcess; // To be sure that we are in the process of events int _CurrentStateIndex; // Index of column in a transition table (0 - based) StateType _InitialState; // Start machine state // ......



  5. Copy Source | Copy HTML template < class SState , class SEvent , class SFunctor = SEmptyFunctor< SState , SEvent >, class SUnknownEventStrategy = SThrowStrategy< SEvent > > class SFiniteStateMachine { public : typedef SState StateType ; typedef SEvent EventType ; private : StateType _CurrentState; // Current machine state std::vector< StateType > _States; // A list of the registered states std::vector< EventType > _Events; // A list of the registered events std::vector< std::vector< StateType > > _Transitions; // A table of transitions between states SFunctor _Functor; // Transition function SUnknownEventStrategy _UnknownEventStrategy; // Unknown event strategy std::deque< EventType > _EventsQueue; // Internal events queue to support events // that were generated in the transition functions bool _InProcess; // To be sure that we are in the process of events int _CurrentStateIndex; // Index of column in a transition table (0 - based) StateType _InitialState; // Start machine state // ......



  6. Copy Source | Copy HTML template < class SState , class SEvent , class SFunctor = SEmptyFunctor< SState , SEvent >, class SUnknownEventStrategy = SThrowStrategy< SEvent > > class SFiniteStateMachine { public : typedef SState StateType ; typedef SEvent EventType ; private : StateType _CurrentState; // Current machine state std::vector< StateType > _States; // A list of the registered states std::vector< EventType > _Events; // A list of the registered events std::vector< std::vector< StateType > > _Transitions; // A table of transitions between states SFunctor _Functor; // Transition function SUnknownEventStrategy _UnknownEventStrategy; // Unknown event strategy std::deque< EventType > _EventsQueue; // Internal events queue to support events // that were generated in the transition functions bool _InProcess; // To be sure that we are in the process of events int _CurrentStateIndex; // Index of column in a transition table (0 - based) StateType _InitialState; // Start machine state // ......



  7. Copy Source | Copy HTML template < class SState , class SEvent , class SFunctor = SEmptyFunctor< SState , SEvent >, class SUnknownEventStrategy = SThrowStrategy< SEvent > > class SFiniteStateMachine { public : typedef SState StateType ; typedef SEvent EventType ; private : StateType _CurrentState; // Current machine state std::vector< StateType > _States; // A list of the registered states std::vector< EventType > _Events; // A list of the registered events std::vector< std::vector< StateType > > _Transitions; // A table of transitions between states SFunctor _Functor; // Transition function SUnknownEventStrategy _UnknownEventStrategy; // Unknown event strategy std::deque< EventType > _EventsQueue; // Internal events queue to support events // that were generated in the transition functions bool _InProcess; // To be sure that we are in the process of events int _CurrentStateIndex; // Index of column in a transition table (0 - based) StateType _InitialState; // Start machine state // ......



  8. Copy Source | Copy HTML template < class SState , class SEvent , class SFunctor = SEmptyFunctor< SState , SEvent >, class SUnknownEventStrategy = SThrowStrategy< SEvent > > class SFiniteStateMachine { public : typedef SState StateType ; typedef SEvent EventType ; private : StateType _CurrentState; // Current machine state std::vector< StateType > _States; // A list of the registered states std::vector< EventType > _Events; // A list of the registered events std::vector< std::vector< StateType > > _Transitions; // A table of transitions between states SFunctor _Functor; // Transition function SUnknownEventStrategy _UnknownEventStrategy; // Unknown event strategy std::deque< EventType > _EventsQueue; // Internal events queue to support events // that were generated in the transition functions bool _InProcess; // To be sure that we are in the process of events int _CurrentStateIndex; // Index of column in a transition table (0 - based) StateType _InitialState; // Start machine state // ......



  9. Copy Source | Copy HTML template < class SState , class SEvent , class SFunctor = SEmptyFunctor< SState , SEvent >, class SUnknownEventStrategy = SThrowStrategy< SEvent > > class SFiniteStateMachine { public : typedef SState StateType ; typedef SEvent EventType ; private : StateType _CurrentState; // Current machine state std::vector< StateType > _States; // A list of the registered states std::vector< EventType > _Events; // A list of the registered events std::vector< std::vector< StateType > > _Transitions; // A table of transitions between states SFunctor _Functor; // Transition function SUnknownEventStrategy _UnknownEventStrategy; // Unknown event strategy std::deque< EventType > _EventsQueue; // Internal events queue to support events // that were generated in the transition functions bool _InProcess; // To be sure that we are in the process of events int _CurrentStateIndex; // Index of column in a transition table (0 - based) StateType _InitialState; // Start machine state // ......



  10. Copy Source | Copy HTML template < class SState , class SEvent , class SFunctor = SEmptyFunctor< SState , SEvent >, class SUnknownEventStrategy = SThrowStrategy< SEvent > > class SFiniteStateMachine { public : typedef SState StateType ; typedef SEvent EventType ; private : StateType _CurrentState; // Current machine state std::vector< StateType > _States; // A list of the registered states std::vector< EventType > _Events; // A list of the registered events std::vector< std::vector< StateType > > _Transitions; // A table of transitions between states SFunctor _Functor; // Transition function SUnknownEventStrategy _UnknownEventStrategy; // Unknown event strategy std::deque< EventType > _EventsQueue; // Internal events queue to support events // that were generated in the transition functions bool _InProcess; // To be sure that we are in the process of events int _CurrentStateIndex; // Index of column in a transition table (0 - based) StateType _InitialState; // Start machine state // ......



  11. Copy Source | Copy HTML template < class SState , class SEvent , class SFunctor = SEmptyFunctor< SState , SEvent >, class SUnknownEventStrategy = SThrowStrategy< SEvent > > class SFiniteStateMachine { public : typedef SState StateType ; typedef SEvent EventType ; private : StateType _CurrentState; // Current machine state std::vector< StateType > _States; // A list of the registered states std::vector< EventType > _Events; // A list of the registered events std::vector< std::vector< StateType > > _Transitions; // A table of transitions between states SFunctor _Functor; // Transition function SUnknownEventStrategy _UnknownEventStrategy; // Unknown event strategy std::deque< EventType > _EventsQueue; // Internal events queue to support events // that were generated in the transition functions bool _InProcess; // To be sure that we are in the process of events int _CurrentStateIndex; // Index of column in a transition table (0 - based) StateType _InitialState; // Start machine state // ......



  12. Copy Source | Copy HTML template < class SState , class SEvent , class SFunctor = SEmptyFunctor< SState , SEvent >, class SUnknownEventStrategy = SThrowStrategy< SEvent > > class SFiniteStateMachine { public : typedef SState StateType ; typedef SEvent EventType ; private : StateType _CurrentState; // Current machine state std::vector< StateType > _States; // A list of the registered states std::vector< EventType > _Events; // A list of the registered events std::vector< std::vector< StateType > > _Transitions; // A table of transitions between states SFunctor _Functor; // Transition function SUnknownEventStrategy _UnknownEventStrategy; // Unknown event strategy std::deque< EventType > _EventsQueue; // Internal events queue to support events // that were generated in the transition functions bool _InProcess; // To be sure that we are in the process of events int _CurrentStateIndex; // Index of column in a transition table (0 - based) StateType _InitialState; // Start machine state // ......



  13. Copy Source | Copy HTML template < class SState , class SEvent , class SFunctor = SEmptyFunctor< SState , SEvent >, class SUnknownEventStrategy = SThrowStrategy< SEvent > > class SFiniteStateMachine { public : typedef SState StateType ; typedef SEvent EventType ; private : StateType _CurrentState; // Current machine state std::vector< StateType > _States; // A list of the registered states std::vector< EventType > _Events; // A list of the registered events std::vector< std::vector< StateType > > _Transitions; // A table of transitions between states SFunctor _Functor; // Transition function SUnknownEventStrategy _UnknownEventStrategy; // Unknown event strategy std::deque< EventType > _EventsQueue; // Internal events queue to support events // that were generated in the transition functions bool _InProcess; // To be sure that we are in the process of events int _CurrentStateIndex; // Index of column in a transition table (0 - based) StateType _InitialState; // Start machine state // ......



  14. Copy Source | Copy HTML template < class SState , class SEvent , class SFunctor = SEmptyFunctor< SState , SEvent >, class SUnknownEventStrategy = SThrowStrategy< SEvent > > class SFiniteStateMachine { public : typedef SState StateType ; typedef SEvent EventType ; private : StateType _CurrentState; // Current machine state std::vector< StateType > _States; // A list of the registered states std::vector< EventType > _Events; // A list of the registered events std::vector< std::vector< StateType > > _Transitions; // A table of transitions between states SFunctor _Functor; // Transition function SUnknownEventStrategy _UnknownEventStrategy; // Unknown event strategy std::deque< EventType > _EventsQueue; // Internal events queue to support events // that were generated in the transition functions bool _InProcess; // To be sure that we are in the process of events int _CurrentStateIndex; // Index of column in a transition table (0 - based) StateType _InitialState; // Start machine state // ......



  15. Copy Source | Copy HTML template < class SState , class SEvent , class SFunctor = SEmptyFunctor< SState , SEvent >, class SUnknownEventStrategy = SThrowStrategy< SEvent > > class SFiniteStateMachine { public : typedef SState StateType ; typedef SEvent EventType ; private : StateType _CurrentState; // Current machine state std::vector< StateType > _States; // A list of the registered states std::vector< EventType > _Events; // A list of the registered events std::vector< std::vector< StateType > > _Transitions; // A table of transitions between states SFunctor _Functor; // Transition function SUnknownEventStrategy _UnknownEventStrategy; // Unknown event strategy std::deque< EventType > _EventsQueue; // Internal events queue to support events // that were generated in the transition functions bool _InProcess; // To be sure that we are in the process of events int _CurrentStateIndex; // Index of column in a transition table (0 - based) StateType _InitialState; // Start machine state // ......



  16. Copy Source | Copy HTML template < class SState , class SEvent , class SFunctor = SEmptyFunctor< SState , SEvent >, class SUnknownEventStrategy = SThrowStrategy< SEvent > > class SFiniteStateMachine { public : typedef SState StateType ; typedef SEvent EventType ; private : StateType _CurrentState; // Current machine state std::vector< StateType > _States; // A list of the registered states std::vector< EventType > _Events; // A list of the registered events std::vector< std::vector< StateType > > _Transitions; // A table of transitions between states SFunctor _Functor; // Transition function SUnknownEventStrategy _UnknownEventStrategy; // Unknown event strategy std::deque< EventType > _EventsQueue; // Internal events queue to support events // that were generated in the transition functions bool _InProcess; // To be sure that we are in the process of events int _CurrentStateIndex; // Index of column in a transition table (0 - based) StateType _InitialState; // Start machine state // ......



  17. Copy Source | Copy HTML template < class SState , class SEvent , class SFunctor = SEmptyFunctor< SState , SEvent >, class SUnknownEventStrategy = SThrowStrategy< SEvent > > class SFiniteStateMachine { public : typedef SState StateType ; typedef SEvent EventType ; private : StateType _CurrentState; // Current machine state std::vector< StateType > _States; // A list of the registered states std::vector< EventType > _Events; // A list of the registered events std::vector< std::vector< StateType > > _Transitions; // A table of transitions between states SFunctor _Functor; // Transition function SUnknownEventStrategy _UnknownEventStrategy; // Unknown event strategy std::deque< EventType > _EventsQueue; // Internal events queue to support events // that were generated in the transition functions bool _InProcess; // To be sure that we are in the process of events int _CurrentStateIndex; // Index of column in a transition table (0 - based) StateType _InitialState; // Start machine state // ......



  18. Copy Source | Copy HTML template < class SState , class SEvent , class SFunctor = SEmptyFunctor< SState , SEvent >, class SUnknownEventStrategy = SThrowStrategy< SEvent > > class SFiniteStateMachine { public : typedef SState StateType ; typedef SEvent EventType ; private : StateType _CurrentState; // Current machine state std::vector< StateType > _States; // A list of the registered states std::vector< EventType > _Events; // A list of the registered events std::vector< std::vector< StateType > > _Transitions; // A table of transitions between states SFunctor _Functor; // Transition function SUnknownEventStrategy _UnknownEventStrategy; // Unknown event strategy std::deque< EventType > _EventsQueue; // Internal events queue to support events // that were generated in the transition functions bool _InProcess; // To be sure that we are in the process of events int _CurrentStateIndex; // Index of column in a transition table (0 - based) StateType _InitialState; // Start machine state // ......



  19. Copy Source | Copy HTML template < class SState , class SEvent , class SFunctor = SEmptyFunctor< SState , SEvent >, class SUnknownEventStrategy = SThrowStrategy< SEvent > > class SFiniteStateMachine { public : typedef SState StateType ; typedef SEvent EventType ; private : StateType _CurrentState; // Current machine state std::vector< StateType > _States; // A list of the registered states std::vector< EventType > _Events; // A list of the registered events std::vector< std::vector< StateType > > _Transitions; // A table of transitions between states SFunctor _Functor; // Transition function SUnknownEventStrategy _UnknownEventStrategy; // Unknown event strategy std::deque< EventType > _EventsQueue; // Internal events queue to support events // that were generated in the transition functions bool _InProcess; // To be sure that we are in the process of events int _CurrentStateIndex; // Index of column in a transition table (0 - based) StateType _InitialState; // Start machine state // ......









Sackiステートマシン( [h] )は、「Strategy」パターンで実装されます。 (戦略、 [1][2][3] )。 戦略には、SState、SEvent(イベント、状態)、SFunctor(ある状態から別の状態への遷移のファンクター)、およびSUnknownEventStrategy(イベントが認識されない場合のKA動作)のタイプが含まれます。 イベントを認識し、宇宙船を新しい状態に変換する関数は次のとおりです。



Copy Source | Copy HTML



  1. インラインvoid ProcessEvent( const EventType&tEvent)
  2. {
  3. int EventIndex(GetEventIndex(tEvent));
  4. if (EventIndex ==- 1return ;
  5. StateType OldState(_CurrentState); //古い状態を保持します。
  6. _CurrentState =(_Transitions [EventIndex])[_ CurrentStateIndex]; //新しい状態を定義します。
  7. _CurrentStateIndex = GetStateIndex(_CurrentState);
  8. _Functor(OldState、tEvent、_CurrentState); //新しい状態に関連付けられたアクションを実行します。
  9. }




外部環境のどこかで、文字ごとにテキストをソートします。 次の文字を取得して、ProcessEvent()関数に渡します。 ステートマシンはこのシンボルを使用して、現在の状態にも基づいて遷移テーブル内のセルを選択します。 セルは、どの状態が発生し、次に何をすべきかを示します。 彼が「シンボル」をイベントとして理解できるように、宇宙船を少し変更しました。 また、州のクラスをスリップすることにより、「州」戦略を再定義しました。 したがって、遷移表は特定のイベントと私が持っていた状態で構成されていました。



そして、開発することは非常に困難でした。 彼女は飛躍的に成長しました。 状態が「空」で、文字「A」が来た場合、対応する状態に入ることにより、単語、文、および段落の認識を開始する必要があります。 ポイントが来た場合、特別な状態に入り、これが複合句読点でない場合は待機する必要があります。 「B」、「C」、「b」、「c」、「!」、「1」、「2」...アルファベットのすべての文字、すべての句読点、通常はASCIIテーブルのすべての文字が来る可能性があります。 そして、何らかの形でそれらに反応する必要があります! 〜255行(1行-1イベント、1シンボル)と約20列の宇宙船状態のテーブルを想像してください。 ほうきで殺すのと同じです。何個のセルを塗りつぶすための移行コマンドがあります! 20 * 255 = 5100セル。 もっと簡単なアプローチを見つけました。 同じタイプのすべての文字は文字セット( [CCharsSet.h] ; [UConstants.h] )に配置され、その文字が到着すると、文字セット全体がすでにイベントと見なされます。 もちろん、セットは交差する場合があります。 たとえば、文字「B」は、「すべての文字」、「文字」、「大文字」、「ロシア文字」、「ロシア大文字」のセットの要素です。 ドット記号は、すべての記号、句読点、および段落の終了記号セットに含まれています。 などなど。 セットはきちんと出てきましたが、これはすべてのキャラクターよりも少ないです。 変換テーブル( [xls] )は10倍に削減されました。 もう1つの重要な利点は、ナビゲーションテーブルに影響を与えずにセットを変更できることです。 さて、「」という文字を忘れてしまいました。まあ、それを対応するセットに挿入して、ビジネスにしましょう!..



それはどのように機能しますか? ステートマシンにテキストを渡した後、レベルのツリーを構築する必要があります。 つまり、イベントが発生したときに命令を実行するステートマシンは、イベントに関連付けられた関数を呼び出します。 そのような関数( [UParSentWordFSM.h] )がいくつかあり、それらはすべて共通のツリーを構築し、それへの入力ポインターを受け取ります。 宇宙船の構成がどのように記述されているかを詳しく見てみましょう。



Copy Source | Copy HTML



  1. クラス TState ;
  2. typedef TCharsSetEvent TEvent ;
  3. typedef TCFDivisionTreeItem < TCFLevelDataTypeTUnitType > TCFCustomUnitDivisionTreeItem;
  4. typedef TCFTreeLevel < TCFLevelDataTypeTUnitType > TCFCustomUnitTreeLevel;
  5. typedef void (* TFuncPtr)(TCFCustomUnitDivisionTreeItem *、 const TEvent &);
  6. typedef TStateMachineDescriptor < TStateTEvent > TParSenWordDescriptor;
  7. / *使用される状態のクラス* /
  8. TState クラスpublic TFunctionalState < TEventTTextString >
  9. {
  10. プライベート
  11. TFuncPtr _Function;
  12. TCFCustomUnitDivisionTreeItem * _TargetTree;
  13. 公開
  14. void OnEnter( const TState &tStateFrom、 const TEvent &tEvent){
  15. _Function(_TargetTree、tEvent);
  16. };
  17. TStateconst TTextString &tName、TFuncPtr tFunc、TCFCustomUnitDivisionTreeItem * tTargetTree)
  18. :TFunctionalState < TEventTTextString >(tName、sat_None)、_Function(tFunc)、_TargetTree(tTargetTree){};
  19. };




TStateクラスは、その名前が示すとおり、CAの状態です。 その中には、ツリーを構築するための関数へのポインターと、ツリー自体へのポインターがあります。 この関数は、OnEnter()メソッドがTStateから要求されたときに呼び出されます。 このツリーが表すクラス(TCFCustomUnitDivisionTreeItem)はかなり複雑です。これは、より抽象的なテンプレートクラス( [h] )の拡張です。 今はそれに焦点を合わせません。 ツリー構築関数をさらに見てみましょう。



Copy Source | Copy HTML



  1. void FEmpty(TCFCustomUnitDivisionTreeItem * tItem、 const TEvent&tEvent)
  2. {
  3. 帰る
  4. };
  5. void FOpenParagraph(TCFCustomUnitDivisionTreeItem * tTree、 const TEventおよびtEvent)
  6. {
  7. TCFCustomUnitDivisionTreeItem NewItem( 0 、TRangeItem(tEvent.Iterator()、tEvent.Iterator()));
  8. tTree-> AddItem( 0 、NewItem);
  9. };
  10. void FOpenSentence(TCFCustomUnitDivisionTreeItem * tTree、 const TEventおよびtEvent)
  11. {
  12. TCFCustomUnitDivisionTreeItem NewItem( 1 、TRangeItem(tEvent.Iterator()、tEvent.Iterator()));
  13. tTree-> AddItem( 1 、NewItem);
  14. };
  15. // .......
  16. void FCloseAll(TCFCustomUnitDivisionTreeItem * tTree、 const TEventおよびtEvent)
  17. {
  18. FCloseWord(tTree、tEvent);
  19. FCloseSentence(tTree、tEvent);
  20. FCloseParagraph(tTree、tEvent);
  21. };
  22. // ...およびその他の関数...




すべて1つのタイプがあります。 TStateクラスのこれらの関数を互いに置き換えることができます。したがって、関数のオーバーロードとポリモーフィズムがあります( [1][2] )。



しかし、これらはすべて主要なものの定義と準備です。 宇宙船の設定を含む特定のオブジェクトを取得する必要がありますよね? このようなオブジェクトを返す関数は次のとおりです。



Copy Source | Copy HTML



  1. TParSenWordDescriptor記述子(TCFCustomUnitDivisionTreeItem * tTree)
  2. {
  3. //初期状態はq0​​、関数はFEmpty、遷移表は「段落、文、単語」と呼ばれます。
  4. TParSenWordDescriptor D(TState( "q0" 、FEmpty、tTree)、 "段落、文章、単語" );
  5. //状態を設定し、それぞれに独自の名前を付けます:q0、q1、... q14。
  6. //各状態に関数(FEmpty、FOpenAll ...)を渡します。この関数は、状態が発生したときに呼び出す必要があります。
  7. //また、構築しているツリーへのポインタを渡します-tTree。
  8. D << TState( "q0" 、FEmpty、tTree)
  9. << TState( "q1" 、FOpenAll、tTree)
  10. << TState( "q2" 、FOpenSentenceOpenWord、tTree)
  11. << TState( "q3" 、FOpenWord、tTree)
  12. << TState( " q4 " 、FEmpty、tTree)
  13. << TState( " q5 " 、FCloseWord、tTree)
  14. << TState( " q6 " 、FCloseSentence、tTree)
  15. << TState( "q7" 、FCloseSentenceOpenSentenceOpenWord、tTree)
  16. << TState( "q8" 、FEmpty、tTree)
  17. << TState( "q9" 、FEmpty、tTree)
  18. << TState( "q10" 、FCloseWord、tTree)
  19. << TState( "q11" 、FCloseAll、tTree)
  20. << TState( "q12" 、FCloseSentenceCloseParagraph、tTree)
  21. << TState( "q13" 、FCloseParagraph、tTree)
  22. << TState( "q14" 、FEmpty、tTree);
  23. //イベントを設定し、それぞれ独自の名前(「WinParagraph」、「SentenceEnd」...)を付けます。
  24. // cWinParagraph、cSentenceEndはUConstants.hで説明されている文字セットです。
  25. D << TEvent( "WinParagraph" 、cWinParagraph)
  26. << "q0" << "q11" << "q11" << "q11" << "q11" << "q12" << "q12" << "q11" << "q12" << "q13" << "q12" << "q0" << "q0" << "q0" << "q12" ;
  27. D << TEvent( "SentenceEnd" 、cSentenceEnd)
  28. << "q0" << "q10" << "q10" << "q10" << "q10" << "q14" << "q14" << "q10" << "q14" << "q9" << "q14" << "q0" << "q0" << "q0" << "q14" ;
  29. D << TEvent( "Letters" 、cLetters + cDigits)
  30. << "q1" << "q4" << "q4" << "q4" << "q4" << "q3" << "q2" << "q4" << "q3" << "q2" << "q7" << "q1" << "q1" << "q1" << "q7" ;
  31. D << TEvent( "スペース" 、cSpace + cTab)
  32. << "q0" << " q5" << "q5" << "q5" << "q5" << "q8" << "q9" << "q5" << "q8" << "q9" << "q6" << "q0" << "q0" << "q0" << "q6" ;
  33. D << TEvent( "PunctMarksおよびその他のシンボル" 、TCharsSet(cPrintable)>> cWinParagraph >> cSentenceEnd >> cLetters >> cDigits >> cSpace >> cTab)
  34. << "q0" << " q5" << "q5" << "q5" << "q5" << "q8" << "q9" << "q5" << "q8" << "q9" << "q14" << "q0" << "q0" << "q0" << "q14" ;
  35. D << TEvent( "EndSign" 、TCharsSet(CTextStringEndSign))
  36. << "q0" << "q11" << "q11" << "q11" << "q11" << "q12" << "q13" << "q11" << "q12" << "q13" << "q12" << "q0" << "q0" << "q0" << "q12" ;
  37. return D;
  38. };




宇宙船のテーブルにイベントや条件がどのように入力されるかは非常にはっきりと見えますが、これらの「<<」記号でテーブル自体を見ることは困難です。 信じてください、ここではすべて記録されています。



TEventクラスを調べることもできます。実際には、TCharsSetEvent( [cpp][h] )です。 好奇心-ようこそ、宇宙船のマネージャーTFiniteStateMachineManager( [h] )に行きます。 このクラスの目的は次のとおりです。統一されたインターフェイスを使用して、イベントの抽象的なソースに対して抽象的な宇宙船を使用して分析します。 また、マネージャーでは、認識の進行状況をどこかに表示する関数を渡すことができます。 このクラスのコードは非常に豊富であり、最も興味深いセクションのみを示します。



Copy Source | Copy HTML



  1. テンプレート< クラス EventTypeクラス FiniteStateMachineTypeクラス StateMachineDescriptorTypeクラス SourceType >
  2. クラス TFiniteStateMachineManager
  3. {
  4. プライベート
  5. StateMachineDescriptorType _Descriptor;
  6. SourceType _Source;
  7. TUInt _Begin;
  8. TUInt _End;
  9. TUInt _Iterator;
  10. 公開
  11. // <......>
  12. TFiniteStateMachineManagerconst StateMachineDescriptorType &tDescriptor、 const SourceType &tSource)
  13. _Descriptor(tDescriptor)、
  14. _Source(tSource)、
  15. _ReportStep( 0 )、
  16. _ProgressFunc(NULL)
  17. {};
  18. // <......>




繰り返しますが、テンプレートを扱っています。 正確には、「戦略」パターンもここに示されています。 戦略に関する有限状態マシンだけでなく、何らかのマネージャーもあります! 私は隠していません。この同じマネージャーを使用してハーモニーカードを作成します。これは、別のステートマシン、特別なマシンでのみ機能します。 実際、彼は、インターフェイスが同じである場合、どんな種類の宇宙船が滑るのか気にしません。 マネージャーは、この「スリップ」した宇宙船を受け取り、すべての作業を彼にリダイレクトします。 1つの宇宙船がサウンドルールを処理し、もう1つがテキストをレベルに分割します。 これが「戦略」パターンの本質です。実際の作業を行うオブジェクト戦略でクラスを構成します。 たとえば、イベントが発生したときにフィルターをトリガーする必要がある場合、フィルターを戦略として実装する別のクラスを渡すことで、EventTypeイベントを置き換えることができます。



Copy Source | Copy HTML



  1. TFiniteStateMachineManagerおよびプロセス()
  2. {
  3. _Begin = _Source.Begin();
  4. _End = _Source.End();
  5. FiniteStateMachineType _Machine(_Descriptor.StartState()、_ Descriptor.Proxy());
  6. for (_Iterator = _Begin; _Iterator <= _ End; _Iterator ++)
  7. {
  8. _Machine << EventType(_Source [_Iterator]、_Iterator);
  9. };
  10. _Machine << EventType(_Source.EndSign()、_End + 1 );
  11. return * this ;
  12. };






Process()関数は簡単です。 _Machineステートマシンオブジェクトが作成され、記述子(遷移テーブル、初期状態)によって構成され、ループ内の認識プロセスが開始されます。 EventTypeクラスに2番目のパラメーターとして渡される「イテレーター」は、テキスト内の文字のインデックスを示します。 イベントがトリガーされた場合、たとえば「Start Word」状態の場合、インデックスはBeginIndexとしてRangeItemに書き込まれます。 逆もまた同様です。イベントが単語(文、段落)の終わりである場合、インデックスはこの範囲で最終的になります。 したがって、テキストに関連するRangeItemのリストを取得します。



「イテレータ」という言葉を引用したのはなぜですか? これは実際のイテレータではなく、単なる整数変数だからです。 しかし、真に汎用的なマネージャーを作成する必要がある場合は、イベントオブジェクトから抽象化する必要があります。 マネージャーは、データがそこに格納されているリスト、テーブル、または配列、それらがどこから来たのか、要素の数、およびそれらの順序についてまったく心配する必要はありません。 そして、それらを確実に通過させるには、Iteratorパターン(Iterator、 [1][2] )を使用できます。 ループをイテレーターで機能させるために大きな変更は必要ありません。



Copy Source | Copy HTML



  1. // ......
  2. SourceType * _Source;
  3. // ......
  4. SourceType ::イテレータ_Iterator = _Source-> Begin();
  5. for (; _Iterator!= _Source-> End(); ++ _ Iterator)
  6. {
  7. _Machine << EventType(* _ Iterator);
  8. };






この宇宙船の結果は、テキスト分割ツリーになります。 次に、これらのパーティションから比較するセクションを選択する必要があります。 アルゴリズムは非常に簡単です。

-テキストごとに、セクションは長さが増加する順にソートされます。

-最小限のコンプライアンステンプレートがコンパイルされます。 すべてのテキストから同じ長さのセクションをいくつ取得できるかを規定しています。 すべてのテキストに1000の長さの10の段落があり、そのような段落の1つのテキストに8しかない場合、最小一致は8セクションです。 だから-各長さ。

-テンプレートに従って、テキストのセクションが選択されます。

-アルゴリズムには、段落、文、単語のすべてのレベルが順番に含まれます。



これでサンプルが得られ、特性の収集を開始できます。



All Articles