我需要按字比較兩個字符串。 有點像差異,但對於文字而言,不適用於線條。逐字比較.NET中兩個字符串的比較
在結果我想返回的話指數,這是兩個不同的字符串的兩個陣列完成。
是否有任何庫/框架/ standalone_methods的.NET可以做到這一點?
P.S.我想比較幾個千字節的文本
我需要按字比較兩個字符串。 有點像差異,但對於文字而言,不適用於線條。逐字比較.NET中兩個字符串的比較
在結果我想返回的話指數,這是兩個不同的字符串的兩個陣列完成。
是否有任何庫/框架/ standalone_methods的.NET可以做到這一點?
P.S.我想比較幾個千字節的文本
看來我已經找到所需的解決方案:
DiffPlex是一個.NET版本比較圖書館既具有Silverlight和HTML差異閱讀器的組合。 http://diffplex.codeplex.com/
但它有一個錯誤。在「Hello-Kitty」「Hello - Kitty」這兩行中,單詞「Hello」將被標記爲差異。雖然區別是空間符號。
您可以使用唯一的數字替換2個文本中的所有單詞,爲編輯距離計算取一些現成的代碼並將其字符替換爲字符比較,完成了!
我不確定是否存在任何你想要的庫。但是你肯定會找到很多編輯距離的代碼。
此外,取決於您是否想要在編輯距離計算中實際允許替換,您可以更改動態編程代碼中的條件。
其實我已經寫過比較例程,但我不喜歡它是如何工作的,因爲新的錯誤不時出現,但我沒有太多時間去爭取,因爲這是所有功能的和平。這就是爲什麼我一直在尋找已經經過良好測試的東西。它的風趣,但似乎這樣的事情不存在:) – 2009-11-24 00:57:15
@亞歷克斯︰見我的回答上面:) – Pedery 2010-11-08 12:16:18
你可以試試這個,雖然我不知道這是你在找什麼StringUtils.difference()(http://commons.apache.org/lang/api-release/org/apache/commons/lang/StringUtils.html#difference%28java.lang.String,%20java.lang.String%29)
或者,在Eclipse(eclipse.org)項目具有差異比較功能,意味着他們還必須有代碼來確定差異,您可以瀏覽他們的API或來源以查看您可以找到的內容。
祝你好運。
使用RegularExpressions使用/端口算法。
就像在例如:
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using System.Collections.Specialized;
namespace WindowsApplication10
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
private void button2_Click(object sender, EventArgs e)
{
decimal discrimation = 0.75M;
string formHeading = "The brown dog jumped over the red lazy river, and then took a little nap! Fun!";
string userSearch = "The brown dog jumped over the red lazy river, and then took a little ";
//string userSearch = "brown dog nap fun";
decimal res = CompareText(formHeading, userSearch);
if (res >= discrimation)
{
MessageBox.Show("MATCH!" + res.ToString());
}
else
{
MessageBox.Show("does not match! " + res.ToString());
}
}
/// <summary>
/// Returns a percentage of 1 on how many words were matched
/// </summary>
/// <returns></returns>
private decimal CompareText(string formHeading, string userSearch)
{
StringCollection formHeadingWords = new StringCollection();
StringCollection userSearchWords = new StringCollection();
formHeadingWords.AddRange(System.Text.RegularExpressions.Regex.Split(formHeading, @"\W"));
userSearchWords.AddRange(System.Text.RegularExpressions.Regex.Split(userSearch, @"\W"));
int wordsFound = 0;
for (int i1 = 0; i1 < userSearchWords.Count; i1++)
{
if (formHeadingWords.Contains(userSearchWords[i1]))
wordsFound += 1;
}
return (Convert.ToDecimal(wordsFound)/Convert.ToDecimal(formHeadingWords.Count));
}
}
}
其實,你可能想實現的,我們在DNA sequence alignments使用局部對齊/全球校準算法的變化。這是因爲你可能無法逐字比較兩個字符串。即:
敏捷的棕色狐狸跳過 懶狗
快速狐狸躍過 懶狗
換句話說,如果你不能識別整個單詞的插入和刪除,你的比較算法可能變得非常sc(r)ewed。請看一下Smith-Waterman算法和Needleman-Wunsch算法,並找到一種方法使其適應您的需求。由於如果字符串很長,這樣的搜索空間可能變得非常大,您還可以查看BLAST。BLAST是一種非常常見的啓發式算法,幾乎是基因搜索的標準。
我沒有得到,爲什麼我不能逐字比較兩個字符串?我想要的就像你說的 - 識別整個單詞的插入和刪除。 – 2010-11-12 14:19:28
因爲如果逐字比較,您的比較算法可能會很快變得非常複雜。上面的例子是微不足道的,但說明了這一點我提出的序列算法被設計用於識別可比序列中的空位和插入。 PS:不要忘記獎勵你認爲有幫助的答案。畢竟,這就是這個社區如何保持活力。點擊有用答案旁邊的向上箭頭圖像。 – Pedery 2010-11-20 02:56:31
c#的另一個庫是diff-match-patch - http://code.google.com/p/google-diff-match-patch/。
它發現字符差異的壞事。好的,有指示你必須添加以區分這些單詞。
重複http://stackoverflow.com/questions/473522/word-comparison-algorithm – 2009-11-23 22:20:38
首先,將字符串分解爲兩個單詞數組。然後找到兩個數組中相同的字符串非常簡單。如果你能做到這一點,那麼當然你可以找到不同的單詞。這是JScript中的一個簡單示例;把它變成C#只需要幾分鐘。 http://beta.blogs.msdn.com/ericlippert/archive/2004/07/21/recursion-and-dynamic-programming.aspx – 2009-11-23 23:12:02