Beefy Boxes and Bandwidth Generously Provided by pair Networks
Pathologically Eclectic Rubbish Lister
 
PerlMonks  

Re^2: Sorting Vietnamese text

by Anonymous Monk
on Dec 23, 2013 at 07:31 UTC ( #1068152=note: print w/ replies, xml ) Need Help??


in reply to Re: Sorting Vietnamese text
in thread Sorting Vietnamese text

And here's what I came up

sub make_sort_order {
	my $str = shift;
	$str =~
	tr(aảạăaảạăắằẳẵặấầẩẫậbcdđeẻẽẹếềểễệfghiỉĩịjklmnoỏọốồổỗộơớờởỡợpqrstuủũụưứừửữựvwxyỳỷỹỵz)
	  (00000011111111111112222223456777777888888abcddddddefghijjjjjjkkkkkkllllllmnopqrrrrrrsssssstuvwwwwwwx)d;

	return $str;
}

my @words = ('ầm', 'm', 'ấm ch', 'm số');


print $_->[1], "[n" for
	sort { $a->[0] cmp $b->[0] || $a->[1] cmp $b->[1] }
	map  { [ make_sort_order($_), $_ ] } @words;

It's still missing a correct 'secondary sort' (for the edge case when the diacritic-stripped words are identical); it should not be difficult to add once someone figures out a suitable transliteration that sorts asciibetically.


Comment on Re^2: Sorting Vietnamese text
Re^3: Sorting Vietnamese text
by pdenisowski (Acolyte) on Dec 23, 2013 at 15:10 UTC

    It's still missing a correct 'secondary sort' (for the edge case when the diacritic-stripped words are identical);

    (laughs) That's hardly an "edge case" in Vietnamese - there are thousands of minimal pairs where the only difference between the words is the diacritical marks. While it's possible to read and understand Vietnamese typed in (7-bit) ASCII without too much ambiguity (i.e. you can figure out what word is meant from the context), this obviously wouldn't work for a dictionary.

    The other issue is that the words in the dictionary need to be sorted in the "correct" order for me to detect duplicates, etc.

    I'll try out your suggestion later today - thanks again!

      I'm probably completely off base here -- why does m số come before m when sorts at the same point as ? And the same question with ỳ ạch and ỷ and the other lines...

      I think there's something wrong with my alphabet -- some characters being eaten perhaps -- since the tr/// has duplicates? If they're wrong, please re-input the correct alphabetical order into the tr/// lines yourself.

      I'm still not sure how spaces play part in all this; I'd sort all the single-character y variants together, and before the two-syllable ones, but the order you've given does not match that and somehow my code handles it handled it even though it shouldn't have.

      You should split the string into two before sorting, though. One part for the actual word, the other for the translation.

      Anyway, here's a try for a correct secondary sort -- of course, I do not have any words to test it on. It sorts how I expect it to do based on what I said in 1068139, but it does not sort how you expect it to.

      Also, why the hell is my tr///d not deleting characters?

      Okay -- the alphabet is only 93 characters per the link in 1068127; you had seven characters extra.

      You should clarify what the relation between monosyllables and polysyllables is, and what is the desired collation behaviour between them.

Log In?
Username:
Password:

What's my password?
Create A New User
Node Status?
node history
Node Type: note [id://1068152]
help
Chatterbox?
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others pondering the Monastery: (8)
As of 2014-08-23 15:36 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    The best computer themed movie is:











    Results (174 votes), past polls