0

私は2つのテキストファイルを持っています。どちらも同じ内容ですが、それぞれの書式は異なります。 1つのファイルには、単語や文字の間に余分なスペースがあります。異なる改行もあります。たとえば:別のテキストファイルのあるテキストファイルから文字列に一致する文字列を検索するにはどうすればよいですか?

はFile1:

The annotation framework we presented is 
embedded in the Knowledge Management and 
Acquisition Platform Semantic Turkey (Pazienza, et 
al., 2012), and comes out-the-box with a few 
annotation families which differ in the underlying 
annotation model and, notably, in the tasks they 
support. The default handlers take into consideration 
the annotation of atomic ontological resources, and 
complex activities that are provided as macros, e.g. 
the creation of new instances, the definition of new 
subclasses in OWL, or of narrower concepts in 
SKOS. 

File2の:

Theannotationframework we presented is 
embedded in th e K n o w l e d ge Management and 
Acquisition Platform Semantic Turkey (Pazienza, et 
al., 2012), and comes out-the- 
box with a few 
annotation families which differ in the underlying 
annotation model and, notably, in the tasks they 
support. The default handlers take into consideration 
the a n n o t a t i o n o f a t o m i c ontological resources, and 
complex activities that are provided as macros, e.g. 
the creation of new instances, the definition of new 
subclasses in OWL, or of narrower concepts in 
SKOS. 

は、私はファイル1から文字列the Knowledge Managementを選択して、私はFile2の中の文字列th e K n o w l e d ge Managementでそれを一致させたいとします。

どうすれば実現できますか? 2番目のファイルには固定された変形はありません。両方のファイルで文字が同じ順序であり、余分なスペースで区切られている可能性があります。間に余白があると保証できません。

私はSellers AlgorithmまたはViterbi Algorithmを適用することを考えましたが、わかりません。おおよその文字列マッチングは高価かもしれません。

任意の鉛が役立ちます。 ありがとう!

+0

パターンマッチングアルゴリズムを本当に探しているなら、ラビンのKnuth-Morris-Pratt(最も難しいアルゴリズムの1つ)やBoyer-Mooresアルゴリズムを見てください。しかし、あなたの両方のファイルがすべて同じ文字を持っているので、ループで簡単に行うことができます –

答えて

0

ファイルを文字列としてインポートし、両方から空白をすべて削除できます。これは、ストリングに一致するストリングでなければなりません。

また、一致するパターンの開始インデックスが必要な場合は、折りたたまれた文字列の開始点のインデックスを取得し、間隔を空けたバージョンに対してforループを実行し、文字のみをカウントします。

1

あなたは2つのテキストがあるのではなく、実質的に1つの文字が同じ位置にあることに気づかなければなりません。

どのような魔法?まあ、すべての空白と区切り文字を取り除くだけでいいですし、ある文字から次の文字に進むときには空白と区切り文字をスキップしてください。

両方のテキストを並行して簡単にトラバースすることができ、同期しています。検索が不要です!例えば

、「the Knowledge Management」と45位から67

に「 th e K n o w l e d ge Management」実行

あなたが最初のテキストに検索文字列の開始位置がわからない場合は、その後、実行の両方最初のテキストの通常の検索(スペースの有無にかかわらず、それはあなた次第です)、2番目のテキストを同じ位置にトラバースします。

The annotation framework we presented is 
0   1   2   3 
012234567890122346789012234456789

テキストで数多くの文字列の場所を実行する必要がある場合は、最初から毎回を横断することはコストがかかるになります。空白のない場所を通常の場所に関連付ける索引表を使用し、必要に応じてバイナリ検索を実行できます。

+0

洞察力に感謝します。 –

関連する問題