我想比較兩個字符串忽略幾個字(比如三個)。比較兩個字符串忽略小變化
一樣,如果我這兩個字符串比較:
"Hello! My name is Alex Jolig. Its nice to meet you."
"My name is Alex. Nice to meet you."
我應該得到的結果True
。
有沒有辦法做到這一點?
我想比較兩個字符串忽略幾個字(比如三個)。比較兩個字符串忽略小變化
一樣,如果我這兩個字符串比較:
"Hello! My name is Alex Jolig. Its nice to meet you."
"My name is Alex. Nice to meet you."
我應該得到的結果True
。
有沒有辦法做到這一點?
在我的腦海裏沒有任何內置的,但我認爲你可以使用分隔符(''在你的情況下)標記兩個字符串&標點符號(!&。在你的情況)。
一旦這兩個字符串按照有序令牌分解,您可以按照您的要求應用單個令牌之間的比較。
你可以將字符串拆分爲單詞並比較它們;
private bool compareStrings()
{
string stringLeft = "Hello! My name is Alex Jolig. Its nice to meet you.";
string stringRight = "My name is Alex. Nice to meet you.";
List<string> liLeft = stringLeft.Split(' ').ToList();
List<string> liRight = stringRight.Split(' ').ToList();
double totalWordCount = liLeft.Count();
double matchingWordCount = 0;
foreach (var item in liLeft)
{
if(liRight.Contains(item)){
matchingWordCount ++;
}
}
//return bool based on percentage of matching words
return ((matchingWordCount/totalWordCount) * 100) >= 50;
}
這將返回基於匹配單詞的百分比一個布爾值,則可能需要使用正則表達式或類似的替換一些格式字符更準確的結果。
在codeproject上有一篇關於模糊字符串與編輯距離匹配的文章 您可以擴展這個想法以滿足您的需求。它使用Levenshtein的編輯距離作爲模糊字符串匹配。
http://www.codeproject.com/Articles/162790/Fuzzy-String-Matching-with-Edit-Distance
哎,所以我在這裏是我的答案去。
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace ConsoleApplication1
{
class Program
{
static void Main(string[] args)
{
var string1 = "Hi thar im a string";
var string2 = "Hi thar im a string";
var string3 = "Hi thar im a similar string";
var string4 = "im a really different string";
var string5 = "Hi thar im a string but have many different words";
Console.WriteLine(StringComparo(string1, string2));
Console.WriteLine(StringComparo(string1, string3));
Console.WriteLine(StringComparo(string1, string4));
Console.WriteLine(StringComparo(string1, string5));
Console.ReadLine();
}
public static bool StringComparo(string str1, string str2, int diffCounterLimiter = 3)
{
var counter = 0;
var arr1 = str1.Split(' ');
var arr2 = str2.Split(' ');
while (counter <= diffCounterLimiter)
{
TreeNode bestResult = null;
for (int i = 0; i < arr1.Length; i++)
{
for (int j = 0; j < arr2.Length; j++)
{
var result = new TreeNode() { arr1Index = i, arr2Index = j };
if (string.Equals(arr1[i], arr2[j]) && (bestResult == null || bestResult.diff < result.diff))
{
bestResult = result;
}
}
}
// no result found
if(bestResult == null)
{
// any left over words plus current counter
return arr1.Length + arr2.Length + counter <= diffCounterLimiter;
}
counter += bestResult.diff;
arr1 = arr1.Where((val, idx) => idx != bestResult.arr1Index).ToArray();
arr2 = arr2.Where((val, idx) => idx != bestResult.arr2Index).ToArray();
}
return false;
}
}
public class TreeNode
{
public int arr1Index;
public int arr2Index;
public int diff => Math.Abs(arr1Index - arr2Index);
}
}
我試圖實現樹搜索(我知道它不是一個真正的搜索樹,我可能會重寫它)。
實質上,它找到每個字符串中最接近的匹配元素。雖然在3個差異的限制下,它刪除了匹配它們的元素,增加了差異並重復。希望能幫助到你。
這似乎幾乎不可能沒有一些重要的工作在自然語言分析程序... – Steve
它不可能通過句子。你可以檢查一個一個字。 – andy
你想忽略兩個字符串中的單詞嗎?例如,如果第二個字符串是'嗨!我的名字叫亞歷克斯。很高興見到你。' –