Last active
September 18, 2017 04:40
-
-
Save santhoshtr/681e8bb72c63cb74d67d123f4fb7e7be to your computer and use it in GitHub Desktop.
Malayalam Collator
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/usr/bin/python3 | |
import sys | |
import gi | |
gi.require_version('Gtk', '3.0') | |
from gi.repository import Gtk, Gio, GLib, Pango | |
import locale | |
from pyuca import Collator | |
from icu import UnicodeString, Locale, Collator as ICUCollator | |
class CollatorWindow(Gtk.ApplicationWindow): | |
def __init__(self, *args, **kwargs): | |
super().__init__(*args, **kwargs) | |
self.set_border_width(10) | |
self.set_default_size(800, 600) | |
grid = Gtk.Grid() | |
self.add(grid) | |
button = Gtk.Button.new_with_label("Sort") | |
button.connect("clicked", self.on_click) | |
scrolledwindow_glibc = Gtk.ScrolledWindow() | |
scrolledwindow_glibc.set_hexpand(True) | |
scrolledwindow_glibc.set_vexpand(True) | |
text = "അതിൽ\nഅത്\nആണ്\nആണ്\nആൺ\nകമ്പ്യൂട്ടർ\nകംപ്യൂട്ടർ\nകര\nകരി\nചക്ക\nചക്കി\nചക്കു\nചക്കു്\nചക്ക്\nപദം\nപദ്മിനി\nപൗരൻ\nപൌർണ്ണമി\nപൗർണ്ണമി\nയാത്ര\nലതിക\nവര\nവർത്തമാനം\nവല\nഹരി\nളോഹ\nറംസാൻ\nറവ" | |
self.textview_glibc = Gtk.TextView() | |
self.textbuffer_glibc = self.textview_glibc.get_buffer() | |
self.textbuffer_glibc .set_text(text) | |
self.textview_glibc.modify_font( Pango.FontDescription('Manjari 16')) | |
self.textview_glibc.set_left_margin(16) | |
scrolledwindow_glibc.add(self.textview_glibc) | |
scrolledwindow_uca = Gtk.ScrolledWindow() | |
scrolledwindow_uca.set_hexpand(True) | |
scrolledwindow_uca.set_vexpand(True) | |
scrolledwindow_uca.set_shadow_type(2) | |
self.textview_uca = Gtk.TextView() | |
self.textbuffer_uca = self.textview_uca.get_buffer() | |
self.textbuffer_uca.set_text(text) | |
self.textview_uca.modify_font( Pango.FontDescription('Manjari 16')) | |
self.textview_uca.set_left_margin(16) | |
self.textview_uca.set_editable(False) | |
scrolledwindow_uca.add(self.textview_uca) | |
scrolledwindow_icu = Gtk.ScrolledWindow() | |
scrolledwindow_icu.set_hexpand(True) | |
scrolledwindow_icu.set_vexpand(True) | |
self.textview_icu = Gtk.TextView() | |
self.textbuffer_icu = self.textview_icu.get_buffer() | |
self.textbuffer_icu.set_text(text) | |
self.textview_icu.modify_font( Pango.FontDescription('Manjari 16')) | |
self.textview_icu.set_left_margin(16) | |
self.textview_icu.set_editable(False) | |
scrolledwindow_icu.add(self.textview_icu) | |
glibclabel = Gtk.Label("GLIBC") | |
ucalabel = Gtk.Label("UCA") | |
iculabel = Gtk.Label("ICU") | |
grid.attach( glibclabel, 0,0,1,1) | |
grid.attach( ucalabel, 1,0,1,1) | |
grid.attach_next_to(iculabel, ucalabel, Gtk.PositionType.RIGHT, 1,1) | |
grid.attach(scrolledwindow_glibc, 0, 1, 1, 2) | |
grid.attach_next_to(scrolledwindow_uca, scrolledwindow_glibc, Gtk.PositionType.RIGHT, 1, 1) | |
grid.attach_next_to(scrolledwindow_icu, scrolledwindow_uca, Gtk.PositionType.RIGHT, 1, 1) | |
grid.attach(button, 0, 3, 4, 4) | |
grid.show() | |
def on_click(self, button): | |
start,end=self.textbuffer_glibc.get_bounds() | |
lines = self.textbuffer_glibc.get_text(start,end, False).splitlines() | |
sorted_lines_glibc=self.glibc_collation(lines) | |
self.textbuffer_glibc.set_text(str.join('\n', sorted_lines_glibc)) | |
sorted_lines_uca=self.uca_collation(lines); | |
self.textbuffer_uca.set_text(str.join('\n', sorted_lines_uca)) | |
sorted_lines_icu=self.icu_collation(lines); | |
self.textbuffer_icu.set_text(str.join('\n', sorted_lines_icu)) | |
def glibc_collation(self, lines): | |
locale.setlocale(locale.LC_ALL, "ml_IN.UTF-8") | |
return sorted(lines, key=locale.strxfrm) | |
def uca_collation(self, lines): | |
c = Collator() | |
return sorted(lines, key=c.sort_key) | |
def icu_collation(self, lines): | |
collator = ICUCollator.createInstance(Locale("ml_IN")) | |
return sorted(lines, key=collator.getSortKey) | |
class Application(Gtk.Application): | |
def __init__(self, *args, **kwargs): | |
Gtk.Application.__init__(self, | |
application_id="org.gnome.example", | |
flags=Gio.ApplicationFlags.FLAGS_NONE) | |
self.window = None | |
self.connect("activate", self.do_activate) | |
def do_activate(self): | |
# We only allow a single window and raise any existing ones | |
if not self.window: | |
# Windows are associated with the application | |
# when the last one is closed the application shuts down | |
self.window = CollatorWindow(application=self, title="Collator") | |
self.add_window(self.window) | |
self.window.connect("delete-event", Gtk.main_quit) | |
self.window.show_all() | |
def on_quit(self, action, param): | |
self.quit() | |
if __name__ == "__main__": | |
app = Application() | |
app.run(sys.argv) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment