記事の構造:
追加資料:
- テキストアナライザープロジェクトのソース (Borland C ++ Builder 6.0)
- Excelでハミングニューラルシステムをテストする( [xls] )
- テキストをレベルに分割する宇宙船の遷移表( [xls] )
- 個々の文字の調和の計算( [xls] )
- テキストアナライザーディプロマプロジェクトのプレゼンテーション( [ppt] )
- 「Map of Harmony」プロジェクトのプレゼンテーション( [ppt] )
- これらの素材はすべて圧縮されています( [zip] 、 [7z] 、 [rar] )
4.戦略に関する有限状態マシンによるレベリング
必要性:
- テキストを単語、文、段落に分割します。
- 内部実現について何も知らずに、これらのレベルに同じ方法で対処します。
- これらのレベルを現物で保管しないでください。保管しないと、テキストが膨大な回数コピーされてしまいます。

結論:
- レベルは、インデックス付きのリストとして保存されます。 論理的には、範囲の概念はここに適合します(Range、 [1] 、 [2] 、 [3] )。 その当時、私はまだRangeについてもAlexandrescu( [1] )についても何も知りませんでしたが、原始的な範囲を得ました。 範囲には、開始インデックスと終了インデックス:RangeItem(BeginIndex、EndIndex)、または開始インデックスと数量(オフセット):Range(BeginIndex、Count)があります。 インデックスは、テキスト内の文字の位置に正確に対応しています。 すべてのテキストを示す場合、RangeItem(1、Length(Text))になります。 途中で段落が必要な場合は、RangeItem(312031、312355)を取得します。 範囲のリストは、直接コピーせずにテキストの一部になります。
- 範囲は抽象化の背後に隠れている必要があります。 そのため、最初と最後の段落である範囲のリストを参照すると、その中に隠されているテキストのみが表示されるはずです。 これらのRangeの数とそれらが指す場所を気にする必要はありません。 より高いレベルに目を向けると、彼は私たちが望むものを提供してくれます。
- 言葉の定義は問題ではありません。 すべての文字を他の文字と区別してください...段落も簡単に識別できます。 もちろん、異なるオペレーティングシステムでは、段落の終わりが異なるように指定されていることを忘れないでください( [1] )。 そして、ここにオファーがあります...オファーを完了するための「合法的な」サインはいくつあると思いますか? スリー?! ポイント、質問、感嘆符?..どこに!!。まだ組み合わせることができます!!! ( [1] )。 しかし、それだけではありません。 私は、1つの文に実際に2つ以上がある構造に出会いました。 そして、間違って数えると、前の7つの文の代わりに、15を超える数を数えることができます。 結論は、原則として明らかです。有限状態マシンが必要です。
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 // ......
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 // ......
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 // ......
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 // ......
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 // ......
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 // ......
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 // ......
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 // ......
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 // ......
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 // ......
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 // ......
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 // ......
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 // ......
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 // ......
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 // ......
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 // ......
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 // ......
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 // ......
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 // ......
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
- インラインvoid ProcessEvent( const EventType&tEvent)
- {
- int EventIndex(GetEventIndex(tEvent));
- if (EventIndex ==- 1 ) return ;
- StateType OldState(_CurrentState); //古い状態を保持します。
- _CurrentState =(_Transitions [EventIndex])[_ CurrentStateIndex]; //新しい状態を定義します。
- _CurrentStateIndex = GetStateIndex(_CurrentState);
- _Functor(OldState、tEvent、_CurrentState); //新しい状態に関連付けられたアクションを実行します。
- }
外部環境のどこかで、文字ごとにテキストをソートします。 次の文字を取得して、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
- クラス TState ;
- typedef TCharsSetEvent TEvent ;
- typedef TCFDivisionTreeItem < TCFLevelDataType 、 TUnitType > TCFCustomUnitDivisionTreeItem;
- typedef TCFTreeLevel < TCFLevelDataType 、 TUnitType > TCFCustomUnitTreeLevel;
- typedef void (* TFuncPtr)(TCFCustomUnitDivisionTreeItem *、 const TEvent &);
- typedef TStateMachineDescriptor < TState 、 TEvent > TParSenWordDescriptor;
- / *使用される状態のクラス* /
- TState クラス : public TFunctionalState < TEvent 、 TTextString >
- {
- プライベート :
- TFuncPtr _Function;
- TCFCustomUnitDivisionTreeItem * _TargetTree;
- 公開 :
- void OnEnter( const TState &tStateFrom、 const TEvent &tEvent){
- _Function(_TargetTree、tEvent);
- };
- TState ( const TTextString &tName、TFuncPtr tFunc、TCFCustomUnitDivisionTreeItem * tTargetTree)
- :TFunctionalState < TEvent 、 TTextString >(tName、sat_None)、_Function(tFunc)、_TargetTree(tTargetTree){};
- };
TStateクラスは、その名前が示すとおり、CAの状態です。 その中には、ツリーを構築するための関数へのポインターと、ツリー自体へのポインターがあります。 この関数は、OnEnter()メソッドがTStateから要求されたときに呼び出されます。 このツリーが表すクラス(TCFCustomUnitDivisionTreeItem)はかなり複雑です。これは、より抽象的なテンプレートクラス( [h] )の拡張です。 今はそれに焦点を合わせません。 ツリー構築関数をさらに見てみましょう。
Copy Source | Copy HTML
- void FEmpty(TCFCustomUnitDivisionTreeItem * tItem、 const TEvent&tEvent)
- {
- 帰る
- };
- void FOpenParagraph(TCFCustomUnitDivisionTreeItem * tTree、 const TEventおよびtEvent)
- {
- TCFCustomUnitDivisionTreeItem NewItem( 0 、TRangeItem(tEvent.Iterator()、tEvent.Iterator()));
- tTree-> AddItem( 0 、NewItem);
- };
- void FOpenSentence(TCFCustomUnitDivisionTreeItem * tTree、 const TEventおよびtEvent)
- {
- TCFCustomUnitDivisionTreeItem NewItem( 1 、TRangeItem(tEvent.Iterator()、tEvent.Iterator()));
- tTree-> AddItem( 1 、NewItem);
- };
- // .......
- void FCloseAll(TCFCustomUnitDivisionTreeItem * tTree、 const TEventおよびtEvent)
- {
- FCloseWord(tTree、tEvent);
- FCloseSentence(tTree、tEvent);
- FCloseParagraph(tTree、tEvent);
- };
- // ...およびその他の関数...
すべて1つのタイプがあります。 TStateクラスのこれらの関数を互いに置き換えることができます。したがって、関数のオーバーロードとポリモーフィズムがあります( [1] 、 [2] )。
しかし、これらはすべて主要なものの定義と準備です。 宇宙船の設定を含む特定のオブジェクトを取得する必要がありますよね? このようなオブジェクトを返す関数は次のとおりです。
Copy Source | Copy HTML
- TParSenWordDescriptor記述子(TCFCustomUnitDivisionTreeItem * tTree)
- {
- //初期状態はq0、関数はFEmpty、遷移表は「段落、文、単語」と呼ばれます。
- TParSenWordDescriptor D(TState( "q0" 、FEmpty、tTree)、 "段落、文章、単語" );
- //状態を設定し、それぞれに独自の名前を付けます:q0、q1、... q14。
- //各状態に関数(FEmpty、FOpenAll ...)を渡します。この関数は、状態が発生したときに呼び出す必要があります。
- //また、構築しているツリーへのポインタを渡します-tTree。
- D << TState( "q0" 、FEmpty、tTree)
- << TState( "q1" 、FOpenAll、tTree)
- << TState( "q2" 、FOpenSentenceOpenWord、tTree)
- << TState( "q3" 、FOpenWord、tTree)
- << TState( " q4 " 、FEmpty、tTree)
- << TState( " q5 " 、FCloseWord、tTree)
- << TState( " q6 " 、FCloseSentence、tTree)
- << TState( "q7" 、FCloseSentenceOpenSentenceOpenWord、tTree)
- << TState( "q8" 、FEmpty、tTree)
- << TState( "q9" 、FEmpty、tTree)
- << TState( "q10" 、FCloseWord、tTree)
- << TState( "q11" 、FCloseAll、tTree)
- << TState( "q12" 、FCloseSentenceCloseParagraph、tTree)
- << TState( "q13" 、FCloseParagraph、tTree)
- << TState( "q14" 、FEmpty、tTree);
- //イベントを設定し、それぞれ独自の名前(「WinParagraph」、「SentenceEnd」...)を付けます。
- // cWinParagraph、cSentenceEndはUConstants.hで説明されている文字セットです。
- D << TEvent( "WinParagraph" 、cWinParagraph)
- << "q0" << "q11" << "q11" << "q11" << "q11" << "q12" << "q12" << "q11" << "q12" << "q13" << "q12" << "q0" << "q0" << "q0" << "q12" ;
- D << TEvent( "SentenceEnd" 、cSentenceEnd)
- << "q0" << "q10" << "q10" << "q10" << "q10" << "q14" << "q14" << "q10" << "q14" << "q9" << "q14" << "q0" << "q0" << "q0" << "q14" ;
- D << TEvent( "Letters" 、cLetters + cDigits)
- << "q1" << "q4" << "q4" << "q4" << "q4" << "q3" << "q2" << "q4" << "q3" << "q2" << "q7" << "q1" << "q1" << "q1" << "q7" ;
- D << TEvent( "スペース" 、cSpace + cTab)
- << "q0" << " q5" << "q5" << "q5" << "q5" << "q8" << "q9" << "q5" << "q8" << "q9" << "q6" << "q0" << "q0" << "q0" << "q6" ;
- D << TEvent( "PunctMarksおよびその他のシンボル" 、TCharsSet(cPrintable)>> cWinParagraph >> cSentenceEnd >> cLetters >> cDigits >> cSpace >> cTab)
- << "q0" << " q5" << "q5" << "q5" << "q5" << "q8" << "q9" << "q5" << "q8" << "q9" << "q14" << "q0" << "q0" << "q0" << "q14" ;
- D << TEvent( "EndSign" 、TCharsSet(CTextStringEndSign))
- << "q0" << "q11" << "q11" << "q11" << "q11" << "q12" << "q13" << "q11" << "q12" << "q13" << "q12" << "q0" << "q0" << "q0" << "q12" ;
- return D;
- };
宇宙船のテーブルにイベントや条件がどのように入力されるかは非常にはっきりと見えますが、これらの「<<」記号でテーブル自体を見ることは困難です。 信じてください、ここではすべて記録されています。
TEventクラスを調べることもできます。実際には、TCharsSetEvent( [cpp] 、 [h] )です。 好奇心-ようこそ、宇宙船のマネージャーTFiniteStateMachineManager( [h] )に行きます。 このクラスの目的は次のとおりです。統一されたインターフェイスを使用して、イベントの抽象的なソースに対して抽象的な宇宙船を使用して分析します。 また、マネージャーでは、認識の進行状況をどこかに表示する関数を渡すことができます。 このクラスのコードは非常に豊富であり、最も興味深いセクションのみを示します。
Copy Source | Copy HTML
- テンプレート< クラス EventType 、 クラス FiniteStateMachineType 、 クラス StateMachineDescriptorType 、 クラス SourceType >
- クラス TFiniteStateMachineManager
- {
- プライベート :
- StateMachineDescriptorType _Descriptor;
- SourceType _Source;
- TUInt _Begin;
- TUInt _End;
- TUInt _Iterator;
- 公開 :
- // <......>
- TFiniteStateMachineManager ( const StateMachineDescriptorType &tDescriptor、 const SourceType &tSource)
- :
- _Descriptor(tDescriptor)、
- _Source(tSource)、
- _ReportStep( 0 )、
- _ProgressFunc(NULL)
- {};
- // <......>
繰り返しますが、テンプレートを扱っています。 正確には、「戦略」パターンもここに示されています。 戦略に関する有限状態マシンだけでなく、何らかのマネージャーもあります! 私は隠していません。この同じマネージャーを使用してハーモニーカードを作成します。これは、別のステートマシン、特別なマシンでのみ機能します。 実際、彼は、インターフェイスが同じである場合、どんな種類の宇宙船が滑るのか気にしません。 マネージャーは、この「スリップ」した宇宙船を受け取り、すべての作業を彼にリダイレクトします。 1つの宇宙船がサウンドルールを処理し、もう1つがテキストをレベルに分割します。 これが「戦略」パターンの本質です。実際の作業を行うオブジェクト戦略でクラスを構成します。 たとえば、イベントが発生したときにフィルターをトリガーする必要がある場合、フィルターを戦略として実装する別のクラスを渡すことで、EventTypeイベントを置き換えることができます。
Copy Source | Copy HTML
- TFiniteStateMachineManagerおよびプロセス()
- {
- _Begin = _Source.Begin();
- _End = _Source.End();
- FiniteStateMachineType _Machine(_Descriptor.StartState()、_ Descriptor.Proxy());
- for (_Iterator = _Begin; _Iterator <= _ End; _Iterator ++)
- {
- _Machine << EventType(_Source [_Iterator]、_Iterator);
- };
- _Machine << EventType(_Source.EndSign()、_End + 1 );
- return * this ;
- };

Process()関数は簡単です。 _Machineステートマシンオブジェクトが作成され、記述子(遷移テーブル、初期状態)によって構成され、ループ内の認識プロセスが開始されます。 EventTypeクラスに2番目のパラメーターとして渡される「イテレーター」は、テキスト内の文字のインデックスを示します。 イベントがトリガーされた場合、たとえば「Start Word」状態の場合、インデックスはBeginIndexとしてRangeItemに書き込まれます。 逆もまた同様です。イベントが単語(文、段落)の終わりである場合、インデックスはこの範囲で最終的になります。 したがって、テキストに関連するRangeItemのリストを取得します。
「イテレータ」という言葉を引用したのはなぜですか? これは実際のイテレータではなく、単なる整数変数だからです。 しかし、真に汎用的なマネージャーを作成する必要がある場合は、イベントオブジェクトから抽象化する必要があります。 マネージャーは、データがそこに格納されているリスト、テーブル、または配列、それらがどこから来たのか、要素の数、およびそれらの順序についてまったく心配する必要はありません。 そして、それらを確実に通過させるには、Iteratorパターン(Iterator、 [1] 、 [2] )を使用できます。 ループをイテレーターで機能させるために大きな変更は必要ありません。
Copy Source | Copy HTML
- // ......
- SourceType * _Source;
- // ......
- SourceType ::イテレータ_Iterator = _Source-> Begin();
- for (; _Iterator!= _Source-> End(); ++ _ Iterator)
- {
- _Machine << EventType(* _ Iterator);
- };

この宇宙船の結果は、テキスト分割ツリーになります。 次に、これらのパーティションから比較するセクションを選択する必要があります。 アルゴリズムは非常に簡単です。
-テキストごとに、セクションは長さが増加する順にソートされます。
-最小限のコンプライアンステンプレートがコンパイルされます。 すべてのテキストから同じ長さのセクションをいくつ取得できるかを規定しています。 すべてのテキストに1000の長さの10の段落があり、そのような段落の1つのテキストに8しかない場合、最小一致は8セクションです。 だから-各長さ。
-テンプレートに従って、テキストのセクションが選択されます。
-アルゴリズムには、段落、文、単語のすべてのレベルが順番に含まれます。
これでサンプルが得られ、特性の収集を開始できます。