32 #include <QStringList>
33 #include <QStringMatcher>
34 #include <QStringBuilder>
35 #include <QLinkedList>
54 QStringList
calcLCS(
const QStringList& s1Words,
55 const QStringList& s2Words,
56 const QStringList& s1Space,
57 const QStringList& s2Space
70 LCSprinter(
const QStringList &s_1,
71 const QStringList& s_2,
72 QVector<LCSMarker> *b_,
75 const QStringList& s1Space_,
76 const QStringList& s2Space_
79 void printLCS(uint index);
80 inline QStringList operator()();
84 QLinkedList<QString> resultString;
85 QStringList s1Space, s2Space;
86 QStringList::const_iterator it1, it2;
87 QStringList::const_iterator it1Space, it2Space;
90 QVector<LCSMarker> *b;
95 QStringList LCSprinter::operator()()
98 foreach(
const QString& str, resultString)
106 LCSprinter::LCSprinter(
const QStringList& s_1,
107 const QStringList& s_2,
108 QVector<LCSMarker> *b_,
111 const QStringList& s1Space_,
112 const QStringList& s2Space_
118 , it1(s1.constBegin())
119 , it2(s2.constBegin())
120 , it1Space(s1Space.constBegin())
121 , it2Space(s2Space.constBegin())
126 haveSpaces=!s1Space_.isEmpty();
136 result.prepend(QString(str.at(i)));
137 result.prepend(QString());
141 void LCSprinter::printLCS(uint index)
144 if (index % nT == 0 || index < nT)
147 uint bound = index%nT;
148 for (index=0; index<bound; ++index)
151 resultString.append(*it2);
155 resultString.append(*it2Space);
166 printLCS(index-nT-1);
167 if (it1!=s1.constEnd())
175 resultString.append(*it1);
182 resultString.append(
calcLCS(word1,word2,empty,empty).join(QString()));
185 if((*it1Space)==(*it2Space))
186 resultString.append(*it1Space);
196 empty=
calcLCS(word2,word1,empty,empty);
197 empty.replaceInStrings(
"KBABELADD>",
"KBABELTMP>");
198 empty.replaceInStrings(
"KBABELDEL>",
"KBABELADD>");
199 empty.replaceInStrings(
"KBABELTMP>",
"KBABELDEL>");
201 resultString.append(empty.join(QString()));
208 resultString.append(*it1);
221 resultString.append(*it1);
225 resultString.append(*it1Space);
235 resultString.append(*it2);
240 resultString.append(*it2Space);
251 QStringList
calcLCS(
const QStringList& s1Words,
252 const QStringList& s2Words,
253 const QStringList& s1Space,
254 const QStringList& s2Space
261 uint mX = s1Words.count();
262 uint nY = s2Words.count();
266 QStringList s1(s1Words);
267 QStringList s2(s2Words);
269 if (!s1Space.isEmpty())
276 s1[i]=s1.at(i).toLower();
278 s2[i]=s2.at(i).toLower();
279 #if 0 //i'm too lazy...
284 if ((s1Space.at(i)==accel))
290 s1Words.removeAt(i+1);
302 QVector<LCSMarker> b(mT*nT,
NOTHING);
303 QVector<uint> c(mT*nT, 0);
308 uint index_cache = 0;
309 QStringList::const_iterator it1, it2;
311 for (i=1, it1 = s1.constBegin(); i<mT; ++i, ++it1)
313 for (j=1, it2 = s2.constBegin(); j<nT; ++j, ++it2)
315 index_cache = i*nT+j;
318 c[index_cache] = c.at(index_cache-nT-1) + 1;
321 else if (c.at(index_cache-nT) >= c.at(index_cache-1))
323 c[index_cache] = c.at(index_cache-nT);
328 c[index_cache] = c.at(index_cache-1);
336 LCSprinter printer(s1Words, s2Words, &b, nT, index_cache, s1Space, s2Space);
344 static QString space(
" ");
348 static QStringList empty;
349 QStringList list=
calcLCS(s1,s2,empty,empty);
350 bool r=list.first()==
" ";
354 qDebug()<<
"first ' ' assumption is wrong"<<list.first();
356 QString result=list.join(QString());
361 result.remove(
"</KBABELADD><KBABELADD>");
362 result.remove(
"</KBABELDEL><KBABELDEL>");
369 static void prepareLists(QString str, QStringList&
main, QStringList& space,
const QString& accel, QString markup)
377 QRegExp rxAccelInWord(
"[^\\W|\\d]"+accel+
"[^\\W|\\d]");
378 int accelLen=accel.size();
379 while ((pos=rxAccelInWord.indexIn(str,pos))!=-1)
381 str.remove(rxAccelInWord.pos()+1,accelLen);
388 if (!markup.isEmpty())
390 QRegExp rxSplit(
'('%markup%
"\\W+|\\d+)+");
392 main=str.split(rxSplit,QString::SkipEmptyParts);
399 while ((pos=rxSplit.indexIn(str,pos))!=-1)
401 space.append(rxSplit.cap(0));
402 pos+=rxSplit.matchedLength();
404 space.append(QString());
405 space.append(QString());
409 const QString& str2ForMatching,
410 const QString& accel,
411 const QString& markup,
415 QStringList s1Space, s2Space;
418 prepareLists(str1ForMatching, s1, s1Space, accel, markup);
419 prepareLists(str2ForMatching, s2, s2Space, accel, markup);
428 QStringList result(
calcLCS(s1,s2,s1Space,s2Space));
429 result.removeFirst();
430 result.first().remove(0,1);
432 result.replaceInStrings(
"<KBABELDEL></KBABELDEL>",
"");
433 result.replaceInStrings(
"<KBABELADD></KBABELADD>",
"");
435 result.replaceInStrings(
"<KBABELADD>",
"{KBABELADD}");
436 result.replaceInStrings(
"</KBABELADD>",
"{/KBABELADD}");
437 result.replaceInStrings(
"<KBABELDEL>",
"{KBABELDEL}");
438 result.replaceInStrings(
"</KBABELDEL>",
"{/KBABELDEL}");
442 result.replaceInStrings(
"&",
"&");
443 result.replaceInStrings(
"<",
"<");
444 result.replaceInStrings(
">",
">");
450 QString res(result.join(QString()));
451 res.remove(
"{/KBABELADD}{KBABELADD}");
452 res.remove(
"{/KBABELDEL}{KBABELDEL}");
456 res.replace(
"{KBABELADD}",
"<font style=\"background-color:"%
Settings::addColor().name()%
";color:black\">");
457 res.replace(
"{/KBABELADD}",
"</font>");
458 res.replace(
"{KBABELDEL}",
"<font style=\"background-color:"%
Settings::delColor().name()%
";color:black\">");
459 res.replace(
"{/KBABELDEL}",
"</font>");
460 res.replace(
"\\n",
"\\n<br>");
QStringList calcLCS(const QStringList &s1Words, const QStringList &s2Words, const QStringList &s1Space, const QStringList &s2Space)
QString wordDiff(QStringList s1, QStringList s2)
This is low-level wrapper used for evaluating translation memory search results.
int main(int argc, char **argv)
QString userVisibleWordDiff(const QString &str1ForMatching, const QString &str2ForMatching, const QString &accel, const QString &markup, int options)
static Project * instance()
static QString delMarkerEnd
static QColor delColor()
Get DelColor.
static QString addMarkerStart
static QColor addColor()
Get AddColor.
static QString addMarkerEnd
static QStringList prepareForInternalDiff(const QString &str)
static QString delMarkerStart
static void prepareLists(QString str, QStringList &main, QStringList &space, const QString &accel, QString markup)