C# 比较相似字符串
我有一个带有一些文件名的泛型(LIST1)和另一个带有完整名称列表的大泛型(LIST2)。 我需要将 LIST1 中的名称与 LIST2 中的类似名称进行匹配。 例如,
LIST1
- **MAIZE_SLIP_QUANTITY_3_9.1.aif**
LIST 2
1- TUTORIAL_FAILURE_CLINCH_4.1.aif
2- **MAIZE_SLIP_QUANTITY_3_5.1.aif**
3- **MAIZE_SLIP_QUANTITY_3_9.2.aif**
4- TUTORIAL_FAILURE_CLINCH_5.1.aif
5- TUTORIAL_FAILURE_CLINCH_6.1.aif
6- TUTORIAL_FAILURE_CLINCH_7.1.aif
7- TUTORIAL_FAILURE_CLINCH_8.1.aif
8- TUTORIAL_FAILURE_CLINCH_9.1.aif
9- TUTORIAL_FAILURE_PUSH_4.1.aif
我读过Levenshtein distance 并在框架中使用了它的实现(< a href="http://www.signumframework.com/Others.ashx#StringDistance" rel="nofollow noreferrer">SignumFramework 实用程序)。 它在第 2 行和第 3 行中返回 distance=1。但就我而言,第 3 行比第 2 行匹配得更好。
是否有另一种方法可以更好地比较相似的字符串? 有更灵活的东西吗?
I have a generic with some filenames (LIST1) and another biggeneric with a full list of names (LIST2).
I need to match names from LIST1 to similar ones in LIST2. For example
LIST1
- **MAIZE_SLIP_QUANTITY_3_9.1.aif**
LIST 2
1- TUTORIAL_FAILURE_CLINCH_4.1.aif
2- **MAIZE_SLIP_QUANTITY_3_5.1.aif**
3- **MAIZE_SLIP_QUANTITY_3_9.2.aif**
4- TUTORIAL_FAILURE_CLINCH_5.1.aif
5- TUTORIAL_FAILURE_CLINCH_6.1.aif
6- TUTORIAL_FAILURE_CLINCH_7.1.aif
7- TUTORIAL_FAILURE_CLINCH_8.1.aif
8- TUTORIAL_FAILURE_CLINCH_9.1.aif
9- TUTORIAL_FAILURE_PUSH_4.1.aif
I've read about Levenshtein distance and used an implementation of it in a Framework (SignumFramework Utilities).
It returns me distance=1 in lines 2 and 3. But in my case line 3 is a better match than line 2.
Is there another method better to compare similar strings? Something more flexible?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
当作为字符串进行比较时,对于“9.1”,“9.2”并不比“5.1”更好匹配。 如果您希望以数字方式评估版本号,则必须解析字符串,以便可以分别比较字符串部分和数字部分。
When comparing as strings, "9.2" is not a better match than "5.1" for "9.1". If you want the version numbers to be evaluated numerically, you have to parse the strings so that you can compare the string parts and the numerical parts separately.
您的相似性标准可以是其他几个标准的组合。 一个可以是编辑距离,其他的可以是最长的公共子串或前缀/后缀。
最长公共子串问题实际上是编辑距离的一种特殊情况,当禁止替换并且只有精确字符匹配、插入和删除是允许的编辑操作时(参见此处)。
此处描述了字符串相似性的更多指标。
Your similarity criteria could be a combination of several other criteria. One could be the Levenshtein distance, others might e.g be the longest common substring or prefix/suffix.
The longest common substring problem is actually a special case of edit distance, when substitutions are forbidden and only exact character match, insert, and delete are allowable edit operations (see here).
Further metrics for string similarity are described here.
可以使用正则表达式来获取与名称匹配的项目。 版本号可以收集在匹配的正则表达式组中,并解析为 .net 对象(例如十进制),您可以使用它来比较最接近的版本号。
A regular expression could be used to get the items that match the name. The version number could be collected in a regex group in the match and parsed into a .net object (e.g. decimal) that you could use to compare which one was closest.
这个问题有一套相当详尽的答案。 底部是我提供的 soundex、双变音位、PHP 相似性和 levenstein 的 C# 实现的链接。
There's a fairly exhaustive set of answers to this SO question. At the bottom is link I put up to C# implementations for soundex, double metaphone, PHP similarity and levenstein.