[英]How to find a matching string to a string from one text file in another text file?
我有兩個文本文件。 兩者的內容相同,但格式不同。 在一個文件中,單詞或字母之間有多余的空格。 也有不同的換行符。 例如:
文件1:
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.
文件2:
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
字符串the Knowledge Management
,並且希望將其與“文件2”中的“字符串th e K nowled ge Management
匹配。
我該如何實現? 第二個文件中沒有固定的變形。 唯一可以確定的是,兩個文件中的字符順序相同,並且它們之間可能會被多余的空格隔開,或者它們之間的空格可能會丟失。
我曾考慮應用賣方算法或維特比算法,但是我不確定。 近似字符串匹配也可能很昂貴。
任何線索都將有所幫助。 非常感謝!
您應該意識到,您沒有兩個文本,實際上只有一個文本,所有字符都位於同一位置!
通過什么魔術? 好了,剝離所有空白和分隔符就足夠了,或者,當您從一個字符前進到下一個字符時,最好跳過它們。
您可以輕松地並行遍歷兩個文本,保持同步, 無需搜索 !
例如,兩個“ the Knowledge Management
”和“ th e K nowled ge Management
”從位置45運行至67。
如果您不知道搜索字符串在第一個文本中的開始位置,請在第一個文本中執行普通搜索(有空格或無空格,這取決於您),然后將第二個文本遍歷到相同位置。
The annotation framework we presented is
0 1 2 3
0122345678901223467890122344567890123345
如果您需要在文本中執行多個字符串位置,則每次從頭開始遍歷都會變得很昂貴。 然后,您可以使用將無空格位置與普通位置相關聯的索引表,並在必要時執行二進制搜索。
您可以將文件作為字符串導入,並從這兩個文件中刪除所有空格。 然后,它應該是直接的字符串匹配活動。
如果您還需要匹配模式的起始索引,請獲取折疊字符串中起始點的索引,並在間隔版本上運行一次for循環,僅計算字符數。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.