特定のルールに応じて入力テキストをトークン化するプログラムを作成しています。これにはC++を使用しています。
ルール
Letter 'a' should be converted to token 'V-A'
Letter 'p' should be converted to token 'C-PA'
Letter 'pp' should be converted to token 'C-PPA'
Letter 'u' should be converted to token 'V-U'
これは単なるサンプルであり、リアルタイムでこのような約 500 以上のルールがあります。「 appu 」として入力を提供している場合、「 VA + C-PPA + VU 」のようにトークン化する必要があります。これを行うためのアルゴリズムを実装しましたが、正しいことをしていることを確認したかったのです。
アルゴリズム
すべてのルールは、対応するトークンへのマッピングとともに XML ファイルに保持されます。何かのようなもの
<rules>
<rule pattern="a" token="V-A" />
<rule pattern="p" token="C-PA" />
<rule pattern="pp" token="C-PPA" />
<rule pattern="u" token="V-U" />
</rules>
1 - アプリケーションの開始時に、この xml ファイルを読み取り、値を「std::map」に保持します。これは、アプリケーションの終了 (シングルトン パターンの実装) まで使用できます。
2 - 入力テキスト文字を繰り返します。各文字について、一致するものを探します。見つかった場合は、より貪欲になり、入力テキストから次の文字を取得して、より多くの一致を探します。一致しない状態になるまでこれを行います。したがって、入力テキスト ' appu ' については、まず ' a 'に一致するものを探します。見つかった場合は、入力テキストから次の文字を取得して、さらに一致するようにします。そのため、' ap ' との一致を試みますが、一致するものは見つかりませんでした。だからそれはただ戻ってくる。
3 - トークンを取得したので、入力テキストの文字「a」を置き換えます。
4 - 入力テキストの残りの文字で手順 2 と 3 を繰り返します。
手順のより簡単な説明は次のとおりです
input-text = 'appu'
tokens-generated=''
// First iteration
character-to-match = 'a'
pattern-found = true
// since pattern found, going recursive and check for more matches
character-to-match = 'ap'
pattern-found = false
tokens-generated = 'V-A'
// since no match found for 'ap', taking the first success and replacing it from input text
input-text = 'ppu'
// second iteration
character-to-match = 'p'
pattern-found = true
// since pattern found, going recursive and check for more matches
character-to-match = 'pp'
pattern-found = true
// since pattern found, going recursive and check for more matches
character-to-match = 'ppu'
pattern-found = false
tokens-generated = 'V-A + C-PPA'
// since no match found for 'ppu', taking the first success and replacing it from input text
input-text = 'u'
// third iteration
character-to-match = 'u'
pattern-found = true
tokens-generated = 'V-A + C-PPA + V-U' // we'r done!
質問
1 - このアルゴリズムはこの問題に適しているように見えますか、それともこの問題に対処するためのより良い方法はありますか?
2 - これが正しい方法である場合、std::map はここで適切な選択ですか? または、独自のキー/値コンテナーを作成する必要がありますか?
3 - 上記のように文字列をトークン化できるライブラリはありますか?
どんな助けでもいただければ幸いです
:)