[prev in list] [next in list] [prev in thread] [next in thread]
List: amarok
Subject: Re: Error when calling Amarok::decapitateString()
From: Nicholas Sinlock <isolatedincident () gmail ! com>
Date: 2009-01-17 1:15:00
Message-ID: 49713114.8090002 () gmail ! com
[Download RAW message or body]
Bart Cerneels wrote:
> On Thu, Jan 8, 2009 at 10:56 PM, Pavel Shved <pavel.shved@gmail.com> wrote:
>
>> Mark Kretschmann wrote:
>>
>>
>>> Cool. It would be great if you could send us a patch when you are done
>>> fixing it :)
>>>
>> Hm, now i tihnk i should've just sent a patch instead of long description.
>>
>> Please, check the file attached and test cases there--in case i
>> misunderstood the function's purpose. If it's OK, commit it to appropriate
>> place; don't think you allow anonymous users into your svn.
>>
>>
>>
>>
>> #include <iostream>
>>
>> #include <qt4/QtCore/QString>
>>
>> /* Strip the common prefix of two strings from the first one and trim
>> * whitespaces from the beginning of the resultant string.
>> * Case-insensitive.
>> *
>> * @param input the string being processed
>> * @param ref the string used to determine prefix
>> */
>> QString decapitateString( const QString &input, const QString &ref )
>> {
>> //Sometimes it's good to be c-like...
>> int len; //the length of common prefix calculated so far
>> for (len = 0; len<input.length() && len<ref.length(); len++){
>> if (input.at(len).toUpper() != ref.at(len).toUpper())
>> break;
>> }
>>
>> return input.right(input.length() - len).trimmed();
>> }
>>
>>
>>
>> int main()
>> {
>> using namespace std;
>> //let's test it
>> QString p[][3] = {
>> //argument1, argument2, correct answer
>> {"simple prefix","simple test","prefix"},
>> {"just", "test", "just"},
>> {"ContainsOther","Contains","Other"},
>> {"Contains","ContainsOther",""},
>> {"Equals","Equals",""},
>> {"","",""},
>> {"","AnotherIsEmpty",""},
>> {"AnotherIsEmpty","","AnotherIsEmpty"},
>> {"","something",""},
>> {"something","","something"},
>> {" \t "," whatever",""},
>> {"sEvERal wOrDs in One StrinG","several words indeed!","One StrinG"},
>> {"sEvERal wOrDs -- now it's more simple","several words, yeah!","--
>> now it's more simple"},
>> };
>> int const num = 13;
>> int failed = num;
>> for (int i = 0; i<num ; i++){
>> cout << "Test "<<(i+1)<<" ";
>> QString res = decapitateString(p[i][0],p[i][1]);
>> if (res == p[i][2]){
>> cout <<"ok"<<endl;
>> failed--;
>> }else{
>> cout <<"FAIL:
>> "<<"f(`"<<p[i][0].toStdString()<<"',`"<<p[i][1].toStdString()<<"')"<<endl
>> <<" yields "<<res.toStdString()<<endl
>> <<" must be "<<p[i][2].toStdString()<<endl;
>> }
>> }
>> cerr << failed << " tests failed."<<endl;
>> return failed;
>> }
>>
>>
>
> Hey Pavel,
>
> If you can also solve these cases I'll probably use your code.
>
> {"This Podcast","This Podcast: January 14, 2009","January 14, 2009"}
> {"Podcast Title","Podcast Title #001","#001"}
> {"The funnycast","Funnycast 042: The Answer","042: The Answer"}
>
>
> Perhaps you have suggestions of how to solve these without false positives:
> {"::Title with ascii art::","::Title with ascii art:: for January 14,
> 2009","January 14, 2009"}
>
> Look here to find more examples of the "creative" naming Podcasters
> practice: http://podcastpickle.com/index/featured.php?id=top100audio
>
> Remember, the reason we have decapitateString is because the
> PlaylistBrowser has a limited width. We try to strip common data in
> titles so we don't lose all relevant info to
> QFontMetrics::elidedText().
>
> Bart
> _______________________________________________
> Amarok mailing list
> Amarok@kde.org
> https://mail.kde.org/mailman/listinfo/amarok
>
>
I don't know if you're still looking for that code, but I felt like
trying my hand at the problem. The solution is attached
and it passed all of the tests except for your last "::title with ascii
art::" problem, which I could not think of a good solution
to.
Nicholas Sinlock
["main.cpp" (text/plain)]
/***************************************************************************
* Copyright (C) 2009 by Nicholas Sinlock *
* nsinlock@linux-1k4n *
* *
* This program is free software; you can redistribute it and/or modify *
* it under the terms of the GNU General Public License as published by *
* the Free Software Foundation; either version 2 of the License, or *
* (at your option) any later version. *
* *
* This program is distributed in the hope that it will be useful, *
* but WITHOUT ANY WARRANTY; without even the implied warranty of *
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the *
* GNU General Public License for more details. *
* *
* You should have received a copy of the GNU General Public License *
* along with this program; if not, write to the *
* Free Software Foundation, Inc., *
* 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. *
***************************************************************************/
#include <QString>
#include <QChar>
#include <QStringList>
#include <iostream>
#include <QtCore>
QString channelList[] = { "This Podcast", "Podcast Title", "The funnycast", "::Title \
with ascii art::" }; QString episodeList[] = { "This Podcast: January 14, 2009", \
"Podcast Title #001", "Funnycast 042: The Answer", "::Title with ascii art:: for \
January 14, 2009" }; QString correctAnswerList[] = { "January 14, 2009", "#001", \
"042: The Answer", "January 14, 2009" };
//QStringList badPredicateList;
QString decapitateString( const QString &input, const QString &ref )
{
QString clean;
int length;
if( input.startsWith( ref, Qt::CaseInsensitive ) ) // if the episode title \
begins with the channel title... {
clean = input.right( input.length() - ( ref.length() ) ).trimmed(); // \
...remove the channel title from the episode title }
else // otherwise search for any phrases which are common among the channel title \
and episode title and remove them from the episode title {
for( length = 0; length < ref.length(); length++ )
{
if( ref[length].isSpace() && input.startsWith( ref.right( ref.length() - \
(length + 1)), Qt::CaseInsensitive )) {
clean = input.right( input.length() - (ref.length() - length) ).trimmed();
break;
}
}
}
for( length = 0; length < clean.length(); length++ ) // remove any extraneous \
characters from the beginning of the episode title such as extra punctuation symbols \
{ if( clean[length].isLetterOrNumber() || clean[length] == '#')
{
clean = clean.right( clean.length() - length );
//std::cout << "Clean Version: " << clean.toStdString() << std::endl;
break;
}
}
std::cout << "Clean Version: " << clean.toStdString() << std::endl;
return clean.trimmed();
}
int main(int argc, char *argv[])
{
QString cleanString;
for( int i = 0; i < 5; i++ )
{
cleanString = decapitateString( episodeList[i], channelList[i] );
if( cleanString.toUpper() != correctAnswerList[i].toUpper() )
{
std::cout << "Wrong Answer" << std::endl;
std::cout << "Correct Answer: " << correctAnswerList[i].toStdString() << \
std::endl << std::endl; }
else
std::cout << "Right Answer\n" << std::endl;
}
return 0;
}
_______________________________________________
Amarok mailing list
Amarok@kde.org
https://mail.kde.org/mailman/listinfo/amarok
[prev in list] [next in list] [prev in thread] [next in thread]
Configure |
About |
News |
Add a list |
Sponsored by KoreLogic