9
9
10
10
#include " utilities/FileUtils.h"
11
11
#include " utilities/Logger.h"
12
+ #include " utilities/StringComparer.h"
12
13
#include " utilities/XMLUtils.h"
13
14
15
+ #include < algorithm>
14
16
#include < chrono>
15
17
#include < regex>
18
+ #include < set>
16
19
#include < thread>
17
20
18
21
#include < kodi/tools/StringUtils.h>
@@ -433,71 +436,102 @@ PVR_ERROR Epg::GetEPGForChannel(int channelUid, time_t epgWindowStart, time_t ep
433
436
return PVR_ERROR_NO_ERROR;
434
437
}
435
438
436
- namespace
437
- {
438
- bool TvgIdMatchesCaseOrNoCase (const std::string& idOne, const std::string& idTwo, bool ignoreCaseForEpgChannelIds)
439
- {
440
- if (ignoreCaseForEpgChannelIds)
441
- return StringUtils::EqualsNoCase (idOne, idTwo);
442
- else
443
- return idOne == idTwo;
444
- }
445
- }
446
-
447
439
ChannelEpg* Epg::FindEpgForChannel (const std::string& id) const
448
440
{
449
441
for (auto & myChannelEpg : m_channelEpgs)
450
442
{
451
- if (TvgIdMatchesCaseOrNoCase (myChannelEpg.GetId (), id, m_settings->IgnoreCaseForEpgChannelIds ()))
443
+ if (m_settings->IgnoreCaseForEpgChannelIds ())
444
+ {
445
+ if (StringUtils::EqualsNoCase (myChannelEpg.GetId (), id))
446
+ return const_cast <ChannelEpg*>(&myChannelEpg);
447
+ }
448
+ else if (myChannelEpg.GetId () == id)
449
+ {
452
450
return const_cast <ChannelEpg*>(&myChannelEpg);
451
+ }
453
452
}
454
453
455
454
return nullptr ;
456
455
}
457
456
458
457
ChannelEpg* Epg::FindEpgForChannel (const Channel& channel) const
459
458
{
460
- for (auto & myChannelEpg : m_channelEpgs)
461
- {
462
- if (TvgIdMatchesCaseOrNoCase (myChannelEpg.GetId (), channel.GetTvgId (), m_settings->IgnoreCaseForEpgChannelIds ()))
463
- return const_cast <ChannelEpg*>(&myChannelEpg);
464
- }
459
+ double maxSimilarity = 0.0 ;
460
+ ChannelEpg* bestMatch = nullptr ;
465
461
466
462
for (auto & myChannelEpg : m_channelEpgs)
467
463
{
468
- for (const DisplayNamePair& displayNamePair : myChannelEpg.GetDisplayNames ())
464
+ double similarity = utilities::StringComparer::SorensenDiceSimilarity (
465
+ myChannelEpg.GetId (), channel.GetTvgId (), m_settings->IgnoreCaseForEpgChannelIds ());
466
+
467
+ if (similarity > maxSimilarity)
469
468
{
470
- if (StringUtils::EqualsNoCase (displayNamePair.m_displayNameWithUnderscores , channel.GetTvgName ()) ||
471
- StringUtils::EqualsNoCase (displayNamePair.m_displayName , channel.GetTvgName ()))
472
- return const_cast <ChannelEpg*>(&myChannelEpg);
469
+ maxSimilarity = similarity;
470
+ bestMatch = const_cast <ChannelEpg*>(&myChannelEpg);
471
+
472
+ if (maxSimilarity == 1.0 )
473
+ break ;
473
474
}
474
475
}
476
+
477
+ if (bestMatch != nullptr )
478
+ return bestMatch;
475
479
476
480
for (auto & myChannelEpg : m_channelEpgs)
477
481
{
478
482
for (const DisplayNamePair& displayNamePair : myChannelEpg.GetDisplayNames ())
479
483
{
480
- if (StringUtils::EqualsNoCase (displayNamePair.m_displayName , channel.GetChannelName ()))
481
- return const_cast <ChannelEpg*>(&myChannelEpg);
484
+ double similarity = utilities::StringComparer::SorensenDiceSimilarity (
485
+ displayNamePair.m_displayName , channel.GetTvgName (),
486
+ m_settings->IgnoreCaseForEpgChannelIds ());
487
+
488
+ if (similarity == 0.0 )
489
+ similarity = utilities::StringComparer::SorensenDiceSimilarity (
490
+ displayNamePair.m_displayNameWithUnderscores , channel.GetTvgName (),
491
+ m_settings->IgnoreCaseForEpgChannelIds ());
492
+
493
+ if (similarity == 0.0 )
494
+ similarity = utilities::StringComparer::SorensenDiceSimilarity (
495
+ displayNamePair.m_displayName , channel.GetChannelName (),
496
+ m_settings->IgnoreCaseForEpgChannelIds ());
497
+
498
+ if (similarity > maxSimilarity)
499
+ {
500
+ maxSimilarity = similarity;
501
+ bestMatch = const_cast <ChannelEpg*>(&myChannelEpg);
502
+
503
+ if (maxSimilarity == 1.0 )
504
+ break ;
505
+ }
482
506
}
507
+ if (maxSimilarity == 1.0 )
508
+ break ;
483
509
}
484
510
485
- return nullptr ;
511
+ return bestMatch ;
486
512
}
487
513
488
514
ChannelEpg* Epg::FindEpgForMediaEntry (const MediaEntry& mediaEntry) const
489
515
{
490
516
for (auto & myChannelEpg : m_channelEpgs)
491
517
{
492
- if (TvgIdMatchesCaseOrNoCase (myChannelEpg.GetId (), mediaEntry.GetTvgId (), m_settings->IgnoreCaseForEpgChannelIds ()))
518
+ if (m_settings->IgnoreCaseForEpgChannelIds ())
519
+ {
520
+ if (StringUtils::EqualsNoCase (myChannelEpg.GetId (), mediaEntry.GetTvgId ()))
521
+ return const_cast <ChannelEpg*>(&myChannelEpg);
522
+ }
523
+ else if (myChannelEpg.GetId () == mediaEntry.GetTvgId ())
524
+ {
493
525
return const_cast <ChannelEpg*>(&myChannelEpg);
526
+ }
494
527
}
495
528
496
529
for (auto & myChannelEpg : m_channelEpgs)
497
530
{
498
531
for (const DisplayNamePair& displayNamePair : myChannelEpg.GetDisplayNames ())
499
532
{
500
- if (StringUtils::EqualsNoCase (displayNamePair.m_displayNameWithUnderscores , mediaEntry.GetTvgName ()) ||
533
+ if (StringUtils::EqualsNoCase (displayNamePair.m_displayNameWithUnderscores ,
534
+ mediaEntry.GetTvgName ()) ||
501
535
StringUtils::EqualsNoCase (displayNamePair.m_displayName , mediaEntry.GetTvgName ()))
502
536
return const_cast <ChannelEpg*>(&myChannelEpg);
503
537
}
@@ -511,7 +545,7 @@ ChannelEpg* Epg::FindEpgForMediaEntry(const MediaEntry& mediaEntry) const
511
545
if (StringUtils::EqualsNoCase (displayNamePair.m_displayName , mediaEntry.GetM3UName ()))
512
546
return const_cast <ChannelEpg*>(&myChannelEpg);
513
547
}
514
- }
548
+ }
515
549
516
550
return nullptr ;
517
551
}
@@ -522,7 +556,7 @@ void Epg::ApplyChannelsLogosFromEPG()
522
556
523
557
for (const auto & channel : m_channels.GetChannelsList ())
524
558
{
525
- const ChannelEpg* channelEpg = FindEpgForChannel (channel);
559
+ const ChannelEpg* channelEpg = FindEpgForChannel (channel. GetTvgName () );
526
560
if (!channelEpg || channelEpg->GetIconPath ().empty ())
527
561
continue ;
528
562
0 commit comments