Sort names as Surname, Name - a solution
#1
In a thread of the long bygone year of 2011, a user asked about sorting names as it's usually done: as Surname, Name.
In fact, in a library, to pick something by Shakespeare, one doesn't walk to shelf W; Proust stuff is not lodged in shelf M, either.
But we users of Kodi, when in a mood for a Bergman movie, must look among Directors under I.
That's OK, we all know that name by heart. But if one wants Ozu? Hmm... just google it, it's under Y. Simple, isn't it?
In fact, the one and only answer to the user's plea was
(2011-09-25, 12:20)... Wrote: nope, that"s not possible.

afaik, xbmc has no knowledge of which part of the name is first / sur.
the entire name is treated as a string an the list is sorted by first character.
In fact, unfortunately, neither IMDb nor TMDB care to list names as a pair of forename and surname, so it's not an immediate task to guess what part of the string is the surname.
But it's definitely feasible by users, by processing .nfo files.
To achieve it regular expressions are our friend (despite being quite temperamental, one must admit).

Within a movie's nfo file (a text file in XML) a director entry looks like
Code:
      <director>Forename Middlename Surname</director>
There may be more than one such entry within a file; leading spaces are optional but aid to legibility.

Just remember that
- there are composite surnames (those that contain spaces), something more often found in Portuguese, Spanish and also French and Italian ones, but we could add surnames ending with Jr. and Sr. among those;
- there's at least one case of someone that has a composite surname and uses no forename (Bigas Luna, the late Catalan director).

To deal with composite surnames, an exception table is required - no other way.
To deal with all other cases, it's quite easy.

Using a text processor that supports search/replace with regex expressions, three steps of search/replace in the movie's nfo file will do:

Step 1 (Composite surnames)
Search for
Code:
( {0,})(<director>)(.*|)(Almeida Prado|Azevedo Gomes|Barbosa da Silva|Bigas Luna|Briem Stamm|De Niro|De Palma|De Sica|Ditto P.R.|Fonseca e Costa|González Iñárritu|Mendonça Filho|Moreira Salles|Pereira dos Santos|Prates Correia|São Paulo|Winding Refn)(</director>)
Replace by
Code:
$1$2$4, $3<cleanme>
Note that the search expression contains the known list of composite surnames; to add more, use "|" as separator.

Step 2 (Common names)
Search for
Code:
( {0,})(<director>)(.*) (.*)(</director>)
Replace by
Code:
$1$2$4, $3<cleanme>

Step 3 (Cleanup)
Search for
Code:
(, | |)<cleanme>
Replace by
Code:
</director>

After these steps, the nfo file will have all Director's entries properly changed to Surname, Name.

If you, like me, already have a large amount of nfo files to process and, unlike those of Team Kodi, you're not an expert programmer, recursively scanning your movie folders to search for nfo files and modify them with regex expressions would seem an unsurmountable task.

Not really: TextCrawler, a free utility, comes to the rescue.
It distinguishes itself from others by the ability to cope not only with multiple files, but also with multiple folders at once.
Just enter the search and replace expressions for each step as above and after the third one you'll have a proper list of directors (no googling required anymore).
Before processing the actual folders, I suggest you to use a test file just to be sure - name it test.nfo for instance. Mine was like that:
Code:
<movie>
  <director>Bigas Luna</director>
  <director>Nome2 Meio2 Sobrenome2</director>
  <director>Guilherme de Almeida Prado</director>
  <director>Nome1 Sobrenome1</director>
  <director>Olney São Paulo</director>
  <director>Vittorio De Sica</director>
  <director>Francis Ford Coppola</director>
  <director>Forname Surname</director>
  <director>Nome Sobrenome</director>
</movie>

Apply the three steps and check the results; after that, define all folders where the actual files are and go ahead.
Don't worry! I have successfully modified 1500 files with not a glitch.

p.s.
Kodi could do that itself, couldn't it?
No need to mess with the nfo files, just an option to Display and sort by surname.
As for the exception list, it would consist of additional entries into AdvancedSettings.xml, the syntax perhaps like (again, using my exceptions):
Code:
<CompositeSurnames>
  <surname>Almeida Prado</surname>
  <surname>Azevedo Gomes</surname>
  <surname>Barbosa da Silva</surname>
  <surname>Bigas Luna</surname>
  <surname>Briem Stamm</surname>
  <surname>De Niro</surname>
  <surname>De Palma</surname>
  <surname>De Sica</surname>
  <surname>Ditto P.R.</surname>
  <surname>Fonseca e Costa</surname>
  <surname>González Iñárritu</surname>
  <surname>Mendonça Filho</surname>
  <surname>Moreira Salles</surname>
  <surname>Pereira dos Santos</surname>
  <surname>Prates Correia</surname>
  <surname>São Paulo</surname>
  <surname>Winding Refn</surname>
</CompositeSurnames>


p.s.2 Similar methods can be used for other name entries, like Actors. Just inspect your nfo files to see other entries that contain names.
p.s.3 Depending on the language, expressions may be slightly different: it may be necessary to escape the slash with a backslash (<\/director>) etc. but, if you're a coder, you already know it.
p.s.4 There may definitely be more elegant regex ways than those suggested - I'm just a novice in that branch of the black arts.
p.s 5 This is how my list of Directors looks like after the fix (with Bergman under B):
Image
Reply
#2
Quote:Kodi could do that itself, couldn't it?

Thank-you for the most thoughtful information (and thank-you again for not dredging up a 2011 link). Kodi does nothing by itself, it's the culmination of many hours/weeks/years of great coders that make Kodi the stand-out software we all appreciate today. I see this information as a feature request, and it would probably garner more attention if it was placed in the 'Feature Request' forum. In that regard, I ask your permission to move this thread post to Feature Requests
Reply
#3
(2017-09-02, 18:36)PatK Wrote:
Quote:Kodi could do that itself, couldn't it?
In that regard, I ask your permission to move this thread post to Feature Requests
Permission granted and thank you for the feedback.
Reply
#4
Note that the music library has implemented for Kodi 18 a "sort name" field that can be scraped from music sites and stored in the music database. Something for video library devs to look at, also keeping in mind that for music video content, there is a link between music and music video artist.

scott s.
.
Reply

Logout Mark Read Team Forum Stats Members Help
Sort names as Surname, Name - a solution0