#!/usr/bin/env python """ Experiment with various ispell affix combinations Quick app written to help the ispell-lt project: http://sraige.mif.vu.lt/cvs/ispell-lt/ Written by Marius Gedminas. Licence: GPL (google for it). """ import subprocess import gtk import gtk.glade class App(object): flags = 'ABCDEHIKMNPQSTUVXYabcdefghijklmnopqrstuvwx' ncols = 7 dictionary = 'lietuviu' encoding = 'ISO-8859-13' def __init__(self, flags=None, dictionary=None, encoding=None): if flags is not None: self.flags = flags if dictionary is not None: self.dictionary = dictionary if encoding is not None: self.encoding = encoding self.xml = gtk.glade.XML('ispell-try.glade') for name in ['main_window', 'word_entry', 'flag_table', 'derived_words_label', 'output_textview']: setattr(self, name, self.xml.get_widget(name)) self.flag_checkboxes = {} ncols = self.ncols nrows = (len(self.flags) - 1) / ncols + 1 self.flag_table.resize(ncols, nrows) for n, flag in enumerate(self.flags): col, row = divmod(n, nrows) checkbox = gtk.CheckButton('_%s' % flag) checkbox.show() checkbox.connect('toggled', self.update) self.flag_table.attach(checkbox, col, col+1, row, row+1) self.flag_checkboxes[flag] = checkbox self.word_entry.connect('changed', self.update) self.main_window.connect('destroy', lambda *args: gtk.main_quit()) def update(self, *args): word = self.word_entry.get_text() if '/' in word: word, flags = word.split('/', 1) self.set_flag_checkboxes(flags) else: flags = self.get_flag_checkboxes() expansion = self.expand(word, flags) label = self.derived_words_label.get_text() label = label.split('(', 1)[0].strip() label += ' (%s)' % self.count_words(expansion) self.derived_words_label.set_text(label) self.output_textview.get_buffer().set_text(expansion) def count_words(self, expansion): return len(expansion.split('\n', 1)[0].split()) def set_flag_checkboxes(self, flags): for flag, checkbox in self.flag_checkboxes.items(): checkbox.set_sensitive(False) checkbox.set_active(flag in flags) def get_flag_checkboxes(self): flags = '' for flag, checkbox in self.flag_checkboxes.items(): checkbox.set_sensitive(True) if checkbox.get_active(): flags += flag return flags def expand(self, word, flags): if not word: return '' if flags: flags = '/' + flags ispell = subprocess.Popen(['ispell', '-e', '-d', self.dictionary], stdin=subprocess.PIPE, stdout=subprocess.PIPE, stderr=subprocess.PIPE) stdin = word + flags stdout, stderr = ispell.communicate(stdin.encode(self.encoding)) return stdout.decode(self.encoding) + '\n' + stderr.decode(self.encoding) if __name__ == '__main__': app = App() gtk.main()