MCPcopy Create free account
hub / github.com/YACReader/yacreader / get_most_common_prefix

Function get_most_common_prefix

common/comic.cpp:957–1020  ·  view source on GitHub ↗

Source from the content-addressed store, hash-verified

955}
956
957QString get_most_common_prefix(const QList<QString> &pageNames)
958{
959 if (pageNames.isEmpty()) {
960 return "";
961 }
962 QMap<QString, uint> frequency;
963 int currentPrefixLenght = pageNames.at(0).split('/').last().length();
964 int currentPrefixCount = 1;
965
966 int i;
967 QString previous;
968 QString current;
969 for (i = 1; i < pageNames.length(); i++) {
970 int pos = 0;
971 previous = pageNames.at(i - 1).split('/').last();
972 current = pageNames.at(i).split('/').last();
973 for (; pos < current.length() && previous[pos] == current[pos]; pos++)
974 ;
975
976 if (pos < currentPrefixLenght && pos > 0) {
977 frequency.insert(previous.left(currentPrefixLenght), currentPrefixCount);
978 currentPrefixLenght = pos;
979 currentPrefixCount++;
980 }
981 /*
982 else if(pos > currentPrefixLenght)
983 {
984 frequency.insert(pageNames.at(i-1).left(currentPrefixLenght), currentPrefixCount - 1);
985 currentPrefixLenght = pos;
986 currentPrefixCount = 2;
987 }*/
988 else if (pos == 0) {
989 frequency.insert(previous.left(currentPrefixLenght), currentPrefixCount);
990 currentPrefixLenght = current.length();
991 currentPrefixCount = 1;
992 } else {
993 currentPrefixCount++;
994 }
995 }
996
997 frequency.insert(previous.left(currentPrefixLenght), currentPrefixCount);
998
999 uint maxFrequency = 0;
1000 QString common_prefix = "";
1001 auto keys = frequency.keys();
1002 for (QString &key : keys) {
1003 if (maxFrequency < frequency.value(key)) {
1004 maxFrequency = frequency.value(key);
1005 common_prefix = key;
1006 }
1007 }
1008
1009 QRegularExpression allNumberRegExp("\\A\\d+\\z");
1010 if (allNumberRegExp.match(common_prefix).hasMatch()) {
1011 return "";
1012 }
1013
1014 if (maxFrequency < pageNames.length() * 0.60) // the most common tipe of image file should a proper page, so we can asume that the common_prefix should be in, at least, the 60% of the pages

Callers 1

get_double_pagesFunction · 0.85

Calls 4

isEmptyMethod · 0.80
splitMethod · 0.80
leftMethod · 0.80
insertMethod · 0.45

Tested by

no test coverage detected