Ejemplo n.º 1
0
    def get(self, token_id):
        # Session request handler
        current_session = Session(self)
        JINJA_ENVIRONMENT.globals['session'] = current_session
        # Language request handler
        Language.language(self)
        # Load jinja template
        template = JINJA_ENVIRONMENT.get_template('static/templates/activation.html')

        # Check if token is expired
        token = database.TokenManager.select_token_by_id(int(token_id))
        if token and (datetime.datetime.now() - datetime.timedelta(days=1) < token.date) and (not token.used):
            # Activate user
            user = token.user.get()
            # Check if user is already activated
            if user.role_level > 0:
                errorMessage = _("AccountAlreadyActivated")
            else:
                errorMessage = None
            database.UserManager.modify_user(user.key, role_level=1)
            # Set token as used
            database.TokenManager.set_used_token(token.key)
        else:
            errorMessage = _("ExpiredTokenOrNotExist")
        # Prompt activation result
        self.response.write(template.render(error=errorMessage))
Ejemplo n.º 2
0
def makeLanguages(logixModuleName, homedir, moduledict):
    import pycompile

    pycompile.installRootopCompilers()
    
    modname = logixModuleName + ".base"

    quotelang = makeQuotelang(parent=None, module=modname)
    syntaxlang = makeSyntaxlang(parent=quotelang, module=modname)
    langlang = makeLangLang(syntaxlang, parent=quotelang, module=modname)
    syntaxlang.__impl__.addOp(langlang.__impl__.getOp("(^"))

    global defaultBaseLang
    defaultBaseLang = langlang

    baselang = Language("base", langlang, modname)
    baselang.operatorBase = PyOp

    filename = homedir + "/base.lx"
    env = dict(__name__=modname,
               base=baselang)
    baselang.__impl__.parse(file(filename, 'U'), mode='exec', execenv=env)
    pycompile.installPyCompilers(baselang.__impl__)

    # HACK: Fix up base lmodule
    mod = new.module(filename)
    mod.__file__ = filename
    vars(mod).update(env)
    moduledict[modname] = mod
    mod.langlang = langlang
    mod.syntaxlang = syntaxlang
    mod.quotelang = quotelang

    return quotelang, syntaxlang, langlang, baselang
Ejemplo n.º 3
0
    def get(self, user_id):
        # Session request handler
        current_session = Session(self)
        JINJA_ENVIRONMENT.globals['session'] = current_session
        # Language request handler
        Language.language(self)

        # Retrieved user_id to integer
        user_id = int(user_id)
        # Retrieve user data
        user = database.UserManager.select_by_id(user_id)
        # Check if user can see the profile photo
        if user.photo is not None:
            profilePhoto = database.PhotosManager.get_photo_by_id(int(user.photo))
            if current_session.get_id() is None:
                profilePhotoAllowed = False
            else:
                requestUser = database.UserManager.select_by_id(current_session.get_id())
                if security.PhotoSecurity.user_is_allowed_to_watch_photo(profilePhoto, requestUser):
                    profilePhotoAllowed = True
                else:
                    profilePhotoAllowed = False
        else:
            profilePhotoAllowed = False

        # Prompt page
        template = JINJA_ENVIRONMENT.get_template('static/templates/profile.html')
        self.response.write(template.render(user=user, profilePhotoAllowed=profilePhotoAllowed))
Ejemplo n.º 4
0
 def get(self):
     # Session request handler
     current_session = Session(self)
     JINJA_ENVIRONMENT.globals['session'] = current_session
     # Language request handler
     Language.language(self)
     template = JINJA_ENVIRONMENT.get_template('static/templates/welcome.html')
     self.response.write(template.render())
Ejemplo n.º 5
0
 def get(self):
     # Session request handler
     current_session = Session(self)
     JINJA_ENVIRONMENT.globals['session'] = current_session
     # Language request handler
     Language.language(self)
     # Retrieve key
     f = open("key/googlemaps.key")
     key = f.read()
     # Render template
     template = JINJA_ENVIRONMENT.get_template('static/templates/map.html')
     self.response.write(template.render(googleApiKey=key))
Ejemplo n.º 6
0
 def get(self):
     # Session request handler
     current_session = Session(self)
     JINJA_ENVIRONMENT.globals['session'] = current_session
     # Language request handler
     Language.language(self)
     # Check if user is already logged in
     if current_session.get_id() is not None:
         self.redirect("/")
         return None
     template = JINJA_ENVIRONMENT.get_template('static/templates/register.html')
     self.response.write(template.render())
Ejemplo n.º 7
0
 def get(self):
     # Session request handler
     current_session = Session(self)
     JINJA_ENVIRONMENT.globals['session'] = current_session
     # Language request handler
     Language.language(self)
     # Check if user is admin
     if (current_session.get_role_level() < 3):
         self.redirect("/")
         return None
     # Retrieve users
     users = database.UserManager.select()
     # Render template
     template = JINJA_ENVIRONMENT.get_template('static/templates/users.html')
     self.response.write(template.render(users=users))
Ejemplo n.º 8
0
 def get(self):
     # Session request handler
     current_session = Session(self)
     JINJA_ENVIRONMENT.globals['session'] = current_session
     # Language request handler
     Language.language(self)
     # Check if user has session started
     if current_session.get_id() is None:
         self.redirect("/")
     # Logout user
     current_session.logout(self)
     # Prompt logout page
     JINJA_ENVIRONMENT.globals['session'] = current_session
     template = JINJA_ENVIRONMENT.get_template('static/templates/logout.html')
     self.response.write(template.render())
Ejemplo n.º 9
0
    def get(self, token_id):
        # Session request handler
        current_session = Session(self)
        JINJA_ENVIRONMENT.globals['session'] = current_session
        # Language request handler
        Language.language(self)

        token = database.TokenManager.select_token_by_id(int(token_id))
        if token and (datetime.datetime.now() - datetime.timedelta(days=1) < token.date) and (not token.used):
            # Present web page
            template = JINJA_ENVIRONMENT.get_template('static/templates/changeProfile.html')
            user = token.user.get()
            self.response.write(template.render(user=user, token_id=token_id))
        else:
            self.redirect("/")
Ejemplo n.º 10
0
    def compute_stats(self):
        """
        Internal: Compute language breakdown for each blob in the Repository.

        Returns nothing
        """
        if self.computed_stats:
            return

        for blob in self.enum:
            # Skip vendored
            if blob.is_vendored:
                continue
            # Skip files that are likely binary
            if blob.is_likely_binary:
                continue
            # Skip generated blobs
            if blob.is_generated or blob.language is None:
                continue
            # Only include programming languages and acceptable markup languages
            if blob.language.type == 'programming' or blob.language.name in Language.detectable_markup():
                self.sizes[blob.language.group] += blob.size

        # Compute total size
        self._size = sum(self.sizes.itervalues())

        # Get primary language
        primary = sorted(self.sizes.iteritems(), key=lambda t: t[1], reverse=True)
        if primary:
            self._language = primary[0][0]

        self.computed_stats = True
Ejemplo n.º 11
0
    def is_likely_binary(self):
        """
        Internal: Is the blob binary according to its mime type,
        overriding it if we have better data from the languages.yml
        database.

        Return true or false
        """
        return self.is_binary_mime_type and not Language.find_by_filename(self.name)
Ejemplo n.º 12
0
 def post(self):
     # Session request handler
     current_session = Session(self)
     JINJA_ENVIRONMENT.globals['session'] = current_session
     # Language request handler
     Language.language(self)
     # Check if user is already logged in
     if current_session.get_id() is not None:
         self.redirect("/")
     # Language task
     Language.language(self)
     # Load form
     template = JINJA_ENVIRONMENT.get_template('static/templates/login.html')
     # Check user and password
     submitted_username = cgi.escape(self.request.get("username"))
     submitted_password = hashlib.sha1(cgi.escape(self.request.get("password"))).hexdigest()
     user = database.UserManager.select_by_username(submitted_username)
     # Check user exists
     if user is not None:
         # Check if user account is blocked or not
         if user.attempts < 3:
             # Check if user and password matches
             if submitted_username == user.name and submitted_password == user.password:
                 # Session initialization
                 current_session.set(self, user.key.id())
                 # Login attempts to zero
                 database.UserManager.modify_user(user.key, attempts=0)
                 # Redirection to initial page
                 self.redirect("/")
             else:
                 # Add an attempt to user login
                 database.UserManager.modify_user(user.key, attempts=user.attempts+1)
                 self.response.write(template.render(error=_("InvalidUsernameOrPassword")))
         else:
             self.response.write(template.render(error=_("AccountBlocked")))
     else:
         self.response.write(template.render(error=_("InvalidUsernameOrPassword")))
Ejemplo n.º 13
0
    def language(self):
        """
        Public: Detects the Language of the blob.

        May load Blob#data

        Returns a Language or nil if none is detected
        """
        if hasattr(self, '_language'):
            return self._language

        def data():
            if self.is_binary_mime_type or self.is_binary:
                return ''
            return self.data
        self._language = Language.detect(self.name, data, self.mode)
        return self._language
Ejemplo n.º 14
0
    def language(self):
        """
        Public: Detects the Language of the blob.

        May load Blob#data

        Returns a Language or nil if none is detected
        """
        if hasattr(self, '_language'):
            return self._language

        _data = getattr(self, '_data', False)
        if _data and isinstance(_data, basestring):
            data = _data
        else:
            data = lambda: '' if (self.is_binary_mime_type or self.is_binary) else self.data
        self._language = Language.detect(self.name, data, self.mode)
        return self._language
Ejemplo n.º 15
0
    def __init__(self, _main):

        print "Engine - init >>>"
        log.info("Engine - init >>>")


        # Main ref
        self.main = _main
        self.settings = Settings()
        self.graphicMgr = GraphicManager(self.settings)
        self.audioMgr = AudioManager()
        self.lng = Language(self.settings)
        self.lng.setup(self.settings)

        ### Bools ###
        # Gets set if we have a player character to work with.
        self.hasPlayer = False
        ### Bools END ###

        ### Setup Engine Holders ###
        self.__resetObjects()
        ### Engine Holders END ###

        # Setup Bullet Physics
        #? We could save this somewhere else i guess?
        self.bulletWorld = BulletWorld()
        self.bulletWorld.setGravity(
            Vec3(self.settings.gravity_x,
                 self.settings.gravity_y,
                 self.settings.gravity_z))

        # Init Factory
        self.factory = Factory(self)

        # Debug node
        self.debugNP = None
Ejemplo n.º 16
0
def makeLanguages(logixModuleName, homedir, moduledict):
    import pycompile

    pycompile.installRootopCompilers()

    modname = logixModuleName + ".base"

    quotelang = makeQuotelang(parent=None, module=modname)
    syntaxlang = makeSyntaxlang(parent=quotelang, module=modname)
    langlang = makeLangLang(syntaxlang, parent=quotelang, module=modname)
    syntaxlang.__impl__.addOp(langlang.__impl__.getOp("(^"))

    global defaultBaseLang
    defaultBaseLang = langlang

    # {{{ baselang = create baselang (possibly from cache)
    baselang = Language("base", langlang, modname)

    filename = homedir + "/base.lx"
    env = dict(__name__=modname, base=baselang, langlang=langlang)

    def persistent_load(pid):
        res = {
            'nothing': nothing,
            'eol': Tokenizer.endOfLine,
            'eob': Tokenizer.endOfBlock,
            'eof': Tokenizer.endOfFile
        }.get(pid)
        if res: return res
        else: raise cPickle.UnpicklingError, 'Invalid persistent id'

    def persistent_id(x):
        return {
            id(nothing): "nothing",
            id(Tokenizer.endOfLine): "eol",
            id(Tokenizer.endOfBlock): "eob",
            id(Tokenizer.endOfFile): "eof"
        }.get(id(x))

    opFile = "%s/logix_opcache" % homedir

    if not path.exists(opFile):
        baselang.__impl__.parse(file(filename, 'U'), mode='exec', execenv=env)
        oplst = [x.syntax for x in baselang.__impl__.operators.values()]

        p = cPickle.Pickler(file(opFile, "w+"))
        p.persistent_id = persistent_id
        p.dump(oplst)

    else:
        p = cPickle.Unpickler(file(opFile))
        p.persistent_load = persistent_load
        for syntax in p.load():
            baselang.__impl__.newOpFromSyntax(syntax)
    # }}}

    pycompile.installPyCompilers(baselang.__impl__)

    # HACK: Fix up base lmodule
    mod = new.module(filename)
    mod.__file__ = filename
    vars(mod).update(env)
    moduledict[modname] = mod
    mod.langlang = langlang
    mod.syntaxlang = syntaxlang
    mod.quotelang = quotelang

    return quotelang, syntaxlang, langlang, baselang
Ejemplo n.º 17
0
    def __init__(self):

        self.conf = Conf()
        self.home = self.conf.home
        self.currentpath = self.home + self.conf.get(
            'GENERAL', 'op_folder') + '/openplotter'

        Language(self.conf)

        wx.Frame.__init__(self, None, title=_('SDR receiver'), size=(690, 370))

        self.SetFont(
            wx.Font(10, wx.FONTFAMILY_DEFAULT, wx.FONTSTYLE_NORMAL,
                    wx.FONTWEIGHT_NORMAL))

        self.icon = wx.Icon(self.currentpath + '/openplotter.ico',
                            wx.BITMAP_TYPE_ICO)
        self.SetIcon(self.icon)

        wx.StaticBox(self, label='', size=(400, 170), pos=(10, 10))

        self.ais_sdr_enable = wx.CheckBox(self,
                                          label=_('Enable AIS reception'),
                                          pos=(20, 25))
        self.ais_sdr_enable.Bind(wx.EVT_CHECKBOX, self.OnOffAIS)

        self.gain = wx.TextCtrl(self, -1, size=(55, 32), pos=(150, 60))
        self.gain_label = wx.StaticText(self, label=_('Gain'), pos=(20, 65))
        self.ppm = wx.TextCtrl(self, -1, size=(55, 32), pos=(150, 95))
        self.correction_label = wx.StaticText(self,
                                              label=_('Correction (ppm)'),
                                              pos=(20, 100))

        self.ais_frequencies1 = wx.CheckBox(self,
                                            label=_('Channel A 161.975Mhz'),
                                            pos=(220, 60))
        self.ais_frequencies1.Bind(wx.EVT_CHECKBOX, self.ais_frequencies)
        self.ais_frequencies2 = wx.CheckBox(self,
                                            label=_('Channel B 162.025Mhz'),
                                            pos=(220, 95))
        self.ais_frequencies2.Bind(wx.EVT_CHECKBOX, self.ais_frequencies)

        #self.show_kplex6 =wx.Button(self, label=_('Inspector'), pos=(20, 140))
        #self.Bind(wx.EVT_BUTTON, self.show_kplex, self.show_kplex6)
        self.button_test_ppm = wx.Button(self,
                                         label=_('Take a look'),
                                         pos=(150, 140))
        self.Bind(wx.EVT_BUTTON, self.test_ppm, self.button_test_ppm)
        self.button_test_gain = wx.Button(self,
                                          label=_('Calibration'),
                                          pos=(275, 140))
        self.Bind(wx.EVT_BUTTON, self.test_gain, self.button_test_gain)

        wx.StaticBox(self,
                     label=_(' Fine calibration using GSM '),
                     size=(260, 170),
                     pos=(420, 10))
        self.bands_label = wx.StaticText(self, label=_('Band'), pos=(430, 50))
        self.bands_list = ['GSM850', 'GSM-R', 'GSM900', 'EGSM', 'DCS', 'PCS']
        self.band = wx.ComboBox(self,
                                choices=self.bands_list,
                                style=wx.CB_READONLY,
                                size=(100, 32),
                                pos=(430, 70))
        self.band.SetValue('GSM900')
        self.check_bands = wx.Button(self,
                                     label=_('Check band'),
                                     pos=(540, 70))
        self.Bind(wx.EVT_BUTTON, self.check_band, self.check_bands)
        self.channel_label = wx.StaticText(self,
                                           label=_('Channel'),
                                           pos=(430, 125))
        self.channel = wx.TextCtrl(self, -1, size=(55, 32), pos=(430, 143))
        self.check_channels = wx.Button(self,
                                        label=_('Fine calibration'),
                                        pos=(495, 140))
        self.Bind(wx.EVT_BUTTON, self.check_channel, self.check_channels)

        wx.StaticBox(self, label=_(' Radio '), size=(260, 120), pos=(420, 185))

        self.button_vhf_Rx = wx.Button(self, label='Gqrx', pos=(430, 210))
        self.Bind(wx.EVT_BUTTON, self.vhf_Rx, self.button_vhf_Rx)

        self.CreateStatusBar()

        self.Centre()

        self.Show(True)

        output = subprocess.check_output('lsusb')
        supported_dev = [
            '0bda:2832', '0bda:2838', '0ccd:00a9', '0ccd:00b3', '0ccd:00d3',
            '0ccd:00d4', '0ccd:00e0', '185b:0620', '185b:0650', '1f4d:b803',
            '1f4d:c803', '1b80:d3a4', '1d19:1101', '1d19:1102', '1d19:1103',
            '0458:707f', '1b80:d393', '1b80:d394', '1b80:d395', '1b80:d39d'
        ]
        found = False
        for i in supported_dev:
            if i in output: found = True
        if found:
            self.gain.SetValue(self.conf.get('AIS-SDR', 'gain'))
            self.ppm.SetValue(self.conf.get('AIS-SDR', 'ppm'))
            self.band.SetValue(self.conf.get('AIS-SDR', 'band'))
            self.channel.SetValue(self.conf.get('AIS-SDR', 'gsm_channel'))
            if self.conf.get('AIS-SDR', 'enable') == '1':
                self.ais_sdr_enable.SetValue(True)
                self.disable_sdr_controls()
            if self.conf.get('AIS-SDR', 'channel') == 'a':
                self.ais_frequencies1.SetValue(True)
            if self.conf.get('AIS-SDR', 'channel') == 'b':
                self.ais_frequencies2.SetValue(True)
        else:
            self.ais_sdr_enable.Disable()
            self.disable_sdr_controls()
            self.button_test_gain.Disable()
            self.button_test_ppm.Disable()
            self.bands_label.Disable()
            self.channel_label.Disable()
            self.band.Disable()
            self.channel.Disable()
            self.check_channels.Disable()
            self.check_bands.Disable()
            self.button_vhf_Rx.Disable()
Ejemplo n.º 18
0
    def __init__(self):
        self.conf = Conf()
        self.home = self.conf.home
        self.op_folder = op_folder
        self.help_bmp = wx.Bitmap(
            self.op_folder + "/static/icons/help-browser.png",
            wx.BITMAP_TYPE_ANY)
        Language(self.conf)
        wx.Frame.__init__(self,
                          None,
                          title=_('Kplex GUI - NMEA 0183 Multiplexer'),
                          size=(710, 460))
        self.SetFont(
            wx.Font(10, wx.FONTFAMILY_DEFAULT, wx.FONTSTYLE_NORMAL,
                    wx.FONTWEIGHT_NORMAL))
        self.icon = wx.Icon(self.op_folder + '/static/icons/kplex.ico',
                            wx.BITMAP_TYPE_ICO)
        self.SetIcon(self.icon)

        self.list_kplex = CheckListCtrl(self, 650, 152)
        self.list_kplex.InsertColumn(0, _('Name'), width=130)
        self.list_kplex.InsertColumn(1, _('Type'), width=45)
        self.list_kplex.InsertColumn(2, _('io'), width=45)
        self.list_kplex.InsertColumn(3, _('Port/Address'), width=95)
        self.list_kplex.InsertColumn(4, _('Bauds/Port'), width=60)
        self.list_kplex.InsertColumn(5, _('inFilter'), width=55)
        self.list_kplex.InsertColumn(6, _('Filtering'), width=80)
        self.list_kplex.InsertColumn(7, _('outFilter'), width=60)
        self.list_kplex.InsertColumn(8, _('Filtering'), width=80)
        self.list_kplex.InsertColumn(9, _('Optional'), width=10)
        self.list_kplex.Bind(wx.EVT_LIST_ITEM_ACTIVATED, self.edit_kplex)

        diagnostic = wx.Button(self, label=_('Diagnostic'))
        diagnostic.Bind(wx.EVT_BUTTON, self.on_diagnostic_kplex)

        add = wx.Button(self, label=_('add'))
        add.Bind(wx.EVT_BUTTON, self.on_add_kplex)
        delete = wx.Button(self, label=_('delete'))
        delete.Bind(wx.EVT_BUTTON, self.on_delete_kplex)

        help_button = wx.BitmapButton(self,
                                      bitmap=self.help_bmp,
                                      size=(self.help_bmp.GetWidth() + 40,
                                            self.help_bmp.GetHeight() + 10))
        help_button.Bind(wx.EVT_BUTTON, self.on_help_kplex)
        restart = wx.Button(self, label=_('Restart'))
        restart.Bind(wx.EVT_BUTTON, self.on_restart_kplex)
        advanced = wx.Button(self, label=_('Advanced'))
        advanced.Bind(wx.EVT_BUTTON, self.on_advanced_kplex)
        apply_changes = wx.Button(self, label=_('Apply changes'))
        apply_changes.Bind(wx.EVT_BUTTON, self.on_apply_changes_kplex)
        cancel_changes = wx.Button(self, label=_('Cancel changes'))
        cancel_changes.Bind(wx.EVT_BUTTON, self.on_cancel_changes_kplex)

        hlistbox = wx.BoxSizer(wx.HORIZONTAL)
        hlistbox.Add(self.list_kplex, 1, wx.ALL | wx.EXPAND, 5)

        hbox = wx.BoxSizer(wx.HORIZONTAL)
        hbox.Add((0, 0), 1, wx.RIGHT | wx.LEFT, 5)
        hbox.Add(add, 0, wx.RIGHT | wx.LEFT, 5)
        hbox.Add(delete, 0, wx.RIGHT | wx.LEFT, 5)

        hboxb = wx.BoxSizer(wx.HORIZONTAL)
        hboxb.Add(help_button, 0, wx.RIGHT | wx.EXPAND, 5)
        hboxb.Add(diagnostic, 0, wx.RIGHT | wx.LEFT | wx.EXPAND, 5)
        hboxb.Add(restart, 0, wx.RIGHT | wx.LEFT | wx.EXPAND, 5)
        hboxb.Add(advanced, 0, wx.RIGHT | wx.LEFT | wx.EXPAND, 5)
        hboxb.Add((0, 0), 1, wx.RIGHT | wx.LEFT, 5)
        hboxb.Add(apply_changes, 0, wx.RIGHT | wx.LEFT | wx.EXPAND, 5)
        hboxb.Add(cancel_changes, 0, wx.RIGHT | wx.LEFT | wx.EXPAND, 5)

        vbox = wx.BoxSizer(wx.VERTICAL)
        vbox.Add(hlistbox, 1, wx.ALL | wx.EXPAND, 0)
        vbox.Add(hbox, 0, wx.ALL | wx.EXPAND, 5)
        vbox.AddSpacer(5)
        vbox.Add(hboxb, 0, wx.ALL | wx.EXPAND, 5)

        self.SetSizer(vbox)
        self.CreateStatusBar()
        font_statusBar = self.GetStatusBar().GetFont()
        font_statusBar.SetWeight(wx.BOLD)
        self.GetStatusBar().SetFont(font_statusBar)
        self.Centre()
        self.manual_settings = ''
        self.read_kplex_conf()
Ejemplo n.º 19
0
    def __init__(self):

        self.option = sys.argv[1]

        self.conf = Conf()
        self.home = self.conf.home
        self.currentpath = self.conf.get('GENERAL', 'op_folder')

        Language(self.conf)

        wx.Frame.__init__(self,
                          None,
                          title=_('Fine calibration'),
                          size=(600, 320))

        self.SetFont(
            wx.Font(10, wx.FONTFAMILY_DEFAULT, wx.FONTSTYLE_NORMAL,
                    wx.FONTWEIGHT_NORMAL))

        self.icon = wx.Icon(self.currentpath + '/static/icons/openplotter.ico',
                            wx.BITMAP_TYPE_ICO)
        self.SetIcon(self.icon)

        self.text = wx.StaticText(self, label=_('Error'), pos=(10, 10))

        self.ppm = self.conf.get('AIS-SDR', 'ppm')
        self.band = self.conf.get('AIS-SDR', 'band')
        self.channel = self.conf.get('AIS-SDR', 'gsm_channel')

        wx.StaticText(self,
                      label=_('Initial PPM: ').decode('utf8') + self.ppm,
                      pos=(10, 80))

        self.output = wx.TextCtrl(self,
                                  style=wx.TE_MULTILINE | wx.TE_READONLY
                                  | wx.TE_DONTWRAP,
                                  size=(580, 120),
                                  pos=(10, 100))

        self.button_close = wx.Button(self, label=_('Close'), pos=(400, 230))
        self.Bind(wx.EVT_BUTTON, self.close, self.button_close)

        self.button_calculate = wx.Button(self,
                                          label=_('Start'),
                                          pos=(500, 230))
        self.Bind(wx.EVT_BUTTON, self.calculate, self.button_calculate)

        if self.option == 'c':
            self.text.SetLabel(
                _('Press Start and wait for the system to calculate the PPM value.\nRound the resulting PPM to the nearest integer and copy it into "PPM" field.\nEstimated time: 1 min.'
                  ))
            wx.StaticText(self,
                          label=_('channel: ').decode('utf8') + self.channel,
                          pos=(200, 80))
        if self.option == 'b':
            self.text.SetLabel(
                _('Press Start and wait for the system to check the band.\nWrite down the strongest channel (power).\nIf you do not find any channel try another band.\nEstimated time: 5 min.'
                  ))
            wx.StaticText(self,
                          label=_('band: ').decode('utf8') + self.band,
                          pos=(200, 80))

        self.CreateStatusBar()

        self.Centre()
 def __init__(self):
     Language.__init__(self, "French")
     f = open("../res/french.txt")
     self.generate_table(f.read())
     f.close()
Ejemplo n.º 21
0
	def parse_options_as_array( cls, args, silent=False ):
		"""Parses options from arguments list to an ordered array.
		
		Parses options from arguments list to an orderd array where elements
		are tuples (option name, value). Unknown options and other arguments
		are added as string to the list.
		
		Parameters:
			- (list) args: The command line arguments.
			- (bool) silent: Disable warning messages.
			
		Return value:
			- (list) -- the parsed options.
		"""
		
		options = []
		
		current_option = None
		current_value = None
		values_to_parse = 0
		
		for arg in args:
			
			match = re.match( r"^-(?:-?([a-zA-Z0-9][a-zA-Z0-9_-]*)?)$", arg )
			
			if match:
				if current_option != None:
					if not silent and (values_to_parse > 0 or (values_to_parse == -1 and len(current_value) == 0)):
						log.warn( Language.get( Command, "missing_value" ) % current_option )
						
					options.append( (current_option, current_value) )
			
				current_option = match.group( 1 ).lower() if match.group( 1 ) else None
				
				if current_option in cls._options:
					values_to_parse = cls._options[current_option]["params"] if "params" in cls._options[current_option] else 0
				
					if values_to_parse == 0:
						current_value = True
					else:
						current_value = ""
				
				else:
					if not silent:
						log.warn( Language.get( Command, "unknown_option" ) % current_option )
					
					options.append( match.group( 0 ) )
					
					current_option = None
					current_value = None
					values_to_parse = 0
			
			else:
				if values_to_parse > 0 or values_to_parse == -1:
				
					if len(current_value):
						current_value += " " + arg
					else:
						current_value = arg
						
					if values_to_parse > 0:
						values_to_parse -= 1
				
				else:
					if current_option != None:
						if not silent and (values_to_parse > 0 or (values_to_parse == -1 and len(current_value) == 0)):
							log.warn( Language.get( Command, "missing_value" ) % current_option )
			
						options.append( (current_option, current_value) )
						
						current_option = None
						current_value = None
						values_to_parse = 0
					
					options.append( arg )
			
		if current_option != None:
			if not silent and (values_to_parse > 0 or (values_to_parse == -1 and len(current_value) == 0)):
				log.warn( Language.get( Command, "missing_value" ) % current_option )
				
			options.append( (current_option, current_value) )
		
		return options
        norwegian_test_sentence = filtered_norwegian[index]

        english_test_sentences.append(english_test_sentence)
        norwegian_test_sentences.append(norwegian_test_sentence)

    return english_train_sentences, norwegian_train_sentences, english_test_sentences, norwegian_test_sentences


if __name__ == '__main__':
    np.random.seed(0)

    # Retrieve preprocessed sentence data
    english_sentences, norwegian_sentences = retrieve_data(
        './data/', 'subtitle_data.tmx')

    english_language = Language('english')
    norwegian_language = Language('norwegian')

    # Filter by length and pad
    max_length = 30
    padded_english_sentences, padded_norwegian_sentences = filter_pad_sentences(
        english_sentences, norwegian_sentences, max_length)

    # Create train and test set
    english_train_sentences, norwegian_train_sentences, english_test_sentences, norwegian_test_sentences = train_test_sentences(
        padded_english_sentences, padded_norwegian_sentences)

    # Count vocabulary in English and Norwegian sentences
    for sentence in english_train_sentences:
        english_language.count_words(sentence)
Ejemplo n.º 23
0
		def __init__(self):

			self.conf = Conf()
			self.home = self.conf.home
			self.op_folder = self.conf.op_folder

			Language(self.conf)
			
			title = _('Moitessier HAT Setup')

			wx.Frame.__init__(self, None, title=title, size=(710,460))
			
			self.SetFont(wx.Font(10, wx.FONTFAMILY_DEFAULT, wx.FONTSTYLE_NORMAL, wx.FONTWEIGHT_NORMAL))
			
			self.icon = wx.Icon(op_folder+'/static/icons/moitessier_hat.ico', wx.BITMAP_TYPE_ICO)
			self.SetIcon(self.icon)
			self.help_bmp = wx.Bitmap(self.op_folder + "/static/icons/help-browser.png", wx.BITMAP_TYPE_ANY)

			self.p = wx.lib.scrolledpanel.ScrolledPanel(self, -1, style=wx.TAB_TRAVERSAL | wx.SUNKEN_BORDER)
			self.p.SetAutoLayout(1)
			self.p.SetupScrolling()
			self.nb = wx.Notebook(self.p)
			self.p_info = wx.Panel(self.nb)
			self.p_settings = wx.Panel(self.nb)
			self.p_update = wx.Panel(self.nb)
			self.p_configure = wx.Panel(self.nb)
			self.nb.AddPage(self.p_info, _('HAT info'))
			self.nb.AddPage(self.p_update, _('Install drivers'))
			self.nb.AddPage(self.p_configure, _('OpenPlotter configuration'))
			self.nb.AddPage(self.p_settings, _('Play with settings'))
			sizer = wx.BoxSizer()
			sizer.Add(self.nb, 1, wx.EXPAND)
			self.p.SetSizer(sizer)

##################################################################### info

			info_box = wx.StaticBox(self.p_info, -1, _(' Info '))

			self.button_get_info =wx.Button(self.p_info, label= _('Settings'))
			self.Bind(wx.EVT_BUTTON, self.on_get_info, self.button_get_info)

			self.button_statistics =wx.Button(self.p_info, label= _('Statistics'))
			self.Bind(wx.EVT_BUTTON, self.on_statistics, self.button_statistics)

			self.button_reset_statistics =wx.Button(self.p_info, label= _('Reset statistics'))
			self.Bind(wx.EVT_BUTTON, self.on_reset_statistics, self.button_reset_statistics)

			sensors_box = wx.StaticBox(self.p_info, -1, _(' Test sensors '))

			self.button_MPU9250 =wx.Button(self.p_info, label= _('MPU-9250'))
			self.Bind(wx.EVT_BUTTON, self.on_MPU9250, self.button_MPU9250)

			self.button_MS560702BA03 =wx.Button(self.p_info, label= _('MS5607-02BA03'))
			self.Bind(wx.EVT_BUTTON, self.on_MS560702BA03, self.button_MS560702BA03)

			self.button_Si7020A20 =wx.Button(self.p_info, label= _('Si7020-A20'))
			self.Bind(wx.EVT_BUTTON, self.on_Si7020A20, self.button_Si7020A20)

			self.logger = rt.RichTextCtrl(self.p_info, style=wx.TE_MULTILINE|wx.TE_READONLY|wx.TE_DONTWRAP|wx.LC_SORT_ASCENDING)
			self.logger.SetMargins((10,10))

			help_button = wx.BitmapButton(self.p_info, bitmap=self.help_bmp, size=(self.help_bmp.GetWidth()+40, self.help_bmp.GetHeight()+10))
			help_button.Bind(wx.EVT_BUTTON, self.on_help)

			shop =wx.Button(self.p_info, label=_('Shop'))
			self.Bind(wx.EVT_BUTTON, self.onShop, shop)

			checkB =wx.Button(self.p_info, label=_('Check'))
			self.Bind(wx.EVT_BUTTON, self.onCheck, checkB)

			button_ok =wx.Button(self.p_info, label=_('Close'))
			self.Bind(wx.EVT_BUTTON, self.on_ok, button_ok)

			h_boxSizer1 = wx.StaticBoxSizer(info_box, wx.HORIZONTAL)
			h_boxSizer1.AddSpacer(5)
			h_boxSizer1.Add(self.button_get_info, 0, wx.ALL | wx.EXPAND, 5)
			h_boxSizer1.Add(self.button_statistics, 0, wx.ALL | wx.EXPAND, 5)
			h_boxSizer1.Add(self.button_reset_statistics, 0, wx.ALL | wx.EXPAND, 5)

			h_boxSizer3 = wx.StaticBoxSizer(sensors_box, wx.HORIZONTAL)
			h_boxSizer3.AddSpacer(5)
			h_boxSizer3.Add(self.button_MPU9250, 0, wx.ALL | wx.EXPAND, 5)
			h_boxSizer3.Add(self.button_MS560702BA03, 0, wx.ALL | wx.EXPAND, 5)
			h_boxSizer3.Add(self.button_Si7020A20, 0, wx.ALL | wx.EXPAND, 5)

			buttons = wx.BoxSizer(wx.HORIZONTAL)
			buttons.Add(help_button, 0, wx.ALL | wx.EXPAND, 0)
			buttons.Add(shop, 0, wx.LEFT | wx.EXPAND, 10)
			buttons.AddStretchSpacer(1)
			buttons.Add(checkB, 0, wx.ALL | wx.EXPAND, 0)
			buttons.Add(button_ok, 0, wx.LEFT | wx.EXPAND, 10)

			vbox3 = wx.BoxSizer(wx.VERTICAL)
			vbox3.Add(h_boxSizer1, 0, wx.ALL | wx.EXPAND, 5)
			vbox3.Add(h_boxSizer3, 0, wx.ALL | wx.EXPAND, 5)
			vbox3.Add(self.logger, 1, wx.ALL | wx.EXPAND, 5)
			vbox3.Add(buttons, 0, wx.ALL | wx.EXPAND, 5)

			self.p_info.SetSizer(vbox3)

##################################################################### settings

			gnss_box = wx.StaticBox(self.p_settings, -1, ' GNSS ')

			self.button_enable_gnss =wx.Button(self.p_settings, label= _('Enable'))
			self.Bind(wx.EVT_BUTTON, self.on_enable_gnss, self.button_enable_gnss)

			self.button_disable_gnss =wx.Button(self.p_settings, label= _('Disable'))
			self.Bind(wx.EVT_BUTTON, self.on_disable_gnss, self.button_disable_gnss)

			general_box = wx.StaticBox(self.p_settings, -1, _(' General '))

			self.button_reset =wx.Button(self.p_settings, label= _('Reset HAT'))
			self.Bind(wx.EVT_BUTTON, self.on_reset, self.button_reset)

			self.button_defaults =wx.Button(self.p_settings, label= _('Load defaults'))
			self.Bind(wx.EVT_BUTTON, self.on_defaults, self.button_defaults)

			ais_box = wx.StaticBox(self.p_settings, -1, ' AIS ')

			self.simulator = wx.CheckBox(self.p_settings, label=_('enable simulator'))

			interval_label = wx.StaticText(self.p_settings, -1, _('interval (ms)'))
			self.interval = wx.SpinCtrl(self.p_settings, min=1, max=9999, initial=1000)

			mmsi1_label = wx.StaticText(self.p_settings, -1, _('MMSI boat 1'))
			self.mmsi1 = wx.SpinCtrl(self.p_settings, min=111111, max=999999999, initial=5551122)

			mmsi2_label = wx.StaticText(self.p_settings, -1, _('MMSI boat 2'))
			self.mmsi2 = wx.SpinCtrl(self.p_settings, min=111111, max=999999999, initial=6884120)

			freq1_label = wx.StaticText(self.p_settings, -1, _('channel A [Hz]'))
			freq2_label = wx.StaticText(self.p_settings, -1, _('channel B [Hz]'))
			metamask_label = wx.StaticText(self.p_settings, -1, 'meta data')
			afcRange_label = wx.StaticText(self.p_settings, -1, 'AFC range [Hz]')

			self.rec1_freq1 = wx.SpinCtrl(self.p_settings, min=159000000, max=162025000, initial=161975000)
			self.rec1_freq2 = wx.SpinCtrl(self.p_settings, min=159000000, max=162025000, initial=162025000)
			self.rec1_metamask = wx.Choice(self.p_settings, choices=(_('none'),'RSSI'), style=wx.CB_READONLY)
			self.rec1_metamask.SetSelection(0)
			self.rec1_afcRange = wx.SpinCtrl(self.p_settings, min=500, max=2000, initial=1500)

			self.logger2 = rt.RichTextCtrl(self.p_settings, style=wx.TE_MULTILINE|wx.TE_READONLY|wx.TE_DONTWRAP|wx.LC_SORT_ASCENDING)
			self.logger2.SetMargins((10,10))

			help_button = wx.BitmapButton(self.p_settings, bitmap=self.help_bmp, size=(self.help_bmp.GetWidth()+40, self.help_bmp.GetHeight()+10))
			help_button.Bind(wx.EVT_BUTTON, self.on_help)

			self.button_apply =wx.Button(self.p_settings, label=_('Apply changes'))
			self.Bind(wx.EVT_BUTTON, self.on_apply, self.button_apply)

			button_ok2 =wx.Button(self.p_settings, label=_('Close'))
			self.Bind(wx.EVT_BUTTON, self.on_ok, button_ok2)

			h_boxSizer2 = wx.StaticBoxSizer(gnss_box, wx.HORIZONTAL)
			h_boxSizer2.AddSpacer(5)
			h_boxSizer2.Add(self.button_enable_gnss, 1, wx.ALL | wx.EXPAND, 5)
			h_boxSizer2.Add(self.button_disable_gnss, 1, wx.ALL | wx.EXPAND, 5)

			h_boxSizer4 = wx.StaticBoxSizer(general_box, wx.HORIZONTAL)
			h_boxSizer4.AddSpacer(5)
			h_boxSizer4.Add(self.button_reset, 1, wx.ALL | wx.EXPAND, 5)
			h_boxSizer4.Add(self.button_defaults, 1, wx.ALL | wx.EXPAND, 5)

			h_boxSizer5 = wx.BoxSizer(wx.HORIZONTAL)
			h_boxSizer5.Add(h_boxSizer2, 1, wx.RIGHT | wx.EXPAND, 10)
			h_boxSizer5.Add(h_boxSizer4, 1, wx.ALL | wx.EXPAND, 0)

			h_boxSizer6 = wx.BoxSizer(wx.HORIZONTAL)
			h_boxSizer6.Add((0,0), 1, wx.LEFT | wx.EXPAND, 5)
			h_boxSizer6.Add(interval_label, 1, wx.LEFT | wx.EXPAND, 5)
			h_boxSizer6.Add(mmsi1_label, 1, wx.LEFT | wx.EXPAND, 5)
			h_boxSizer6.Add(mmsi2_label, 1, wx.LEFT | wx.EXPAND, 5)

			h_boxSizer7 = wx.BoxSizer(wx.HORIZONTAL)
			h_boxSizer7.Add(self.simulator, 1, wx.ALL | wx.EXPAND, 5)
			h_boxSizer7.Add(self.interval, 1, wx.ALL | wx.EXPAND, 5)
			h_boxSizer7.Add(self.mmsi1, 1, wx.ALL | wx.EXPAND, 5)
			h_boxSizer7.Add(self.mmsi2, 1, wx.ALL | wx.EXPAND, 5)

			rec1_labels = wx.BoxSizer(wx.HORIZONTAL)
			rec1_labels.Add(freq1_label, 1, wx.LEFT | wx.EXPAND, 5)
			rec1_labels.Add(freq2_label, 1, wx.LEFT | wx.EXPAND, 5)
			rec1_labels.Add(metamask_label, 1, wx.LEFT | wx.EXPAND, 5)
			rec1_labels.Add(afcRange_label, 1, wx.LEFT | wx.EXPAND, 5)

			receiver1 = wx.BoxSizer(wx.HORIZONTAL)
			receiver1.Add(self.rec1_freq1, 1, wx.ALL | wx.EXPAND, 5)
			receiver1.Add(self.rec1_freq2, 1, wx.ALL | wx.EXPAND, 5)
			receiver1.Add(self.rec1_metamask, 1, wx.ALL | wx.EXPAND, 5)
			receiver1.Add(self.rec1_afcRange, 1, wx.ALL | wx.EXPAND, 5)

			v_boxSizer8 = wx.StaticBoxSizer(ais_box, wx.VERTICAL)
			v_boxSizer8.Add(h_boxSizer6, 0, wx.ALL | wx.EXPAND, 0)
			v_boxSizer8.Add(h_boxSizer7, 0, wx.ALL | wx.EXPAND, 0)
			v_boxSizer8.AddSpacer(15)
			v_boxSizer8.Add(rec1_labels, 0, wx.ALL | wx.EXPAND, 0)
			v_boxSizer8.Add(receiver1, 0, wx.ALL | wx.EXPAND, 0)
	

			buttons2 = wx.BoxSizer(wx.HORIZONTAL)
			buttons2.Add(help_button, 0, wx.ALL | wx.EXPAND, 0)
			buttons2.AddStretchSpacer(1)
			buttons2.Add(self.button_apply, 0, wx.RIGHT | wx.EXPAND, 10)
			buttons2.Add(button_ok2, 0, wx.ALL | wx.EXPAND, 0)

			vbox4 = wx.BoxSizer(wx.VERTICAL)
			vbox4.Add(h_boxSizer5, 0, wx.ALL | wx.EXPAND, 5)
			vbox4.Add(v_boxSizer8, 0, wx.ALL | wx.EXPAND, 5)
			vbox4.Add(self.logger2, 1, wx.ALL | wx.EXPAND, 5)
			vbox4.Add(buttons2, 0, wx.ALL | wx.EXPAND, 5)

			self.p_settings.SetSizer(vbox4)

##################################################################### install

			kernel_box = wx.StaticBox(self.p_update, -1, _(' Current kernel version '))

			self.kernel_label = wx.StaticText(self.p_update, -1)

			packages_box = wx.StaticBox(self.p_update, -1, _(' Available packages '))

			self.packages_list = []
			self.packages_select = wx.Choice(self.p_update, choices=self.packages_list, style=wx.CB_READONLY)
			self.readAvailable()

			self.button_install =wx.Button(self.p_update, label=_('Install'))
			self.Bind(wx.EVT_BUTTON, self.on_install, self.button_install)

			downloadB =wx.Button(self.p_update, label=_('Download'))
			self.Bind(wx.EVT_BUTTON, self.onDownload, downloadB)

			drivers =wx.Button(self.p_update, label=_('All drivers'))
			self.Bind(wx.EVT_BUTTON, self.onDrivers, drivers)

			self.logger3 = rt.RichTextCtrl(self.p_update, style=wx.TE_MULTILINE|wx.TE_READONLY|wx.TE_DONTWRAP|wx.LC_SORT_ASCENDING)
			self.logger3.SetMargins((10,10))

			help_button = wx.BitmapButton(self.p_update, bitmap=self.help_bmp, size=(self.help_bmp.GetWidth()+40, self.help_bmp.GetHeight()+10))
			help_button.Bind(wx.EVT_BUTTON, self.on_help)

			button_ok3 =wx.Button(self.p_update, label=_('Close'))
			self.Bind(wx.EVT_BUTTON, self.on_ok, button_ok3)

			v_kernel_box = wx.StaticBoxSizer(kernel_box, wx.VERTICAL)
			v_kernel_box.AddSpacer(5)
			v_kernel_box.Add(self.kernel_label, 0, wx.ALL | wx.EXPAND, 5)

			h_packages_box = wx.StaticBoxSizer(packages_box, wx.HORIZONTAL)
			h_packages_box.Add(self.packages_select, 1, wx.ALL | wx.EXPAND, 5)
			h_packages_box.Add(self.button_install, 0, wx.ALL | wx.EXPAND, 5)
			h_packages_box.Add(downloadB, 0, wx.ALL | wx.EXPAND, 5)
			h_packages_box.Add(drivers, 0, wx.ALL | wx.EXPAND, 5)

			buttons3 = wx.BoxSizer(wx.HORIZONTAL)
			buttons3.Add(help_button, 0, wx.ALL | wx.EXPAND, 0)
			buttons3.AddStretchSpacer(1)
			buttons3.Add(button_ok3, 0, wx.ALL | wx.EXPAND, 0)

			update_final = wx.BoxSizer(wx.VERTICAL)
			update_final.Add(v_kernel_box, 0, wx.ALL | wx.EXPAND, 5)
			update_final.Add(h_packages_box, 0, wx.ALL | wx.EXPAND, 5)
			update_final.Add(self.logger3, 1, wx.ALL | wx.EXPAND, 5)
			update_final.Add(buttons3, 0, wx.ALL | wx.EXPAND, 5)

			self.p_update.SetSizer(update_final)

##################################################################### configure

			checkConfB =wx.Button(self.p_configure, label= _('Check'))
			self.Bind(wx.EVT_BUTTON, self.onCheckConfB, checkConfB)

			self.logger4 = rt.RichTextCtrl(self.p_configure, style=wx.TE_MULTILINE|wx.TE_READONLY|wx.TE_DONTWRAP|wx.LC_SORT_ASCENDING)
			self.logger4.SetMargins((10,10))

			help_button = wx.BitmapButton(self.p_configure, bitmap=self.help_bmp, size=(self.help_bmp.GetWidth()+40, self.help_bmp.GetHeight()+10))
			help_button.Bind(wx.EVT_BUTTON, self.on_help)

			button_ok4 =wx.Button(self.p_configure, label=_('Close'))
			self.Bind(wx.EVT_BUTTON, self.on_ok, button_ok4)

			buttons4 = wx.BoxSizer(wx.HORIZONTAL)
			buttons4.Add(help_button, 0, wx.ALL | wx.EXPAND, 0)
			buttons4.AddStretchSpacer(1)
			buttons4.Add(checkConfB, 0, wx.ALL | wx.EXPAND, 0)
			buttons4.Add(button_ok4, 0, wx.LEFT | wx.EXPAND, 10)

			vbox5 = wx.BoxSizer(wx.VERTICAL)
			vbox5.Add(self.logger4, 1, wx.ALL | wx.EXPAND, 5)
			vbox5.Add(buttons4, 0, wx.ALL | wx.EXPAND, 5)

			self.p_configure.SetSizer(vbox5)

#####################################################################

			self.Centre()
			self.read()
Ejemplo n.º 24
0
from parser import Parser
from language import Language

#
# make language
#
language = Language({
    "special": [
        ["+"]
    ],
    "bracket": [
        ["(",")"],
        ['"','"', "quote"]
    ],
    "struct": [
        ["if"]
    ]
})

#
# make parser
#
parser = Parser(language)

#
# parse text
#
parser.parse("txt/test.txt")
Ejemplo n.º 25
0
class MainWindow():
    """Main window class."""

    def delete(self, widget, event=None):
        """Close the main window."""
        if os.path.exists('/tmp/.gbi'):
            shutil.rmtree('/tmp/.gbi')
        Gtk.main_quit()
        return False

    def next_page(self, widget, notebook):
        """Go to the next window."""
        page = self.notebook.get_current_page()
        if page == 0:
            self.lang.save_selection()
            kbbox = Gtk.VBox(False, 0)
            kbbox.show()
            self.kb = Keyboard(self.button3)
            get_kb = self.kb.get_model()
            kbbox.pack_start(get_kb, True, True, 0)
            label = Gtk.Label("Keyboard")
            self.notebook.insert_page(kbbox, label, 1)
            self.window.show_all()
            self.notebook.next_page()
            self.button1.set_sensitive(True)
            self.button3.set_sensitive(True)
        elif page == 1:
            self.kb.save_selection()
            tbbox = Gtk.VBox(False, 0)
            tbbox.show()
            self.tz = TimeZone(self.button3)
            get_tz = self.tz.get_model()
            tbbox.pack_start(get_tz, True, True, 0)
            label = Gtk.Label("TimeZone")
            self.notebook.insert_page(tbbox, label, 2)
            self.window.show_all()
            self.notebook.next_page()
            self.button3.set_sensitive(True)
        elif page == 2:
            self.tz.save_selection()
            typebox = Gtk.VBox(False, 0)
            typebox.show()
            self.types = Types()
            get_types = self.types.get_model()
            typebox.pack_start(get_types, True, True, 0)
            label = Gtk.Label("Types")
            self.notebook.insert_page(typebox, label, 3)
            self.window.show_all()
            self.notebook.next_page()
        elif page == 3:
            if self.types.get_type() == "ufs":
                partition_repos()
                udbox = Gtk.VBox(False, 0)
                udbox.show()
                self.partition = use_ufs(self.button3)
                get_ud = self.partition.get_model()
                udbox.pack_start(get_ud, True, True, 0)
                label = Gtk.Label("UFS Disk Configuration")
                self.notebook.insert_page(udbox, label, 4)
                self.window.show_all()
                self.notebook.next_page()
                self.button3.set_sensitive(False)
            elif self.types.get_type() == "custom":
                partition_repos()
                Pbox = Gtk.VBox(False, 0)
                Pbox.show()
                self.partition = Partitions(self.button3)
                get_part = self.partition.get_model()
                Pbox.pack_start(get_part, True, True, 0)
                label = Gtk.Label("UFS Custom Configuration")
                self.notebook.insert_page(Pbox, label, 4)
                self.window.show_all()
                self.notebook.next_page()
                self.button3.set_sensitive(False)
            elif self.types.get_type() == "zfs":
                Zbox = Gtk.VBox(False, 0)
                Zbox.show()
                self.partition = ZFS(self.button3)
                get_ZFS = self.partition.get_model()
                Zbox.pack_start(get_ZFS, True, True, 0)
                label = Gtk.Label("ZFS Configuration")
                self.notebook.insert_page(Zbox, label, 4)
                self.window.show_all()
                self.notebook.next_page()
                self.button3.set_sensitive(False)
        elif page == 4:
            self.partition.save_selection()
            Mbox = Gtk.VBox(False, 0)
            Mbox.show()
            self.bootmanager = bootManager()
            get_root = self.bootmanager.get_model()
            Mbox.pack_start(get_root, True, True, 0)
            label = Gtk.Label("Boot Option")
            self.notebook.insert_page(Mbox, label, 5)
            self.window.show_all()
            self.notebook.next_page()
            self.button3.set_sensitive(True)
        elif page == 5:
            Rbox = Gtk.VBox(False, 0)
            Rbox.show()
            self.rootuser = RootUser(self.button3)
            get_root = self.rootuser.get_model()
            Rbox.pack_start(get_root, True, True, 0)
            label = Gtk.Label("Root Password")
            self.notebook.insert_page(Rbox, label, 6)
            self.window.show_all()
            self.notebook.next_page()
            self.button3.set_sensitive(False)
        elif page == 6:
            self.rootuser.save_selection()
            Abox = Gtk.VBox(False, 0)
            Abox.show()
            self.adduser = AddUser(self.button3)
            get_adduser = self.adduser.get_model()
            Abox.pack_start(get_adduser, True, True, 0)
            label = Gtk.Label("Adding User")
            self.notebook.insert_page(Abox, label, 7)
            self.button3.set_label("Install")
            self.window.show_all()
            self.notebook.next_page()
            self.button3.set_sensitive(False)
        elif page == 7:
            self.adduser.save_selection()
            Ibox = Gtk.VBox(False, 0)
            Ibox.show()
            install = installSlide()
            get_install = install.get_model()
            Ibox.pack_start(get_install, True, True, 0)
            label = Gtk.Label("Installation")
            self.notebook.insert_page(Ibox, label, 8)
            self.notebook.next_page()
            instpro = installProgress()
            progressBar = instpro.getProgressBar()
            box1 = Gtk.VBox(False, 0)
            box1.show()
            label = Gtk.Label("Progress Bar")
            box1.pack_end(progressBar, False, False, 0)
            self.nbButton.insert_page(box1, label, 1)
            self.nbButton.next_page()
            self.window.show_all()

    def back_page(self, widget):
        """Go back to the previous window."""
        current_page = self.notebook.get_current_page()
        if current_page == 1:
            self.button1.set_sensitive(False)
        elif current_page == 7:
            self.button3.set_label("Next")
        self.notebook.prev_page()
        new_page = self.notebook.get_current_page()
        if current_page == 4 and new_page == 3:
            if os.path.exists(partitiondb):
                shutil.rmtree(partitiondb)
            if os.path.exists(tmp + 'create'):
                os.remove(tmp + 'create')
            if os.path.exists(tmp + 'delete'):
                os.remove(tmp + 'delete')
            if os.path.exists(tmp + 'destroy'):
                os.remove(tmp + 'destroy')
            if os.path.exists(tmp + 'partlabel'):
                os.remove(tmp + 'partlabel')
            if os.path.exists(zfs_config):
                os.remove(zfs_config)
            if os.path.exists(ufs_config):
                os.remove(ufs_config)
            if os.path.exists(disk):
                os.remove(disk)
            if os.path.exists(dslice):
                os.remove(dslice)
            if os.path.exists(disk_schem):
                os.remove(disk_schem)
        self.button3.set_sensitive(True)

    def __init__(self):
        """Were the Main window start."""
        self.window = Gtk.Window()
        self.window.connect("delete_event", self.delete)
        self.window.set_border_width(0)
        self.window.set_default_size(800, 500)
        self.window.set_size_request(800, 500)
        self.window.set_title("GhostBSD Installer")
        self.window.set_border_width(0)
        self.window.set_icon_from_file(logo)
        mainHBox = Gtk.HBox(False, 0)
        mainHBox.show()
        mainVbox = Gtk.VBox(False, 0)
        mainVbox.show()
        self.window.add(mainHBox)
        mainHBox.pack_start(mainVbox, True, True, 0)
        # Create a new self.notebook
        self.notebook = Gtk.Notebook()
        mainVbox.pack_start(self.notebook, True, True, 0)
        self.notebook.show()
        self.notebook.set_show_tabs(False)
        self.notebook.set_show_border(False)
        vbox = Gtk.VBox(False, 0)
        vbox.show()
        self.lang = Language()
        get_lang = self.lang.get_model()
        # self.lang = Installs()
        # get_lang = self.lang.get_model()
        vbox.pack_start(get_lang, True, True, 0)
        label = Gtk.Label("Language")
        self.notebook.insert_page(vbox, label, 0)

        # Set what page to start at Language
        self.notebook.set_current_page(0)

        # Create buttons
        self.table = Gtk.Table(1, 6, True)

        self.button1 = Gtk.Button(label='Back')
        self.button1.connect("clicked", self.back_page)
        self.table.attach(self.button1, 3, 4, 0, 1)
        self.button1.show()
        self.button1.set_sensitive(False)

        self.button2 = Gtk.Button(label='Cancel')
        self.button2.connect("clicked", self.delete)
        self.table.attach(self.button2, 4, 5, 0, 1)
        self.button2.show()

        self.button3 = Gtk.Button(label='Next')
        self.button3.connect("clicked", self.next_page, self.notebook)
        self.table.attach(self.button3, 5, 6, 0, 1)
        self.button3.show()

        self.table.set_col_spacings(5)
        self.table.show()
        # Create a new notebook
        self.nbButton = Gtk.Notebook()
        mainVbox.pack_end(self.nbButton, False, False, 5)
        self.nbButton.show()
        self.nbButton.set_show_tabs(False)
        self.nbButton.set_show_border(False)
        label = Gtk.Label("Button")
        self.nbButton.insert_page(self.table, label, 0)
        self.window.show_all()
Ejemplo n.º 26
0
def load_glob(pattern):
    result = []
    for file_name in glob(pattern):
        with open(file_name) as f:
            result.append(Language(f.read(), language_name(file_name)))
    return result
Ejemplo n.º 27
0
class MainWindow:
    def delete(self, widget, event=None):
        if os.path.exists('/tmp/.gbi'):
            shutil.rmtree('/tmp/.gbi')
        Gtk.main_quit()
        return False

    def next_page(self, widget, notebook):
        page = self.notebook.get_current_page()
        if page == 0:
            self.lang.save_selection()
            Kbbox = Gtk.VBox(False, 0)
            Kbbox.show()
            self.kb = Keyboard(self.button3)
            get_kb = self.kb.get_model()
            Kbbox.pack_start(get_kb, True, True, 0)
            label = Gtk.Label("Keyboard")
            self.notebook.insert_page(Kbbox, label, 1)
            self.window.show_all()
            self.notebook.next_page()
            self.button1.set_sensitive(True)
            self.button3.set_sensitive(False)
        elif page == 1:
            self.kb.save_selection()
            Tbbox = Gtk.VBox(False, 0)
            Tbbox.show()
            self.tz = TimeZone(self.button3)
            get_tz = self.tz.get_model()
            Tbbox.pack_start(get_tz, True, True, 0)
            label = Gtk.Label("TimeZone")
            self.notebook.insert_page(Tbbox, label, 2)
            self.window.show_all()
            self.notebook.next_page()
            self.button3.set_sensitive(False)
        elif page == 2:
            self.tz.save_selection()
            typebox = Gtk.VBox(False, 0)
            typebox.show()
            self.types = Types()
            get_types = self.types.get_model()
            typebox.pack_start(get_types, True, True, 0)
            label = Gtk.Label("Types")
            self.notebook.insert_page(typebox, label, 3)
            self.window.show_all()
            self.notebook.next_page()
        elif page == 3:
            partition_repos()
            if self.types.get_type() == "disk":
                Udbox = Gtk.VBox(False, 0)
                Udbox.show()
                self.partition = UFSDisk(self.button3)
                get_UD = self.partition.get_model()
                Udbox.pack_start(get_UD, True, True, 0)
                label = Gtk.Label("UFS Disk Configuration")
                self.notebook.insert_page(Udbox, label, 4)
                self.window.show_all()
                self.notebook.next_page()
                self.button3.set_sensitive(False)
            elif self.types.get_type() == "custom":
                Pbox = Gtk.VBox(False, 0)
                Pbox.show()
                self.partition = Partitions(self.button3)
                get_part = self.partition.get_model()
                Pbox.pack_start(get_part, True, True, 0)
                label = Gtk.Label("UFS Custom Configuration")
                self.notebook.insert_page(Pbox, label, 4)
                self.window.show_all()
                self.notebook.next_page()
                self.button3.set_sensitive(False)
            elif self.types.get_type() == "zfs":
                Zbox = Gtk.VBox(False, 0)
                Zbox.show()
                self.partition = ZFS(self.button3)
                get_ZFS = self.partition.get_model()
                Zbox.pack_start(get_ZFS, True, True, 0)
                label = Gtk.Label("ZFS Configuration")
                self.notebook.insert_page(Zbox, label, 4)
                self.window.show_all()
                self.notebook.next_page()
                self.button3.set_sensitive(False)
        elif page == 4:
            self.partition.save_selection()
            Rbox = Gtk.VBox(False, 0)
            Rbox.show()
            self.rootuser = RootUser(self.button3)
            get_root = self.rootuser.get_model()
            Rbox.pack_start(get_root, True, True, 0)
            label = Gtk.Label("Root Password")
            self.notebook.insert_page(Rbox, label, 5)
            self.window.show_all()
            self.notebook.next_page()
            self.button3.set_sensitive(False)
        elif page == 5:
            self.rootuser.save_selection()
            Abox = Gtk.VBox(False, 0)
            Abox.show()
            self.adduser = AddUser(self.button3)
            get_adduser = self.adduser.get_model()
            Abox.pack_start(get_adduser, True, True, 0)
            label = Gtk.Label("Adding User")
            self.notebook.insert_page(Abox, label, 6)
            self.button3.set_label("Install")
            self.window.show_all()
            self.notebook.next_page()
            self.button3.set_sensitive(False)
        elif page == 6:
            self.adduser.save_selection()
            Ibox = Gtk.VBox(False, 0)
            Ibox.show()
            install = installSlide()
            get_install = install.get_model()
            Ibox.pack_start(get_install, True, True, 0)
            label = Gtk.Label("Installation")
            self.notebook.insert_page(Ibox, label, 7)
            self.notebook.next_page()
            instpro = installProgress()
            progressBar = instpro.getProgressBar()
            box1 = Gtk.VBox(False, 0)
            box1.show()
            label = Gtk.Label("Progress Bar")
            box1.pack_end(progressBar, False, False, 0)
            self.nbButton.insert_page(box1, label, 1)
            self.nbButton.next_page()
            self.window.show_all()

    def back_page(self, widget):
        current_page = self.notebook.get_current_page()
        if current_page == 1:
            self.button1.set_sensitive(False)
        elif current_page == 6:
            self.button3.set_label("Next")
        self.notebook.prev_page()
        new_page = self.notebook.get_current_page()
        if current_page == 4 and new_page == 3:
            if os.path.exists(partitiondb):
                shutil.rmtree(partitiondb)
            if os.path.exists(tmp + 'create'):
                os.remove(tmp + 'create')
            if os.path.exists(tmp + 'delete'):
                os.remove(tmp + 'delete')
            if os.path.exists(tmp + 'destroy'):
                os.remove(tmp + 'destroy')
            if os.path.exists(tmp + 'partlabel'):
                os.remove(tmp + 'partlabel')
            if os.path.exists(zfs_config):
                os.remove(zfs_config)
            if os.path.exists(disk):
                os.remove(disk)
            if os.path.exists(dslice):
                os.remove(dslice)
            if os.path.exists(disk_schem):
                os.remove(disk_schem)
        self.button3.set_sensitive(True)

    def __init__(self):
        self.window = Gtk.Window()
        self.window.connect("delete_event", self.delete)
        self.window.set_border_width(0)
        self.window.set_default_size(700, 500)
        self.window.set_size_request(700, 500)
        self.window.set_title("GhostBSD Installer")
        self.window.set_border_width(0)
        self.window.set_icon_from_file(logo)
        mainHBox = Gtk.HBox(False, 0)
        mainHBox.show()
        mainVbox = Gtk.VBox(False, 0)
        mainVbox.show()
        self.window.add(mainHBox)
        mainHBox.pack_start(mainVbox, True, True, 0)
        # Create a new self.notebook
        self.notebook = Gtk.Notebook()
        mainVbox.pack_start(self.notebook, True, True, 0)
        self.notebook.show()
        self.notebook.set_show_tabs(False)
        self.notebook.set_show_border(False)
        vbox = Gtk.VBox(False, 0)
        vbox.show()
        self.lang = Language()
        get_lang = self.lang.get_model()
        # self.lang = Installs()
        # get_lang = self.lang.get_model()
        vbox.pack_start(get_lang, True, True, 0)
        label = Gtk.Label("Language")
        self.notebook.insert_page(vbox, label, 0)

        # Set what page to start at Language
        self.notebook.set_current_page(0)

        # Create buttons
        self.table = Gtk.Table(1, 6, True)

        self.button1 = Gtk.Button(label='Back')
        self.button1.connect("clicked", self.back_page)
        self.table.attach(self.button1, 3, 4, 0, 1)
        self.button1.show()
        self.button1.set_sensitive(False)

        self.button2 = Gtk.Button(label='Cancel')
        self.button2.connect("clicked", self.delete)
        self.table.attach(self.button2, 4, 5, 0, 1)
        self.button2.show()

        self.button3 = Gtk.Button(label='Next')
        self.button3.connect("clicked", self.next_page, self.notebook)
        self.table.attach(self.button3, 5, 6, 0, 1)
        self.button3.show()

        self.table.set_col_spacings(5)
        self.table.show()
        # Create a new notebook
        self.nbButton = Gtk.Notebook()
        mainVbox.pack_end(self.nbButton, False, False, 5)
        self.nbButton.show()
        self.nbButton.set_show_tabs(False)
        self.nbButton.set_show_border(False)
        label = Gtk.Label("Button")
        self.nbButton.insert_page(self.table, label, 0)
        self.window.show_all()
Ejemplo n.º 28
0
    def __init__(self):
        self.window = Gtk.Window()
        self.window.connect("delete_event", self.delete)
        self.window.set_border_width(0)
        self.window.set_default_size(700, 500)
        self.window.set_size_request(700, 500)
        self.window.set_title("GhostBSD Installer")
        self.window.set_border_width(0)
        self.window.set_icon_from_file(logo)
        mainHBox = Gtk.HBox(False, 0)
        mainHBox.show()
        mainVbox = Gtk.VBox(False, 0)
        mainVbox.show()
        self.window.add(mainHBox)
        mainHBox.pack_start(mainVbox, True, True, 0)
        # Create a new self.notebook
        self.notebook = Gtk.Notebook()
        mainVbox.pack_start(self.notebook, True, True, 0)
        self.notebook.show()
        self.notebook.set_show_tabs(False)
        self.notebook.set_show_border(False)
        vbox = Gtk.VBox(False, 0)
        vbox.show()
        self.lang = Language()
        get_lang = self.lang.get_model()
        # self.lang = Installs()
        # get_lang = self.lang.get_model()
        vbox.pack_start(get_lang, True, True, 0)
        label = Gtk.Label("Language")
        self.notebook.insert_page(vbox, label, 0)

        # Set what page to start at Language
        self.notebook.set_current_page(0)

        # Create buttons
        self.table = Gtk.Table(1, 6, True)

        self.button1 = Gtk.Button(label='Back')
        self.button1.connect("clicked", self.back_page)
        self.table.attach(self.button1, 3, 4, 0, 1)
        self.button1.show()
        self.button1.set_sensitive(False)

        self.button2 = Gtk.Button(label='Cancel')
        self.button2.connect("clicked", self.delete)
        self.table.attach(self.button2, 4, 5, 0, 1)
        self.button2.show()

        self.button3 = Gtk.Button(label='Next')
        self.button3.connect("clicked", self.next_page, self.notebook)
        self.table.attach(self.button3, 5, 6, 0, 1)
        self.button3.show()

        self.table.set_col_spacings(5)
        self.table.show()
        # Create a new notebook
        self.nbButton = Gtk.Notebook()
        mainVbox.pack_end(self.nbButton, False, False, 5)
        self.nbButton.show()
        self.nbButton.set_show_tabs(False)
        self.nbButton.set_show_border(False)
        label = Gtk.Label("Button")
        self.nbButton.insert_page(self.table, label, 0)
        self.window.show_all()
Ejemplo n.º 29
0
	def __init__(self, encoding, substitutions, verbose, log):
		Language.__init__(self, encoding, substitutions, verbose, log)
Ejemplo n.º 30
0
    # Generate a hexagonal grid over the continent
    class LGrid(Grid):
        grid = hexagonal_earth_grid(continent, area)
        all_gridcells = {}
        model = PopulationCapModel()

    land = (numpy.apply_along_axis(is_land, axis=2, arr=LGrid.grid[0]),
            numpy.apply_along_axis(is_land, axis=2, arr=LGrid.grid[1]))

    # Find a random starting cell that is capable of supporting at least one individual
    g = LGrid.random_cell()
    while LGrid.gridcell(*g).popcap < 1:
        g = LGrid.random_cell()

    # Create a population in that starting grid cell
    l = Language(1, LGrid.gridcell(*g))
    generation = 0
    while True:
        generation += 1
        while True:
            try:
                l.grow()
            except StopIteration as s:
                print([c.population for c in l.cells])
                print(l.popcap)
                print(s)
                break
        filled = {
            g
            for g in LGrid.all_gridcells.values() if g.language is not None
        }
Ejemplo n.º 31
0
def run(V, n, d):
    #n
    size = n
    #V
    vocab = V
    #d
    smoothing = d

    in_file1 = "./OriginalDataSet/training-tweets.txt"
    n_gram = N_Gram(in_file1, size, vocab, smoothing)
    [uni, bi, tri] = n_gram.count()
    language_count = n_gram.language_count

    tweets = []

    eu = Language('eu', uni['eu'], bi['eu'], tri['eu'])
    es = Language('es', uni['es'], bi['es'], tri['es'])
    pt = Language('pt', uni['pt'], bi['pt'], tri['pt'])
    en = Language('en', uni['en'], bi['en'], tri['en'])
    ca = Language('ca', uni['ca'], bi['ca'], tri['ca'])
    gl = Language('gl', uni['gl'], bi['gl'], tri['gl'])

    eu.cal_conditional_probabilities(size, smoothing)
    es.cal_conditional_probabilities(size, smoothing)
    pt.cal_conditional_probabilities(size, smoothing)
    en.cal_conditional_probabilities(size, smoothing)
    ca.cal_conditional_probabilities(size, smoothing)
    gl.cal_conditional_probabilities(size, smoothing)

    eu.export_model(vocab, size, smoothing)
    es.export_model(vocab, size, smoothing)
    pt.export_model(vocab, size, smoothing)
    en.export_model(vocab, size, smoothing)
    ca.export_model(vocab, size, smoothing)
    gl.export_model(vocab, size, smoothing)

    in_file2 = "./OriginalDataSet/test-tweets-given.txt"
    input_file = open(in_file2, "r", encoding="utf8")

    for line in input_file:
        try:
            [tweet_id, user_name, language, text] = line.split('\t')
            tweets.append(
                Tweet(tweet_id, user_name, language, text.strip('\n')))
        except:
            print('ERROR reading file')
    input_file.close()

    overall_result = Counter()
    language_result = {
        'eu': Counter(),
        'ca': Counter(),
        'gl': Counter(),
        'es': Counter(),
        'en': Counter(),
        'pt': Counter()
    }
    language_predictions = Counter()
    debug = 0

    if os.path.exists(f'ModifiedDataSet/trace_{vocab}_{size}_{smoothing}.txt'):
        os.remove(f'ModifiedDataSet/trace_{vocab}_{size}_{smoothing}.txt')
    for tweet in tweets:
        try:
            if vocab == 0:
                tweet.lower_case()
            elif vocab == 1:
                tweet.case_sensitive()
            elif vocab == 2:
                tweet.is_alpha()
            tweet.counter()

            count = {}
            if size == 1:
                count = tweet.uni
            elif size == 2:
                count = tweet.bi
            elif size == 3:
                count = tweet.tri

            score = {}

            score['eu'] = eu.score(count, language_count)
            score['es'] = es.score(count, language_count)
            score['ca'] = ca.score(count, language_count)
            score['gl'] = gl.score(count, language_count)
            score['pt'] = pt.score(count, language_count)
            score['en'] = en.score(count, language_count)

            estimate_l = max(score, key=score.get)
            estimate_s = score[estimate_l]

            if debug < 0:
                print(tweet.tweet_id)
                print(score)
                print(estimate_l, tweet.language)

            language_predictions[estimate_l] += 1
            if estimate_l == tweet.language:
                overall_result['right'] += 1
                language_result[tweet.language]['right'] += 1
            else:
                overall_result['wrong'] += 1
                language_result[tweet.language]['wrong'] += 1

            # Trace Output File
            with open(f'ModifiedDataSet/trace_{vocab}_{size}_{smoothing}.txt',
                      'a',
                      encoding='utf8') as trace_file:
                correct_wrong = 'correct' if estimate_l == tweet.language else 'wrong'
                trace_file.write(
                    f'{tweet.tweet_id}  {estimate_l}  {estimate_s:.2E}  {tweet.language}  {correct_wrong}\n'
                )

            debug += 1
        except Exception as error_msg:
            print(f'ERROR calculating score: {error_msg}')

    # Eval Output File
    with open(f'ModifiedDataSet/eval_{vocab}_{size}_{smoothing}.txt',
              'w',
              encoding='utf8') as eval_file:
        accuracy = round(
            overall_result['right'] / sum(overall_result.values()), 4)
        per_class_precision = []
        per_class_recall = []
        for language in language_result:
            per_class_precision.append(
                round(
                    language_result[language]['right'] /
                    language_predictions[language],
                    4)) if language_predictions[
                        language] > 0 else per_class_precision.append(0)
            per_class_recall.append(
                round(
                    language_result[language]['right'] /
                    sum(language_result[language].values()), 4))
        per_class_f1 = [
            round((x * y) / (x + y), 2) if x > 0 or y > 0 else 0.0
            for x, y in zip(per_class_precision, per_class_recall)
        ]
        macro_f1 = round(sum(per_class_f1) / len(per_class_f1), 4)

        weighted_f1 = 0
        for index, language in enumerate(language_result):
            weighted_f1 += sum(
                language_result[language].values()) * per_class_f1[index]
        weighted_f1 = round(weighted_f1 / sum(overall_result.values()), 4)

        eval_file.write(f'{accuracy}\n')
        eval_file.writelines(f'{c}  ' for c in per_class_precision)
        eval_file.write('\n')
        eval_file.writelines(f'{c}  ' for c in per_class_recall)
        eval_file.write('\n')
        eval_file.writelines(f'{c}  ' for c in per_class_f1)
        eval_file.write('\n')
        eval_file.write(f'{macro_f1}  {weighted_f1}')

    print('right: ',
          (overall_result['right'] / sum(overall_result.values())) * 100, '%')
    print('wrong: ',
          (overall_result['wrong'] / sum(overall_result.values())) * 100, '%')
Ejemplo n.º 32
0
	def country_name( self ):
		return Language.get( Ingredient, "country_%s" % self.country )
Ejemplo n.º 33
0
import glob
import io
from network import Network
from language import Language
from pybrain.tools.validation import CrossValidator
from pybrain.tools.validation import ModuleValidator

languages = []

for g in glob.glob("./data/*.txt"):
  language, num = g.split("/")[-1].split("_")
  languages.append(Language(io.open(g, 'r+'), language))

n = Network(languages)
n.train()
n.trainer.verbose = True
n.trainer.trainUntilConvergence()

def correctValFunc(output, target):
  assert len(output) == len(target)

  n_correct = 0

  for idx, instance in enumerate(output):
    # This will find the maximum liklihood language
    classification = instance.argmax(axis=0)
    objective = target[idx].argmax(axis=0)
    if objective == classification:
      n_correct += 1

  return 1 - (float(n_correct) / float(len(output)))
 def __init__(self):
     Language.__init__(self, "Portuguese")
     f = open("../res/portuguese.txt")
     self.generate_table(f.read())
     f.close()
Ejemplo n.º 35
0
# Run the experiment
for nn in range(n):

	if not mute:
		# Report every n/100 times for boredom reasons
		if nn % 100 == 0:
			print(nn, 'iterations run...', file=sys.stderr)

	# Initialize families
	families = []

	for ff in range(f):
		# Generate random language to use as root for family
		randomname = 1000 * ff
		randomroot = Language(constraints, name=randomname)
		randomroot.randomize_ranking()
		families.append(Family(randomroot))

	# Evolve family
	for family in families:
		family.evolve(l,p)
	languages = []
	for family in families:
		languages += family.get_leaves()
	languagenames = []
	for language in languages:
		languagenames.append(language.__name__)
	languagenames.sort()
	# Output gold tree as a .dot file; then call dot -T png -o [tree].png [tree].dot
#	family.tree_to_dot()
Ejemplo n.º 36
0
    def post(self, token_id):
        # Session request handler
        current_session = Session(self)
        JINJA_ENVIRONMENT.globals['session'] = current_session
        # Language request handler
        Language.language(self)

        # Load templates
        change_template = JINJA_ENVIRONMENT.get_template('static/templates/changeProfile.html')

        token = database.TokenManager.select_token_by_id(int(token_id))
        if not(token and (datetime.datetime.now() - datetime.timedelta(days=1) < token.date) and (not token.used)):
            self.redirect("/")

        user = token.user.get()

        # Check username
        username = cgi.escape(self.request.get('username'))
        # If username is modified
        if username != user.name:
            # Username not empty
            if len(username) < 1:
                self.response.write(change_template.render(user=user, error=_("EmptyUsername.")))
                return None
            # Check username not taken
            dbusername = database.UserManager.select_by_username(username)
            if dbusername is not None:
                self.response.write(change_template.render(user=user, error=_("UsernameExists")))
                return None

        # Check email
        email = cgi.escape(self.request.get('email'))
        # If email is modified
        if email != user.email:
            # Check email is well formed
            if not re.match(r"[^@]+@[^@]+\.[^@]+", email):
                self.response.write(change_template.render(user=user, error=_("BadEmail.")))
                return None
            # Check email not taken
            dbuseremail = database.UserManager.select_by_email(email)
            if dbuseremail is not None:
                self.response.write(change_template.render(user=user, error=_("EmailExists")))
                return None

        # Check passwords
        request_password1 = self.request.get('password1', None)
        request_password2 = self.request.get('password2', None)
        password1 = cgi.escape(request_password1)
        password2 = cgi.escape(request_password2)
        # Save user and email if passwords are not changed
        if len(password1) < 1 or len(password2) < 1:
            database.UserManager.modify_user(user.key, username=username, email=email)
        else:
            # Check passwords before modify user data
            if len(password1) < 6:
                self.response.write(change_template.render(user=user, error=_("PasswordMinLengthNotReached.")))
                return None
            # Check passwords match
            if password1 != password2:
                self.response.write(change_template.render(user=user, error=_("PasswordMissmatch")))
                return None
            database.UserManager.modify_user(user.key, username=username, password=password1, email=email)

        # If data is changed, set token as used
        database.TokenManager.set_used_token(token.key)
        # Response changed values
        self.redirect("/profile/"+str(user.key.id()))
 def __init__(self):
     Language.__init__(self, "Italian")
     f = open("../res/italian.txt")
     self.generate_table(f.read())
     f.close()
Ejemplo n.º 38
0
def PLC():
    """Main PLC application"""

    global code_processor

    # Create user input string template
    input_msg = "{} or 'q' to abort: "

    # Create input messages
    input_file_path_msg = input_msg.format("Path to Program File")
    input_language_msg = input_msg.format("To Language")
    input_file_name_msg = input_msg.format("Output File Path")

    # Create variable to store function result
    lang_from = None

    #
    # Get User Input
    #

    validate_methods = [
        (FilePath.validate_file_path, "file_path", input_file_path_msg),
        (Language.validate, "lang_to", input_language_msg),
        (FilePath.validate_file_name, "outfile_path", input_file_name_msg),
    ]

    # Validate user input
    for func, var_name, input_str in validate_methods:

        # Get input from user
        user_input_val, error = get_user_input(func, var_name, input_str)

        user_input_val = str(user_input_val)

        # Check if input is currently at to language
        # and input language is same as output language
        if var_name == "lang_to" and user_input_val.lower() == lang_from:
            error = "Language of file is same as to conversion language"

        # If error encountered, print error and exit
        while error:

            # Parse the error
            Error.parse(error, user_input=True)

            # Get input from user
            user_input_val, error = get_user_input(func, var_name, input_str)

            # Check if input is currently at to language
            # and input language is same as output language
            if var_name == "lang_to" and user_input_val.lower() == lang_from:
                error = "Language of file is same as to conversion language"

        # Store latest value of var
        user_input[var_name] = user_input_val

        # If var_name is file_path recognize language of infile
        if var_name == "file_path":
            lang_from = Language.recognize(user_input_val)

        # else if var_name is language,
        # store lowercase string of var_name
        elif var_name == "lang_to":
            user_input[var_name] = user_input_val.lower()
    #
    # Start Conversion
    #

    # Make local variables for keys in user_input dict
    file_path = user_input['file_path']
    lang_to = user_input['lang_to']
    outfile_path = user_input['outfile_path']
    user_input['lang_from'] = lang_from

    print(lang_from, "->", lang_to)

    # Create code processor instance
    code_processor = CodeProcessor(file_path, lang_from, lang_to, outfile_path,
                                   corrections)

    # Run convert method of code processor
    code_processor.convert()

    # Write converted file to disk
    error = code_processor.write_file_to_disk()

    # Check if error occurred
    if error:
        if error == 5:
            Error.parse(5, quit_=True)
        Error.parse(error, user_input=False)

    return 0
Ejemplo n.º 39
0
# -*- coding: utf-8 -*-

import os
import sys

basepath = os.path.dirname(
    os.path.dirname(os.path.dirname(os.path.abspath(__file__))))

sys.path.append(basepath)
os.chdir(basepath)

from language import Language
Language.initialize(lang="en", path=basepath + os.sep + "i18n")
Ejemplo n.º 40
0
import tkinter as tk
from window import Window
from language import Language

if __name__ == "__main__":
    root = tk.Tk()
    root.title("Program Line Counter v0.00.002")
    languages = Language.loadLanguages("config/languages.json")
    window = Window(root, languages)
    root.mainloop()
Ejemplo n.º 41
0
import util
from sklearn.metrics import classification_report, confusion_matrix
import numpy as np
from keras.models import Model, Input
from sklearn.metrics import accuracy_score

if __name__ == '__main__':
	pre = Preprocessing()
	df_input = pre.data
	train_data = pre.train_data
	test_data = pre.test_data
	Y_train = pre.Y_train
	Y_test = pre.Y_test


	lng = Language(df_input)

	print("Preparing the language data")
	train_tokens = train_data['title'].apply(util.get_tokens)
	lng_data_train = lng.get_encoded_data(train_tokens)

	test_tokens = test_data['title'].apply(util.get_tokens)
	lng_data_test = lng.get_encoded_data(test_tokens)
	language_model = lng.lng_model
	print("training the language model (bi-lstm), this might take some time")
	language_model.fit(lng_data_train, Y_train, verbose=1, validation_split=0.2, nb_epoch=5)

	## printing precision_recall- language modality
	Y_pred = language_model.predict(lng_data_test, verbose=1)
	y_pred = np.array([np.argmax(pred) for pred in Y_pred])
	print("******************language modality scores(unimodal)*******************************")
Ejemplo n.º 42
0
def makeSyntaxlang(parent, module):
    usyntaxlang = Language('syntax', parent, module)
    syntaxlang = usyntaxlang.__impl__

    # {{{ SeqOp (continuation op)
    class SeqOp(RuleOp):
        __syntax__ = OperatorSyntax('seq', 100,
                                    (ExpressionRule(), Rep1(TermRule())))

        macro = staticmethod(
            lambda *seq: CallFunction(getRule(SequenceRule), *seq))

    syntaxlang.continuationOp = SeqOp
    SeqOp.__language__ = usyntaxlang

    # }}}

    # {{{ lit
    class LiteralOp(RuleOp):

        __syntax__ = OperatorSyntax('lit', 150, (None, ExpressionRule()))

        macro = staticmethod(
            lambda lit: CallFunction(getRule(LiteralRule), lit))

    syntaxlang.addOp(LiteralOp)
    # }}}

    syntaxlang.setTokenMacro("string", lambda s: LiteralOp(s))

    # {{{ ' and "
    class SingleQuoteOp(RuleOp):

        __syntax__ = OperatorSyntax(
            "'", 0,
            (None,
             SequenceRule(FreetextRule(r"[^'\\]*(?:\\.[^'\\]*)*", upto=False),
                          LiteralRule("'"))))

        def macro(text):
            return CallFunction(getRule(LiteralRule), eval(repr(text)))

        macro = staticmethod(macro)

    class DoubleQuoteOp(RuleOp):

        __syntax__ = OperatorSyntax(
            '"', 0,
            (None,
             SequenceRule(FreetextRule(r'[^"\\]*(?:\\.[^"\\]*)*', upto=False),
                          LiteralRule('"'))))

        def macro(text):
            return CallFunction(getRule(LiteralRule), eval(repr(text)))

        macro = staticmethod(macro)

    syntaxlang.addOp(SingleQuoteOp)
    syntaxlang.addOp(DoubleQuoteOp)

    # }}}

    # {{{ expr
    class ExprOp(RuleOp):
        __syntax__ = OperatorSyntax(
            'expr', 200,
            (None,
             Opt(
                 SequenceRule(LiteralRule("@"),
                              ChoiceRule(LiteralRule("^"), TermRule())))))

        macro = staticmethod(
            lambda lang=None: CallFunction(getRule(ExpressionRule), lang))

    syntaxlang.addOp(ExprOp)

    # }}}

    # {{{ term
    class TermOp(RuleOp):
        __syntax__ = OperatorSyntax(
            'term', 200,
            (None,
             Opt(
                 SequenceRule(LiteralRule("@"),
                              ChoiceRule(LiteralRule("^"), TermRule())))))

        macro = staticmethod(
            lambda lang=None: CallFunction(getRule(TermRule), lang))

    syntaxlang.addOp(TermOp)

    # }}}

    # {{{ token
    class TokenOp(RuleOp):
        __syntax__ = OperatorSyntax(
            'token', 200,
            (None,
             Opt(
                 SequenceRule(LiteralRule("@"),
                              ChoiceRule(LiteralRule("^"), TermRule())))))

        macro = staticmethod(
            lambda lang=None: CallFunction(getRule(TokenRule), lang))

    syntaxlang.addOp(TokenOp)

    # }}}

    # {{{ block
    class BlockOp(RuleOp):
        __syntax__ = OperatorSyntax(
            'block', 200,
            (None,
             Opt(
                 SequenceRule(LiteralRule("@"),
                              ChoiceRule(LiteralRule("^"), TermRule())))))

        macro = staticmethod(
            lambda lang=None: CallFunction(getRule(BlockRule), lang))

    syntaxlang.addOp(BlockOp)

    # }}}

    # {{{ symbol
    class SymbolOp(RuleOp):
        __syntax__ = OperatorSyntax('symbol', 200, (None, None))

        macro = staticmethod(
            lambda lang=None: CallFunction(getRule(SymbolRule)))

    syntaxlang.addOp(SymbolOp)

    # }}}

    # {{{ eol
    class EolOp(RuleOp):

        __syntax__ = OperatorSyntax('eol', 0, (None, None))

        macro = staticmethod(lambda: CallFunction(getRule(EolRule)))

    syntaxlang.addOp(EolOp)

    # }}}

    # {{{ debug
    class DebugOp(RuleOp):

        __syntax__ = OperatorSyntax(
            'debug', 0, (None,
                         SequenceRule(LiteralRule("("), Opt(ExpressionRule()),
                                      LiteralRule(")"))))

        def macro(valx=None):
            if isinstance(valx, Symbol):
                valx2 = str(valx)
            else:
                valx2 = valx
            return CallFunction(getRule(DebugRule), valx2)

        macro = staticmethod(macro)

    syntaxlang.addOp(DebugOp)

    # }}}

    # {{{ +
    class Rep1Op(RuleOp):

        __syntax__ = OperatorSyntax('+', 120, (ExpressionRule(), None))

        macro = staticmethod(lambda rule: CallFunction(getRule(Rep1), rule))

    syntaxlang.addOp(Rep1Op)

    # }}}

    # {{{ *
    class RepOp(RuleOp):

        __syntax__ = OperatorSyntax('*', 120, (ExpressionRule(), None))

        macro = staticmethod(lambda rule: CallFunction(getRule(Rep), rule))

    syntaxlang.addOp(RepOp)

    # }}}

    # {{{ $
    class NamedRuleOp(RuleOp):

        __syntax__ = OperatorSyntax(
            '$', 110, (None,
                       SequenceRule(Opt(TermRule(), None), LiteralRule(':'),
                                    ExpressionRule())))

        def macro(namex, rulex):
            if isinstance(namex, Symbol):
                n = str(namex)
            else:
                n = namex

            if isinstance(rulex, OptOp) and len(rulex) == 1:
                subrule = rulex[0]
                return CallFunction(getRule(NamedRule), n, OptOp(subrule, '-'))
            else:
                return CallFunction(getRule(NamedRule), n, rulex)

        macro = staticmethod(macro)

    syntaxlang.addOp(NamedRuleOp)

    # }}}

    # {{{ |
    class ChoiceOp(RuleOp):

        __syntax__ = OperatorSyntax(
            '|', 90,
            (ExpressionRule(),
             SequenceRule(
                 ExpressionRule(),
                 Rep(SequenceRule(LiteralRule("|"), ExpressionRule())))))

        macro = staticmethod(
            lambda *choices: CallFunction(getRule(ChoiceRule), *choices))

    syntaxlang.addOp(ChoiceOp)

    # }}}

    # {{{ (
    class ParenOp(RuleOp):

        __syntax__ = OperatorSyntax(
            '(', 0, (None, SequenceRule(ExpressionRule(), LiteralRule(')'))))

        macro = staticmethod(lambda rulex: rulex)

    syntaxlang.addOp(ParenOp)
    # }}}

    # {{{ [
    Seq = SequenceRule

    class OptOp(RuleOp):

        __syntax__ = OperatorSyntax(
            '[', 200,
            (None,
             Seq(
                 ExpressionRule(), LiteralRule(']'),
                 Opt(
                     Seq(LiteralRule("/"),
                         ChoiceRule(LiteralRule("-"), ExpressionRule()))))))

        def macro(rulex, altx=None):
            if altx == '-':
                return CallFunction(getRule(Opt), rulex)
            else:
                if isinstance(altx, Symbol):
                    altx = str(altx)
                return CallFunction(getRule(Opt), rulex, altx)

        macro = staticmethod(macro)

    syntaxlang.addOp(OptOp)

    # }}}

    # {{{ <
    class TrivialOp(RuleOp):

        __syntax__ = OperatorSyntax(
            '<', 0,
            (None, SequenceRule(Opt(ExpressionRule()), LiteralRule('>'))))

        def macro(resultx=None):
            if isinstance(resultx, Symbol):
                resultx = str(resultx)

            if resultx is None:
                return CallFunction(getRule(TrivialRule))
            else:
                return CallFunction(getRule(TrivialRule), resultx)

        macro = staticmethod(macro)

    syntaxlang.addOp(TrivialOp)

    # }}}

    # {{{ freetext
    class FreetextOp(RuleOp):

        __syntax__ = OperatorSyntax(
            'freetext', 200,
            (None,
             SequenceRule(Opt(LiteralRule("upto"), False), LiteralRule('/'),
                          FreetextRule(r"[^/\\]*(?:\\.[^/\\]*)*", upto=False),
                          LiteralRule('/'))))

        def macro(upto, terminator):
            return CallFunction(getRule(FreetextRule), terminator, bool(upto))

        macro = staticmethod(macro)

    syntaxlang.addOp(FreetextOp)

    # }}}

    # {{{ optext
    class OptextOp(RuleOp):

        __syntax__ = OperatorSyntax(
            'optext', 200,
            (None,
             SequenceRule(
                 NamedRule("lang",
                           Opt(SequenceRule(LiteralRule("@"), SymbolRule()))),
                 LiteralRule('/'),
                 FreetextRule(r"[^/\\]*(?:\\.[^/\\]*)*", upto=False),
                 LiteralRule('/'))))

        def macro(terminator, lang=None):
            return CallFunction(getRule(OptextRule), terminator, lang)

        macro = staticmethod(macro)

    syntaxlang.addOp(OptextOp)

    # }}}

    # {{{ symbol:
    class ParsedNameRuleOp(RuleOp):

        __syntax__ = OperatorSyntax('symbol:', 110, (None, ExpressionRule()))

        def macro(rulex):
            return CallFunction(getRule(ParsedNameRule), rulex)

        macro = staticmethod(macro)

    syntaxlang.addOp(ParsedNameRuleOp)
    # }}}

    # make these op-classes available as module fields (needed for quoting)
    globals().update(
        dict([(cls.__name__, cls) for cls in syntaxlang.operators.values()]))
    globals()['LiteralOp'] = LiteralOp
    globals()['SeqOp'] = SeqOp
    return usyntaxlang
Ejemplo n.º 43
0
def makeSyntaxlang(parent, module):
    op = Doc.new

    Seq = SequenceRule

    def makeRule(*args, **kws):
        className = args[0].__name__
        args = args[1:]
        return op(callOp, op(getRuleClass, className), *args, **kws)

    ulang = Language('syntax', parent, module)
    lang = ulang.__impl__

    # {{{ seq (continuation op)
    seq = lang.newOp('seq', 100, (ExpressionRule(), Rep1(TermRule())))
    seq.macro = lambda *seq: makeRule(SequenceRule, *seq)
    lang.continuationOp = seq
    # }}}

    # {{{ lit
    lang.newOp(
        'lit', 150,
        (None,
         ExpressionRule())).macro = lambda lit: makeRule(LiteralRule, lit)
    # }}}

    # {{{ ' and "
    lang.newOp(
        "'", 0,
        (None,
         SequenceRule(FreetextRule(r"[^'\\]*(?:\\.[^'\\]*)*", upto=False),
                      LiteralRule("'"))
         )).macro = lambda text: makeRule(LiteralRule, eval(repr(text)))

    lang.newOp(
        '"', 0,
        (None,
         SequenceRule(FreetextRule(r'[^"\\]*(?:\\.[^"\\]*)*', upto=False),
                      LiteralRule('"'))
         )).macro = lambda text: makeRule(LiteralRule, eval(repr(text)))
    # }}}

    # {{{ expr
    lang.newOp('expr', 200,
               (None,
                Opt(
                    SequenceRule(LiteralRule("@"),
                                 ChoiceRule(LiteralRule("^"), TermRule())))
                )).macro = lambda lang=None: makeRule(ExpressionRule, lang)
    # }}}

    # {{{ term
    lang.newOp('term', 200,
               (None,
                Opt(
                    SequenceRule(LiteralRule("@"),
                                 ChoiceRule(LiteralRule("^"), TermRule())))
                )).macro = lambda lang=None: makeRule(TermRule, lang)

    # }}}

    # {{{ block
    def block_macro(kind=None, x=None):
        if kind == "lang":
            return makeRule(BlockRule, x)
        elif kind == "rule":
            return makeRule(BlockRule, None, x)
        else:
            return makeRule(BlockRule)

    lang.newOp('block', 200,
               (None,
                Opt(
                    ChoiceRule(
                        SequenceRule(TrivialRule("lang"), LiteralRule("@"),
                                     ChoiceRule(LiteralRule("^"), TermRule())),
                        SequenceRule(TrivialRule("rule"), LiteralRule(":"),
                                     ExpressionRule()))))).macro = block_macro
    # }}}

    # {{{ symbol
    lang.newOp('symbol', 200,
               (None, None)).macro = lambda: makeRule(SymbolRule)
    # }}}

    # {{{ eol
    lang.newOp('eol', 0, (None, None)).macro = lambda: makeRule(EolRule)

    # }}}

    # {{{ debug
    def debug_macro(valx=None):
        if isinstance(valx, Symbol):
            valx2 = str(valx)
        else:
            valx2 = valx
        return makeRule(DebugRule, valx2)

    lang.newOp('debug', 0,
               (None,
                SequenceRule(LiteralRule("("), Opt(ExpressionRule()),
                             LiteralRule(")")))).macro = debug_macro
    # }}}

    # {{{ +
    lang.newOp(
        '+', 110,
        (ExpressionRule(), None)).macro = lambda rule: makeRule(Rep1, rule)
    # }}}

    # {{{ *
    lang.newOp(
        '*', 110,
        (ExpressionRule(), None)).macro = lambda rule: makeRule(Rep, rule)

    # }}}

    # {{{ $
    def dollar_macro(namex, rulex):
        if namex == data.none:
            n = None
        elif isinstance(namex, Symbol):
            n = str(namex)
        else:
            n = namex

        optOp = Symbol(syntaxlang_ns, "[")
        if isDoc(rulex, optOp) and rulex.contentLen() == 1:
            # The rule being named is optional ([...]) with no alternative.
            # Set the alternative to 'omit' (because it's named)
            return makeRule(NamedRule, n, op(optOp, rulex[0], '-'))
        else:
            return makeRule(NamedRule, n, rulex)

    lang.newOp('$', 120, (None,
                          SequenceRule(Opt(TermRule(), None), LiteralRule(':'),
                                       ExpressionRule()))).macro = dollar_macro
    # }}}

    # {{{ |
    lang.newOp(
        '|', 90,
        (ExpressionRule(),
         SequenceRule(ExpressionRule(),
                      Rep(SequenceRule(LiteralRule("|"), ExpressionRule())))
         )).macro = lambda *choices: makeRule(ChoiceRule, *choices)
    # }}}

    # {{{ (
    lang.newOp('(', 0, (None, SequenceRule(
        ExpressionRule(), LiteralRule(')')))).macro = lambda rulex: rulex

    # }}}

    # {{{ [
    def opt_macro(rulex, altx=None):
        if altx == '-':
            return makeRule(Opt, rulex)
        else:
            if isinstance(altx, Symbol):
                altx = str(altx)
            return makeRule(Opt, rulex, altx)

    lang.newOp('[', 200,
               (None,
                Seq(
                    ExpressionRule(), LiteralRule(']'),
                    Opt(
                        Seq(LiteralRule("/"),
                            ChoiceRule(LiteralRule("-"),
                                       ExpressionRule())))))).macro = opt_macro

    # }}}

    # {{{ <
    def trivial_macro(resultx=None):
        if isinstance(resultx, Symbol):
            resultx = str(resultx)

        if resultx is None:
            return makeRule(TrivialRule)
        else:
            return makeRule(TrivialRule, resultx)

    lang.newOp('<', 0,
               (None, SequenceRule(Opt(ExpressionRule()),
                                   LiteralRule('>')))).macro = trivial_macro
    # }}}

    # {{{ freetext
    lang.newOp(
        'freetext', 200,
        (None,
         SequenceRule(
             Opt(LiteralRule("upto"), False), LiteralRule('/'),
             FreetextRule(r"[^/\\]*(?:\\.[^/\\]*)*", upto=False),
             LiteralRule('/')))).macro = lambda upto, terminator: makeRule(
                 FreetextRule, terminator, bool(upto))
    # }}}

    # {{{ freetoken
    lang.newOp(
        'freetoken', 200,
        (None,
         SequenceRule(LiteralRule('/'),
                      FreetextRule(r"[^/\\]*(?:\\.[^/\\]*)*", upto=False),
                      LiteralRule('/'))
         )).macro = lambda terminator: makeRule(FreetokenRule, terminator)
    # }}}

    # {{{ optext
    lang.newOp('optext', 200,
               (None,
                SequenceRule(
                    NamedRule("lang", Opt(SymbolRule(), None)),
                    NamedRule(
                        "ops",
                        Rep(
                            SequenceRule(
                                LiteralRule('"'),
                                FreetextRule(r'[^\\"]*(?:\\.[^\\"]*)*',
                                             upto=False), LiteralRule('"')))),
                    Opt(LiteralRule("oneline"), False), LiteralRule('/'),
                    FreetextRule(r"[^/\\]*(?:\\.[^/\\]*)*", upto=False),
                    LiteralRule('/'))
                )).macro = lambda oneline, terminator, lang, ops: makeRule(
                    OptextRule, lang,
                    Doc(listOp, [eval('"%s"' % op)
                                 for op in ops]), bool(oneline), terminator)
    # }}}

    # {{{ "symbol:"
    lang.newOp('symbol:', 110,
               (None, ExpressionRule()
                )).macro = lambda rulex: makeRule(ParsedNameRule, rulex)
    # }}}

    return ulang
Ejemplo n.º 44
0
class MainWindow:

    def delete(self, widget, event=None):
        Gtk.main_quit()
        return False

    def next_page(self, widget, notebook):
        page = self.notebook.get_current_page()
        if page == 0:
            self.lang.save_selection()
            Kbbox = Gtk.VBox(False, 0)
            Kbbox.show()
            self.kb = Keyboard(self.button3)
            get_kb = self.kb.get_model()
            Kbbox.pack_start(get_kb, True, True, 0)
            label = Gtk.Label("Keyboard")
            self.notebook.insert_page(Kbbox, label, 1)
            self.window.show_all()
            self.notebook.next_page()
            self.button1.set_sensitive(True)
            self.button3.set_sensitive(False)
        elif page == 1:
            self.kb.save_selection()
            Tbbox = Gtk.VBox(False, 0)
            Tbbox.show()
            self.tz = TimeZone(self.button3)
            get_tz = self.tz.get_model()
            Tbbox.pack_start(get_tz, True, True, 0)
            label = Gtk.Label("TimeZone")
            self.notebook.insert_page(Tbbox, label, 2)
            self.window.show_all()
            self.notebook.next_page()
            self.button3.set_sensitive(False)
        elif page == 2:
            self.tz.save_selection() 
            typebox = Gtk.VBox(False, 0)
            typebox.show()
            self.types = Types()
            get_types = self.types.get_model()
            typebox.pack_start(get_types, True, True, 0)
            label = Gtk.Label("Types")
            self.notebook.insert_page(typebox, label, 3)
            self.window.show_all()
            self.notebook.next_page()
        elif page == 3:
            if self.types.get_type() == "disk":
                Udbox = Gtk.VBox(False, 0)
                Udbox.show()
                self.partition = UFSDisk(self.button3)
                get_UD = self.partition.get_model()
                Udbox.pack_start(get_UD, True, True, 0)
                label = Gtk.Label("UFS Disk Configuration")
                self.notebook.insert_page(Udbox, label, 4)
                self.window.show_all()
                self.notebook.next_page()
                self.button3.set_sensitive(False)
            elif self.types.get_type() == "custom":
                Pbox = Gtk.VBox(False, 0)
                Pbox.show()
                self.partition = Partitions(self.button3)
                get_part = self.partition.get_model()
                Pbox.pack_start(get_part, True, True, 0)
                label = Gtk.Label("UFS Custom Configuration")
                self.notebook.insert_page(Pbox, label, 4)
                self.window.show_all()
                self.notebook.next_page()
                self.button3.set_sensitive(False)
            elif self.types.get_type() == "zfs":
                Zbox = Gtk.VBox(False, 0)
                Zbox.show()
                self.partition = ZFS(self.button3)
                get_ZFS = self.partition.get_model()
                Zbox.pack_start(get_ZFS, True, True, 0)
                label = Gtk.Label("ZFS Configuration")
                self.notebook.insert_page(Zbox, label, 4)
                self.window.show_all()
                self.notebook.next_page()
                self.button3.set_sensitive(False)
        elif page == 4:
            self.partition.save_selection()
            Rbox = Gtk.VBox(False, 0)
            Rbox.show()
            self.rootuser = RootUser(self.button3)
            get_root = self.rootuser.get_model()
            Rbox.pack_start(get_root, True, True, 0)
            label = Gtk.Label("Root Password")
            self.notebook.insert_page(Rbox, label, 5)
            self.window.show_all()
            self.notebook.next_page()
            self.button3.set_sensitive(False)
        elif page == 5:
            self.rootuser.save_selection()
            Abox = Gtk.VBox(False, 0)
            Abox.show()
            self.adduser = AddUser(self.button3)
            get_adduser = self.adduser.get_model()
            Abox.pack_start(get_adduser, True, True, 0)
            label = Gtk.Label("Adding User")
            self.notebook.insert_page(Abox, label, 6)
            self.button3.set_label("Install")
            self.window.show_all()
            self.notebook.next_page()
            self.button3.set_sensitive(False)
        elif page == 6:
            self.adduser.save_selection()
            Ibox = Gtk.VBox(False, 0)
            Ibox.show()
            install = installSlide()
            get_install = install.get_model()
            Ibox.pack_start(get_install, True, True, 0)
            label = Gtk.Label("Installation")
            self.notebook.insert_page(Ibox, label, 7)
            self.notebook.next_page()
            instpro = installProgress()
            progressBar = instpro.getProgressBar()
            box1 = Gtk.VBox(False, 0)
            box1.show()
            label = Gtk.Label("Progress Bar")
            box1.pack_end(progressBar, False, False, 0)
            self.nbButton.insert_page(box1, label, 1)
            self.nbButton.next_page()
            self.window.show_all()

    def back_page(self, widget):
        page = self.notebook.get_current_page()
        if page == 1:
            self.button1.set_sensitive(False)
        elif page == 6:
            print page
            self.button3.set_label("Next")
        self.notebook.prev_page()
        self.button3.set_sensitive(True)

    def __init__(self):
        self.window = Gtk.Window()
        self.window.connect("delete_event", self.delete)
        self.window.set_border_width(0)
        self.window.set_default_size(700, 500)
        self.window.set_size_request(700, 500)
        self.window.set_title("GhostBSD Installer")
        self.window.set_border_width(0)
        self.window.set_icon_from_file(logo)
        mainHBox = Gtk.HBox(False, 0)
        mainHBox.show()
        mainVbox = Gtk.VBox(False, 0)
        mainVbox.show()
        self.window.add(mainHBox)
        mainHBox.pack_start(mainVbox, True, True, 0)
        # Create a new self.notebook
        self.notebook = Gtk.Notebook()
        mainVbox.pack_start(self.notebook, True, True, 0)
        self.notebook.show()
        self.notebook.set_show_tabs(False)
        self.notebook.set_show_border(False)
        vbox = Gtk.VBox(False, 0)
        vbox.show()
        self.lang = Language()
        get_lang = self.lang.get_model()
        # self.lang = Installs()
        # get_lang = self.lang.get_model()
        vbox.pack_start(get_lang, True, True, 0)
        label = Gtk.Label("Language")
        self.notebook.insert_page(vbox, label, 0)

        # Set what page to start at Language
        self.notebook.set_current_page(0)

        # Create buttons
        self.table = Gtk.Table(1, 6, True)

        self.button1 = Gtk.Button(label='Back')
        self.button1.connect("clicked", self.back_page)
        self.table.attach(self.button1, 3, 4, 0, 1)
        self.button1.show()
        self.button1.set_sensitive(False)

        self.button2 = Gtk.Button(label='Cancel')
        self.button2.connect("clicked", self.delete)
        self.table.attach(self.button2, 4, 5, 0, 1)
        self.button2.show()

        self.button3 = Gtk.Button(label='Next')
        self.button3.connect("clicked", self.next_page, self.notebook)
        self.table.attach(self.button3, 5, 6, 0, 1)
        self.button3.show()

        self.table.set_col_spacings(5)
        self.table.show()
        # Create a new notebook
        self.nbButton = Gtk.Notebook()
        mainVbox.pack_end(self.nbButton, False, False, 5)
        self.nbButton.show()
        self.nbButton.set_show_tabs(False)
        self.nbButton.set_show_border(False)
        label = Gtk.Label("Button")
        self.nbButton.insert_page(self.table, label, 0)
        self.window.show_all()
Ejemplo n.º 45
0
def main():

    cache = Cache()

    for user in config.users:

        # Check that all information about the user is present
        try:
            name = user["name"]
            language = user["language"]
            email = user["email"]
            modules = user["modules"]
        except:
            print("Some keys missing")
            continue

        lang = Language(language)
        if lang.dictionary == {}:
            continue

        header = lang.getHeader(name)
        body = ""
        footer = lang.getFooter()

        # Date
        if "date" in modules:
            date = info.get_date()
            week = info.get_week()
            body += lang.getDate(str(date), str(week))

        # Birthdays
        if "birthdays" in modules:
            birthdays = info.get_birthdays(user["birthdayDBA"])
            birthday_string = lang.getBirthdays(birthdays)
            if birthday_string != "":
                body += birthday_string + "\n"

        # SOL information
        if "sol" in modules:
            try:
                places = user["sol"]
                sol_info = info.get_sol_info(places, cache)
                sol_string = lang.getSol(sol_info)
                if sol_string != "":
                    body += sol_string + "\n"
            except:
                print("Error with the SOL module")

        # Temperature information
        if "temperature" in modules:
            try:
                places = user["temperature"]
                temperature_info = info.get_temperatures(places, cache)
                temperature_string = lang.getTemperature(temperature_info)
                if temperature_string != "":
                    body += temperature_string + "\n"
            except:
                print("Error with the temperature")

        # Aurora information
        if "aurora" in modules:
            values = info.get_aurora_info(cache)
            body += lang.getAurora(values)

        # IP information
        if "ip" in modules:
            current_ip = info.getip()
            body += lang.getIp(current_ip)

        # Exchange information
        if "exchange" in modules:
            try:
                exchanges = user["exchange"]
                exchange_values = info.get_exchange_values(exchanges)
                exchange_string = lang.getExchange(exchange_values)
                body += exchange_string + "\n"
            except:
                print("Error with the exchange module")

        text = header + body + footer

        # Email
        filelist = [f for f in os.listdir(config.application_path) if f.endswith(".png")]
        mail.send_mail(email, lang.getSubject(info.get_date()), text, filelist)

        # Remove the graphs created in the exchange module
        if "exchange" in modules:
            for f in filelist:
                try:
                    os.remove(f)
                except:
                    pass
Ejemplo n.º 46
0
    def __init__(self):
        self.ttimer = 100
        conf = Conf()
        self.currentpath = op_folder
        Language(conf)

        self.list_iter = []

        titleadd = ''
        if len(sys.argv) > 0:
            if sys.argv[1] == '10112':
                titleadd = _('NMEA 0183 input diagnostic')
            elif sys.argv[1] == '10113':
                titleadd = _('NMEA 0183 output diagnostic')

        wx.Frame.__init__(self, None, title=titleadd, size=(650, 435))
        self.Bind(wx.EVT_CLOSE, self.OnClose)
        panel = wx.Panel(self, wx.ID_ANY)

        self.timer = wx.Timer(self)
        self.Bind(wx.EVT_TIMER, self.timer_act, self.timer)

        self.SetFont(
            wx.Font(10, wx.FONTFAMILY_DEFAULT, wx.FONTSTYLE_NORMAL,
                    wx.FONTWEIGHT_NORMAL))

        self.icon = wx.Icon(self.currentpath + '/static/icons/kplex.ico',
                            wx.BITMAP_TYPE_ICO)
        self.SetIcon(self.icon)

        self.logger = wx.TextCtrl(panel,
                                  style=wx.TE_MULTILINE | wx.TE_READONLY
                                  | wx.TE_DONTWRAP | wx.LC_SORT_ASCENDING)

        self.list = wx.ListCtrl(panel,
                                -1,
                                style=wx.LC_REPORT | wx.SUNKEN_BORDER)
        self.list.InsertColumn(0, _('Device'), width=70)
        self.list.InsertColumn(1, _('Type'), width=50)
        self.list.InsertColumn(2,
                               _('Interval'),
                               wx.LIST_FORMAT_RIGHT,
                               width=70)
        self.list.InsertColumn(3, _('Data'), width=430)

        self.button_pause = wx.Button(panel, label=_('Pause'), pos=(555, 160))
        self.button_pause.Bind(wx.EVT_BUTTON, self.pause)

        sort = wx.Button(panel, label=_('Sort'), pos=(555, 200))
        sort.Bind(wx.EVT_BUTTON, self.sort)

        nmea = wx.Button(panel, label=_('NMEA info'), pos=(555, 240))
        nmea.Bind(wx.EVT_BUTTON, self.nmea_info)

        self.pause_all = 0

        htextbox = wx.BoxSizer(wx.HORIZONTAL)
        htextbox.Add(self.logger, 1, wx.ALL | wx.EXPAND, 5)

        hlistbox = wx.BoxSizer(wx.HORIZONTAL)
        hlistbox.Add(self.list, 1, wx.ALL | wx.EXPAND, 5)

        hbox = wx.BoxSizer(wx.HORIZONTAL)
        hbox.Add(self.button_pause, 0, wx.RIGHT | wx.LEFT, 5)
        hbox.Add(sort, 0, wx.RIGHT | wx.LEFT, 5)
        hbox.Add(nmea, 0, wx.RIGHT | wx.LEFT, 5)

        vbox = wx.BoxSizer(wx.VERTICAL)
        vbox.Add(htextbox, 1, wx.ALL | wx.EXPAND, 0)
        vbox.Add(hlistbox, 1, wx.ALL | wx.EXPAND, 0)
        vbox.Add(hbox, 0, wx.ALL | wx.EXPAND, 0)
        panel.SetSizer(vbox)

        self.CreateStatusBar()
        self.Centre()

        self.s2 = ''
        self.status = ''
        self.data = []
        self.baudc = 0
        self.baud = 0

        self.timer.Start(self.ttimer)
Ejemplo n.º 47
0
    def __init__(self):
        self.window = Gtk.Window()
        self.window.connect("delete_event", self.delete)
        self.window.set_border_width(0)
        self.window.set_default_size(700, 500)
        self.window.set_size_request(700, 500)
        self.window.set_title("GhostBSD Installer")
        self.window.set_border_width(0)
        self.window.set_icon_from_file(logo)
        mainHBox = Gtk.HBox(False, 0)
        mainHBox.show()
        mainVbox = Gtk.VBox(False, 0)
        mainVbox.show()
        self.window.add(mainHBox)
        mainHBox.pack_start(mainVbox, True, True, 0)
        # Create a new self.notebook
        self.notebook = Gtk.Notebook()
        mainVbox.pack_start(self.notebook, True, True, 0)
        self.notebook.show()
        self.notebook.set_show_tabs(False)
        self.notebook.set_show_border(False)
        vbox = Gtk.VBox(False, 0)
        vbox.show()
        self.lang = Language()
        get_lang = self.lang.get_model()
        # self.lang = Installs()
        # get_lang = self.lang.get_model()
        vbox.pack_start(get_lang, True, True, 0)
        label = Gtk.Label("Language")
        self.notebook.insert_page(vbox, label, 0)

        # Set what page to start at Language
        self.notebook.set_current_page(0)

        # Create buttons
        self.table = Gtk.Table(1, 6, True)

        self.button1 = Gtk.Button(label='Back')
        self.button1.connect("clicked", self.back_page)
        self.table.attach(self.button1, 3, 4, 0, 1)
        self.button1.show()
        self.button1.set_sensitive(False)

        self.button2 = Gtk.Button(label='Cancel')
        self.button2.connect("clicked", self.delete)
        self.table.attach(self.button2, 4, 5, 0, 1)
        self.button2.show()

        self.button3 = Gtk.Button(label='Next')
        self.button3.connect("clicked", self.next_page, self.notebook)
        self.table.attach(self.button3, 5, 6, 0, 1)
        self.button3.show()

        self.table.set_col_spacings(5)
        self.table.show()
        # Create a new notebook
        self.nbButton = Gtk.Notebook()
        mainVbox.pack_end(self.nbButton, False, False, 5)
        self.nbButton.show()
        self.nbButton.set_show_tabs(False)
        self.nbButton.set_show_border(False)
        label = Gtk.Label("Button")
        self.nbButton.insert_page(self.table, label, 0)
        self.window.show_all()
Ejemplo n.º 48
0
class Mind(object):
    __instance = None

    def __init__(self, verbose=True, dbpath="../agentdata"):
        self.__lastError = None
        self.output = print
        self.__clock = 0
        self.verbose = verbose
        self.windows = []

        self.attention = Attention(self)
        self.language = Language(self)

        self.bodyWindow = BodyWindow.create(self)

        self.memory = Memory(self, dbpath)
        self.workingMemory = WorkingMemory(self)

        self.currentWindow = self.bodyWindow

        #TODO: a memory window!!!
        self.memoryWindow = MemoryWindow.create(self)
        self.attention.start()

    def shutdown(self):
        self.memory.shutdown()
        self.attention.completed = True
        for window in self.windows:
            window.completed = True
            print("stopped")

    def tick(self):
        self.__clock += 1

    def clock(self):
        return self.__clock

    @classmethod
    def singleton(cls, output):
        if cls.__instance == None:
            cls.__instance = Mind()
        cls.__instance.setBodyOutput(output)
        return cls.__instance

    def __del__(self):
        self.shutdown()

    def version(self):
        return "Cognitive Agent Language For Mind Version 0.1\nTime + Space + Intelligence is at your service...."

    def prompt(self):
        return ">>>>"

    def say(self, message):
        #        self.log(message)
        self.output("log", message)

    def log(self, message):
        if self.verbose:
            print(message)

    def error(self, errorType, command):
        traceback.print_exc()
        self.__lastError = str(errorType) + ":" + str(command)
        self.log("ERROR: " + self.__lastError)
        return False

    def lastError(self):
        return self.__lastError

    def hasError(self):
        return (self.__lastError is not None)

    def clearError(self):
        self.__lastError = None

    def setBodyOutput(self, bodyOutput):
        self.output = bodyOutput

    def listen(self, message):
        return self.language.interpret(message)
Ejemplo n.º 49
0
    def __init__(self):
        self.conf = Conf()
        self.home = self.conf.home
        self.op_folder = self.conf.get('GENERAL', 'op_folder')
        self.currentpath = self.op_folder
        self.help_bmp = wx.Bitmap(
            self.op_folder + "/static/icons/help-browser.png",
            wx.BITMAP_TYPE_ANY)
        Language(self.conf)
        self.SK_settings = SK_settings(self.conf)

        self.available_operators = [
            'eq', 'neq', 'lt', 'lte', 'gt', 'gte', 'btwn', 'cont', 'true',
            'false', 'null', 'nnull', 'empty', 'nempty'
        ]
        self.available_conditions = [
            '=', '!=', '<', '<=', '>', '>=',
            _('is between'),
            _('contains'),
            _('is true'), ('is false'),
            _('is null'),
            _('is not null'),
            _('is empty'),
            _('is not empty')
        ]

        self.available_source = [
            _('label'),
            _('type'),
            _('pgn'),
            _('src'),
            _('sentence'),
            _('talker')
        ]
        self.available_source_nr = [
            'label', 'type', 'pgn', 'src', 'sentence', 'talker'
        ]

        wx.Frame.__init__(self,
                          None,
                          title=_('SignalK input filter (uses node-red)'),
                          size=(710, 460))

        self.SetFont(
            wx.Font(10, wx.FONTFAMILY_DEFAULT, wx.FONTSTYLE_NORMAL,
                    wx.FONTWEIGHT_NORMAL))

        self.icon = wx.Icon(self.op_folder + '/static/icons/openplotter.ico',
                            wx.BITMAP_TYPE_ICO)
        self.SetIcon(self.icon)

        self.list_triggers = wx.ListCtrl(self,
                                         -1,
                                         style=wx.LC_REPORT | wx.SUNKEN_BORDER)
        self.list_triggers.InsertColumn(0, _('Signal K key'), width=240)
        self.list_triggers.InsertColumn(1, _('Source Type'), width=120)
        self.list_triggers.InsertColumn(2, _('Condition'), width=70)
        self.list_triggers.InsertColumn(3, _('Value'), width=90)
        self.list_triggers.InsertColumn(4, _('Value2'), width=60)

        self.list_triggers.Bind(wx.EVT_LIST_ITEM_SELECTED,
                                self.on_select_triggers)
        self.list_triggers.Bind(wx.EVT_LIST_ITEM_DESELECTED,
                                self.on_deselected_triggers)
        self.list_triggers.Bind(wx.EVT_LIST_ITEM_ACTIVATED,
                                self.on_edit_triggers)

        add_trigger = wx.Button(self, label=_('add'))
        add_trigger.Bind(wx.EVT_BUTTON, self.on_add_trigger)

        delete_trigger = wx.Button(self, label=_('delete'))
        delete_trigger.Bind(wx.EVT_BUTTON, self.on_delete_trigger)

        diagnostic = wx.Button(self, label=_('SK Diagnostic'))
        diagnostic.Bind(wx.EVT_BUTTON, self.on_diagnostic_SK)

        reset_skf = wx.Button(self, label=_('Restart'))
        reset_skf.Bind(wx.EVT_BUTTON, self.reset_sensors)

        help_button = wx.BitmapButton(self,
                                      bitmap=self.help_bmp,
                                      size=(self.help_bmp.GetWidth() + 40,
                                            self.help_bmp.GetHeight() + 10))
        help_button.Bind(wx.EVT_BUTTON, self.on_help_filter)

        apply_changes = wx.Button(self, label=_('Apply changes'))
        apply_changes.Bind(wx.EVT_BUTTON, self.on_apply_changes_triggers)
        cancel_changes = wx.Button(self, label=_('Cancel changes'))
        cancel_changes.Bind(wx.EVT_BUTTON, self.on_cancel_changes_triggers)

        hlistbox_but = wx.BoxSizer(wx.VERTICAL)
        hlistbox_but.Add(add_trigger, 0, wx.ALL, 5)
        hlistbox_but.Add(delete_trigger, 0, wx.ALL, 5)

        hlistbox = wx.BoxSizer(wx.HORIZONTAL)
        hlistbox.Add(self.list_triggers, 1, wx.ALL | wx.EXPAND, 5)
        hlistbox.Add(hlistbox_but, 0, wx.RIGHT | wx.LEFT, 0)

        hbox = wx.BoxSizer(wx.HORIZONTAL)
        hbox.Add(help_button, 0, wx.ALL, 0)
        hbox.Add(diagnostic, 0, wx.RIGHT | wx.LEFT, 5)
        hbox.Add(reset_skf, 0, wx.RIGHT | wx.LEFT, 5)
        hbox.AddStretchSpacer(1)
        hbox.Add(apply_changes, 0, wx.RIGHT | wx.LEFT, 5)
        hbox.Add(cancel_changes, 0, wx.RIGHT | wx.LEFT, 5)

        vbox = wx.BoxSizer(wx.VERTICAL)
        vbox.Add(hlistbox, 1, wx.ALL | wx.EXPAND, 0)
        vbox.Add(hbox, 0, wx.ALL | wx.EXPAND, 5)

        self.SetSizer(vbox)

        self.read_triggers()
Ejemplo n.º 50
0
class MatrixCocycle(object):
    r"""
    Matrix cocycle

    INPUT:

    - ``gens`` -- list, tuple or dict; the matrices. Keys 0,...,n-1 are
      used for list and tuple.
    - ``cone`` -- dict or matrix or None (default: None); the cone for each
      matrix generators.  If it is a matrix, then it serves as the cone for
      all matrices. The cone is defined by the columns of the matrix. If
      None, then the cone is the identity matrix.
    - ``language`` -- regular language or None (default: None); if None,
      the language is the full shift.

    EXAMPLES::

        sage: from slabbe.matrix_cocycle import MatrixCocycle
        sage: B1 = matrix(3, [1,0,0, 0,1,0, 0,1,1])
        sage: B2 = matrix(3, [1,0,0, 0,0,1, 0,1,1])
        sage: B3 = matrix(3, [0,1,0, 0,0,1, 1,0,1])
        sage: gens = {'1':B1, '2':B2, '3':B3}
        sage: cone = matrix(3, [1,1,1,0,1,1,0,0,1])
        sage: MatrixCocycle(gens, cone)
        Cocycle with 3 gens over Language of finite words over alphabet ['1', '2', '3']
    """
    def __init__(self, gens, cone=None, language=None):
        r"""
        EXAMPLES::

            sage: from slabbe.matrix_cocycle import MatrixCocycle
            sage: gens = {'A':matrix(3, [1,0,0, 0,1,0, 0,1,1])}
            sage: cone = identity_matrix(3)
            sage: MatrixCocycle(gens, cone)
            Cocycle with 1 gens over Language of finite words over alphabet ['A']
        """
        if isinstance(gens, dict):
            self._gens = gens
        elif isinstance(gens, (list, tuple)):
            self._gens = dict(enumerate(gens))
        else:
            raise ValueError("gens must be a list, tuple or a dict")
        if cone is None:
            ID = self.identity_matrix()
            self._cone_dict = {letter:ID for letter in self._gens.keys()}
        elif isinstance(cone, dict):
            self._cone_dict = cone
        else:
            self._cone_dict = {letter:cone for letter in self._gens.keys()}
        if language is None:
            self._language = Language(sorted(self._gens.keys()))
        else:
            self._language = language

    def __repr__(self):
        r"""
        EXAMPLES::

            sage: from slabbe.matrix_cocycle import MatrixCocycle
            sage: gens = {'A':matrix(3, [1,0,0, 0,1,0, 0,1,1])}
            sage: cone = identity_matrix(3)
            sage: MatrixCocycle(gens, cone)
            Cocycle with 1 gens over Language of finite words over alphabet ['A']
        """
        s = "Cocycle with {} gens over {}"
        return s.format(len(self._gens), self._language)

    def gens(self):
        return self._gens
    def gens_inverses(self):
        r"""
        Return a dictionary of the inverses of the generators.

        EXAMPLES::

            sage: from slabbe.matrix_cocycle import cocycles
            sage: coc = cocycles.Brun()
            sage: coc.gens_inverses().keys()
            [321, 132, 231, 213, 312, 123]
            sage: coc.gens_inverses().values()
            [
            [ 1 -1  0]  [ 1  0  0]  [ 1  0 -1]  [ 1  0  0]  [ 1  0  0]  [ 1  0  0]
            [ 0  1  0]  [ 0  1 -1]  [ 0  1  0]  [ 0  1  0]  [-1  1  0]  [ 0  1  0]
            [ 0  0  1], [ 0  0  1], [ 0  0  1], [-1  0  1], [ 0  0  1], [ 0 -1  1]
            ]

        If possible, the ring is the Integer ring::

            sage: coc = cocycles.Reverse()
            sage: coc.gens_inverses().values()
            [
            [ 1 -1 -1]  [ 1  0  0]  [ 1  0  0]  [-1/2  1/2  1/2]
            [ 0  1  0]  [-1  1 -1]  [ 0  1  0]  [ 1/2 -1/2  1/2]
            [ 0  0  1], [ 0  0  1], [-1 -1  1], [ 1/2  1/2 -1/2]
            ]
            sage: [m.parent() for m in _]
            [Full MatrixSpace of 3 by 3 dense matrices over Integer Ring,
             Full MatrixSpace of 3 by 3 dense matrices over Integer Ring,
             Full MatrixSpace of 3 by 3 dense matrices over Integer Ring,
             Full MatrixSpace of 3 by 3 dense matrices over Rational Field]
        """
        from sage.rings.integer_ring import ZZ
        D = {}
        for k,v in self.gens().iteritems():
            M = v.inverse()
            try:
                M_ZZ = M.change_ring(ZZ)
            except TypeError:
                pass
            else:
                M = M_ZZ
            D[k] = M
        return D

    def cone_dict(self):
        return self._cone_dict
    def cone(self, key):
        return self._cone_dict[key]
    def language(self):
        return self._language

    @cached_method
    def identity_matrix(self):
        return self._gens.values()[0].parent().one()

    def word_to_matrix(self, w):
        r"""
        EXAMPLES::

            sage: from slabbe.matrix_cocycle import cocycles
            sage: C = cocycles.Sorted_ARP()
            sage: C.word_to_matrix(Word())
            [1 0 0]
            [0 1 0]
            [0 0 1]
        """
        return prod((self._gens[a] for a in w), z=self.identity_matrix())

    def n_words_iterator(self, n):
        r"""
        EXAMPLES::
            
            sage: from slabbe.matrix_cocycle import cocycles
            sage: ARP = cocycles.Sorted_ARP()
            sage: list(ARP.n_words_iterator(1))
            [word: A1, word: A2, word: A3, word: P1, word: P2, word: P3]
        """
        return self._language.words_of_length_iterator(n)

    def n_matrices_iterator(self, n):
        r"""
        EXAMPLES::

            sage: from slabbe.matrix_cocycle import cocycles
            sage: ARP = cocycles.Sorted_ARP()
            sage: A,B = zip(*list(ARP.n_matrices_iterator(1)))
            sage: A
            (word: A1, word: A2, word: A3, word: P1, word: P2, word: P3)
            sage: B
            (
            [1 0 0]  [1 0 0]  [0 1 0]  [0 1 0]  [0 0 1]  [0 0 1]
            [0 1 0]  [0 0 1]  [0 0 1]  [0 1 1]  [1 0 1]  [0 1 1]
            [1 1 1], [1 1 1], [1 1 1], [1 1 1], [1 1 1], [1 1 1]
            )
        """
        for w in self.n_words_iterator(n):
            yield w, self.word_to_matrix(w)

    def n_matrices_eigenvalues_iterator(self,n):
        r"""
        Return the eigenvalues of the matrices of level n.

        EXAMPLES::

            sage: from slabbe.matrix_cocycle import cocycles
            sage: ARP = cocycles.ARP()
            sage: list(ARP.n_matrices_eigenvalues_iterator(1))
            [(word: 1, [1, 1, 1]),
             (word: 2, [1, 1, 1]),
             (word: 3, [1, 1, 1]),
             (word: 123, [1, 1, 1]),
             (word: 132, [1, 1, 1]),
             (word: 213, [1, 1, 1]),
             (word: 231, [1, 1, 1]),
             (word: 312, [1, 1, 1]),
             (word: 321, [1, 1, 1])]

        ::

            sage: B = cocycles.Sorted_Brun()
            sage: list(B.n_matrices_eigenvalues_iterator(1))
            [(word: 1, [1, 1, 1]),
             (word: 2, [1, -0.618033988749895?, 1.618033988749895?]),
             (word: 3, [1.465571231876768?, 
                        -0.2327856159383841? - 0.7925519925154479?*I,
                        -0.2327856159383841? + 0.7925519925154479?*I])]
        """
        for w,m in self.n_matrices_iterator(n):
            yield w, m.eigenvalues()

    def n_matrices_pinching_iterator(self,n):
        r"""
        Return the pinching matrices of level n.

        EXAMPLES::

            sage: from slabbe.matrix_cocycle import cocycles
            sage: ARP = cocycles.ARP()
            sage: list(ARP.n_matrices_pinching_iterator(0))
            []
            sage: list(ARP.n_matrices_pinching_iterator(1))
            []
            sage: list(ARP.n_matrices_pinching_iterator(2))
            []
            sage: L = list(ARP.n_matrices_pinching_iterator(3))
            sage: L[0]
            (
                           [4 5 2]
                           [2 3 1]
            word: 1,2,213, [1 1 1]
            )
        """
        for w,m in self.n_matrices_iterator(n):
            p = m.charpoly()
            d = p.discriminant()
            if p.is_irreducible() and d > 0 and not d.is_square():
                yield w, m

    def n_matrices_eigenvectors(self,n, verbose=False):
        r"""
        Return the left and right eigenvectors of the matrices of level n.

        EXAMPLES::

            sage: from slabbe.matrix_cocycle import cocycles
            sage: C = cocycles.ARP()
            sage: C.n_matrices_eigenvectors(1)
            [(word: 1, (1.0, 0.0, 0.0), (0.0, 0.0, 1.0)),
             (word: 2, (0.0, 1.0, 0.0), (1.0, 0.0, 0.0)),
             (word: 3, (0.0, 0.0, 1.0), (1.0, 0.0, 0.0)),
             (word: 123, (0.0, 0.0, 1.0), (1.0, 0.0, 0.0)),
             (word: 132, (0.0, 1.0, 0.0), (1.0, 0.0, 0.0)),
             (word: 213, (0.0, 0.0, 1.0), (0.0, 1.0, 0.0)),
             (word: 231, (1.0, 0.0, 0.0), (0.0, 1.0, 0.0)),
             (word: 312, (0.0, 1.0, 0.0), (0.0, 0.0, 1.0)),
             (word: 321, (1.0, 0.0, 0.0), (0.0, 0.0, 1.0))]
        """
        R = []
        for w,m in self.n_matrices_iterator(n):
            try:
                a,v_right = perron_right_eigenvector(m)
                b,v_left = perron_right_eigenvector(m.transpose())
            except ValueError:
                print "problem with :\n",m
            else:
                R.append((w, v_right,v_left))
                if verbose:
                    print "indices of matrices:", w
                    print m
                    print "eigenvectors:", v_right, v_left
        return R
    @cached_method
    def n_matrices_non_pisot(self,n, verbose=False):
        r"""
        Return the list of non pisot matrices (as list of indices of base
        matrices).

        EXAMPLES::

            sage: from slabbe.matrix_cocycle import cocycles
            sage: ARP = cocycles.Sorted_ARP()
            sage: ARP.n_matrices_non_pisot(1)
            [word: A1, word: A2]
            sage: ARP.n_matrices_non_pisot(2)   # long time (1s)
            [word: A1,A1, word: A1,A2, word: A2,A1, word: A2,A2]
            sage: ARP.n_matrices_non_pisot(3)   # long time (11s)
            [word: A1,A1,A1,
             word: A1,A1,A2,
             word: A1,A2,A1,
             word: A1,A2,A2,
             word: A2,A1,A1,
             word: A2,A1,A2,
             word: A2,A2,A1,
             word: A2,A2,A2]
            sage: len(ARP.n_matrices_non_pisot(4))  # long time
            16

        ::

            sage: from slabbe.matrix_cocycle import cocycles
            sage: B = cocycles.Sorted_Brun()
            sage: B.n_matrices_non_pisot(2)
            [word: 11, word: 12, word: 21, word: 22]
            sage: B.n_matrices_non_pisot(3)
            [word: 111,
             word: 112,
             word: 121,
             word: 122,
             word: 211,
             word: 212,
             word: 221,
             word: 222]

        """
        return [w for w in self.n_words_iterator(n) if not self.is_pisot(w)]

    def n_matrices_semi_norm_iterator(self, n, p=2):
        r"""
        EXAMPLES:

        For the 1-norm, all matrices contracts the hyperplane::
            
            sage: from slabbe.matrix_cocycle import cocycles
            sage: C = cocycles.ARP()
            sage: it = C.n_matrices_semi_norm_iterator(1, p=1)
            sage: for _ in range(5): print next(it) # tolerance 0.0001
            (word: 1, 1.0, False)
            (word: 2, 1.0, False)
            (word: 3, 1.0, False)
            (word: 123, 0.9999885582839877, False)
            (word: 132, 0.9999854006354785, False)

        For the 2-norm, AR matrices do not contract::

            sage: it = C.n_matrices_semi_norm_iterator(1, p=2)
            sage: for w,s,b in it: print w,s,b   # long time (6s)
            A1 1.30656296488 False
            A2 1.30656296486 False
            A3 1.30656296475 False
            P12 0.99999999996 False
            P13 0.999999999967 False
            P21 0.999999999967 False
            P23 0.999999999997 False
            P31 0.999999999769 False
            P32 0.999999999839 False

        When, the 1-norm is < 1, the product is pisot::

            sage: it = C.n_matrices_semi_norm_iterator(2, p=1)
            sage: for w,s,b in it: print w,s,b   # long time
            A1,A1 1.0 False
            A1,A2 1.0 False
            A1,A3 1.0 False
            A1,P12 0.999998922557 False
            A1,P13 0.999997464905 False
            A1,P21 0.999993244882 False
            A1,P23 0.999999150973 True
            A1,P31 0.999994030522 False
            A1,P32 0.999998046513 True
            A2,A1 1.0 False
            A2,A2 1.0 False
            A2,A3 1.0 False
            A2,P12 0.99999375291 False
            A2,P13 0.999995591588 True
            ...
            P31,A3 0.999988326888 False
            P31,P12 0.749998931902 True
            P31,P23 0.799999157344 True
            P31,P32 0.749993104833 True
            P32,A1 0.999997170005 True
            P32,A3 0.99999420509 False
            P32,P13 0.666665046248 True
            P32,P21 0.666665629351 True
            P32,P31 0.666664488371 True
        """
        if n == 0:
            raise NotImplementedError
        for w,m in self.n_matrices_iterator(n):
            cone = m*self.cone(w[-1])
            yield w, semi_norm_cone(m.transpose(), cone, p=p), self.is_pisot(w)

    def n_matrices_distorsion_iterator(self, n, p=1):
        r"""
        Return the the distorsion of the n-cylinders.

        EXAMPLES::

            sage: from slabbe.matrix_cocycle import cocycles
            sage: T = cocycles.Sorted_ARP()
            sage: it =T.n_matrices_distorsion_iterator(1)
            sage: list(it)
            [(word: A1, 2),
             (word: A2, 2),
             (word: A3, 2),
             (word: P1, 3),
             (word: P2, 3),
             (word: P3, 3)]
        """
        for w,m in self.n_matrices_iterator(n):
            yield w, distorsion(m, p=p)

    def n_cylinders_iterator(self, n):
        r"""
        EXAMPLES::

            sage: from slabbe.matrix_cocycle import cocycles
            sage: C = cocycles.ARP()
            sage: it = C.n_cylinders_iterator(1)
            sage: for w,cyl in it: print "{}\n{}".format(w,cyl)
            1
            [1 1 1]
            [0 1 0]
            [0 0 1]
            2
            [1 0 0]
            [1 1 1]
            [0 0 1]
            3
            [1 0 0]
            [0 1 0]
            [1 1 1]
            123
            [1 0 1]
            [1 1 1]
            [1 1 2]
            132
            [1 1 0]
            [1 2 1]
            [1 1 1]
            213
            [1 1 1]
            [0 1 1]
            [1 1 2]
            231
            [2 1 1]
            [1 1 0]
            [1 1 1]
            312
            [1 1 1]
            [1 2 1]
            [0 1 1]
            321
            [2 1 1]
            [1 1 1]
            [1 0 1]
        """
        if n == 0:
            raise NotImplementedError
        for w,m in self.n_matrices_iterator(n):
            yield w, m*self.cone(w[-1])

    def n_cylinders_edges(self, n):
        r"""
        Return the set of edges of the n-cylinders.

        EXAMPLES::

            sage: from slabbe.matrix_cocycle import cocycles
            sage: ARP = cocycles.ARP()
            sage: ARP.n_cylinders_edges(1)
            {frozenset({(1, 1, 0), (1, 1, 1)}),
             frozenset({(0, 1, 0), (1, 1, 0)}),
             frozenset({(1, 1, 1), (2, 1, 1)}),
             frozenset({(0, 0, 1), (1, 0, 1)}),
             frozenset({(0, 1, 0), (0, 1, 1)}),
             frozenset({(0, 1, 1), (1, 0, 1)}),
             frozenset({(1, 0, 0), (1, 1, 0)}),
             frozenset({(1, 1, 0), (2, 1, 1)}),
             frozenset({(1, 0, 1), (1, 1, 2)}),
             frozenset({(1, 1, 0), (1, 2, 1)}),
             frozenset({(1, 0, 1), (2, 1, 1)}),
             frozenset({(0, 0, 1), (0, 1, 1)}),
             frozenset({(1, 0, 1), (1, 1, 1)}),
             frozenset({(0, 1, 1), (1, 2, 1)}),
             frozenset({(0, 1, 1), (1, 1, 2)}),
             frozenset({(1, 0, 0), (1, 0, 1)}),
             frozenset({(1, 1, 1), (1, 2, 1)}),
             frozenset({(1, 0, 1), (1, 1, 0)}),
             frozenset({(0, 1, 1), (1, 1, 1)}),
             frozenset({(0, 1, 1), (1, 1, 0)}),
             frozenset({(1, 1, 1), (1, 1, 2)})}
        """
        from sage.rings.finite_rings.integer_mod_ring import Integers
        edges = set()
        for w,cyl in self.n_cylinders_iterator(n):
            cols = cyl.columns()
            indices = Integers(len(cols))
            edges.update(frozenset((cols[i], cols[i+1])) for i in indices)
        return edges

    def is_pisot(self, w):
        r"""
        """
        m = self.word_to_matrix(w)
        S = sorted((abs(e) for e in m.eigenvalues()), reverse=True)
        return S[0] > 1 and S[1] < 1

    def non_pisot_automaton(self, n):
        r"""
        EXAMPLES::

            sage: from slabbe.matrix_cocycle import cocycles
            sage: C = cocycles.ARP()
            sage: A = C.non_pisot_automaton(2)
            sage: A
            Automaton with 2 states
            sage: A.graph().plot(edge_labels=True)   # not tested
        """
        L = []
        for i in range(n):
            L.extend(self.n_matrices_non_pisot(i))
        alphabet = self._language._alphabet
        F = FiniteLanguage(alphabet, L)
        A = F.minimal_automaton()  
        return A
        #G = A.graph()
        #to_remove = set(A.states()) - set(A.final_states())
        #G.delete_vertices(to_remove)
        #return G

    def distorsion_max(self, n, p=1):
        r"""
        EXAMPLES:

        Non borné::

            sage: from slabbe.matrix_cocycle import cocycles
            sage: T = cocycles.Sorted_ARP()
            sage: T.distorsion_max(1, p=oo)
            1
            sage: T.distorsion_max(2, p=oo)
            3
            sage: T.distorsion_max(3, p=oo)
            5
            sage: T.distorsion_max(4, p=oo)
            7
        """
        return max(d for (w,d) in self.n_matrices_distorsion_iterator(n, p=p))

    def distorsion_argmax(self, n, p=1):
        r"""
        EXAMPLES::

            sage: from slabbe.matrix_cocycle import cocycles
            sage: ARP = cocycles.Sorted_ARP()
            sage: ARP.distorsion_argmax(1)
            (
                      [1 0 0]
                      [1 1 0]
            word: A1, [3 2 1]
            )
        """
        it = self.n_cylinders_iterator(n)
        key = lambda (w,m):distorsion(m, p=p)
        return max(it, key=key)

    def plot_n_cylinders(self, n, labels=True):
        r"""
        EXAMPLES::

            sage: from slabbe.matrix_cocycle import cocycles
            sage: C = cocycles.Sorted_ARP()
            sage: G = C.plot_n_cylinders(3)
        """
        from sage.plot.graphics import Graphics
        from sage.plot.polygon import polygon
        from sage.plot.text import text
        from matrices import M3to2
        G = Graphics()
        for w,cyl in self.n_cylinders_iterator(n):
            columns = cyl.columns()
            G += polygon((M3to2*col/col.norm(1) for col in columns), fill=False) 
            if labels:
                sum_cols = sum(columns)
                G += text("{}".format(w), M3to2*sum_cols/sum_cols.norm(1))
        return G

    def plot_n_matrices_eigenvectors(self, n, side='right', color_index=0, draw_line=False):
        r"""
        INPUT:

        - ``n`` -- integer, length
        - ``side`` -- ``'left'`` or ``'right'``, drawing left or right
          eigenvectors
        - ``color_index`` -- 0 for first letter, -1 for last letter
        - ``draw_line`` -- boolean

        EXAMPLES::

            sage: from slabbe.matrix_cocycle import cocycles
            sage: ARP = cocycles.ARP()
            sage: G = ARP.plot_n_matrices_eigenvectors(2)
        """
        from sage.plot.graphics import Graphics
        from sage.plot.point import point
        from sage.plot.line import line
        from sage.plot.text import text
        from sage.plot.colors import hue
        from sage.modules.free_module_element import vector
        from matrices import M3to2
        R = self.n_matrices_eigenvectors(n)
        L = [(w, M3to2*(a/sum(a)), M3to2*(b/sum(b))) for (w,a,b) in R]
        G = Graphics()
        alphabet = self._language._alphabet
        color_ = dict( (letter, hue(i/float(len(alphabet)))) for i,letter in
                enumerate(alphabet))
        for letter in alphabet:
            L_filtered = [(w,p1,p2) for (w,p1,p2) in L if w[color_index] == letter]
            words,rights,lefts = zip(*L_filtered)
            if side == 'right':
                G += point(rights, color=color_[letter], legend_label=letter)
            elif side == 'left':
                G += point(lefts,  color=color_[letter], legend_label=letter)
            else:
                raise ValueError("side(=%s) should be left or right" % side)

        if draw_line:
            for (a,b) in L:
                G += line([a,b], color='black', linestyle=":")
        G += line([M3to2*vector(a) for a in [(1,0,0), (0,1,0), (0,0,1), (1,0,0)]]) 
        title = "%s eigenvectors, colored by letter w[%s] of cylinder w" % (side, color_index)
        G += text(title, (0.5, 1.05), axis_coords=True)
        G.axes(False)
        return G

    def plot_pisot_conjugates(self, n):
        r"""
        EXAMPLES::

            sage: from slabbe.matrix_cocycle import cocycles
            sage: B = cocycles.Sorted_Brun()
            sage: G = B.plot_pisot_conjugates(5)   # long time (8s)

        Image envoyee a Timo (6 mai 2014)::

            sage: G = sum(B.plot_pisot_conjugates(i) for i in [1..6])  #not tested
        """
        from sage.plot.point import points
        Lreal = []
        Limag = []
        for w,s in self.n_matrices_eigenvalues_iterator(n):
            a,b,c = sorted(s, key=abs)
            if a.imag() == 0 and b.imag() == 0:
                Lreal.append((a,b))
            else:
                Limag.append((a.real(),a.imag()))
                Limag.append((b.real(),b.imag()))
        return points(Lreal) + points(Limag, color='red')

    def tikz_n_cylinders(self, n, labels=None, scale=1):
        r"""
        INPUT:

        - ``labels`` -- None, True or False (default: None), if None, it
          takes value True if n is 1.

        EXAMPLES::

            sage: from slabbe.matrix_cocycle import cocycles
            sage: ARP = cocycles.ARP()
            sage: t = ARP.tikz_n_cylinders(1, labels=True, scale=4)
            sage: t
            \documentclass[tikz]{standalone}
            \usepackage{amsmath}
            \begin{document}
            \begin{tikzpicture}
            [scale=4]
            \draw (0.0000, -0.5000) -- (0.0000, 0.0000);
            \draw (0.0000, -0.5000) -- (0.8660, -0.5000);
            \draw (0.0000, 0.0000) -- (-0.2165, -0.1250);
            ...
            ... 23 lines not printed (1317 characters in total) ...
            ...
            \node at (-0.1443, 0.1667) {$213$};
            \node at (-0.2165, 0.0417) {$231$};
            \node at (0.0722, -0.2083) {$312$};
            \node at (-0.0722, -0.2083) {$321$};
            \end{tikzpicture}
            \end{document}

        ::

            sage: from sage.misc.temporary_file import tmp_filename
            sage: filename = tmp_filename('temp','.pdf')
            sage: _ = t.pdf(filename)
        """
        if labels is None:
            labels = True if n == 1 else False
        lines = []
        lines.append(r"\begin{tikzpicture}")
        lines.append("[scale={}]".format(scale))
        from matrices import M3to2
        for (u,v) in self.n_cylinders_edges(n):
            u = rounded_string_vector(M3to2 * u / u.norm(1), digits=4)
            v = rounded_string_vector(M3to2 * v / v.norm(1), digits=4)
            lines.append(r"\draw {} -- {};".format(u,v))
        if labels:
            for w,cyl in self.n_cylinders_iterator(n):
                u = sum(c / c.norm(1) for c in cyl.columns())
                u = rounded_string_vector(M3to2 * u / u.norm(1), digits=4)
                lines.append(r"\node at {} {{${}$}};".format(u, w))
        lines.append(r"\end{tikzpicture}")
        from slabbe import TikzPicture
        return TikzPicture("\n".join(lines), use_sage_preamble=False)
Ejemplo n.º 51
0
 def set(self, name: str, language: str) -> None:
     lang: Language = Language(name)
     lang.set_language(language)
Ejemplo n.º 52
0
# Run the experiment
for nn in range(n):

    if not mute:
        # Report every n/100 times for boredom reasons
        if nn % 100 == 0:
            print(nn, 'iterations run...', file=sys.stderr)

    # Initialize families
    families = []

    for ff in range(f):
        # Generate random language to use as root for family
        randomname = 1000 * ff
        randomroot = Language(constraints, name=randomname)
        randomroot.randomize_ranking()
        families.append(Family(randomroot))

    # Evolve family
    for family in families:
        family.evolve(l, p)
    languages = []
    for family in families:
        languages += family.get_leaves()
    languagenames = []
    for language in languages:
        languagenames.append(language.__name__)
    languagenames.sort()
    # Output gold tree as a .dot file; then call dot -T png -o [tree].png [tree].dot
    #	family.tree_to_dot()
Ejemplo n.º 53
0
from .unit import *
from .time import *
from .color import *
from .weight import *
from .volume import *
from .density import *
from .extract import *
from .pressure import *
from .proportion import *
from .bitterness import *
from .temperature import *

from language import Language

Language.load( "units.ini" )

def _load_units( cls ):
	"""Recursively loads all subclasses of Unit into *.units class variables."""
	
	for unit_cls in cls.__subclasses__():
		_load_units( unit_cls )
		
		# If the class is an Unit
		if "unit" in unit_cls.__dict__:
			cls.units[unit_cls.unit] = unit_cls
			
			for multiple in unit_cls.multiples:
				cls.units[multiple] = unit_cls
		
		# If the class is an Unit-container it is merged with its parent
Ejemplo n.º 54
0
 def stats(tweet):
     inner_text = tweet["text"]
     guess = Language.guess(inner_text)
     print inner_text[0:20], guess
     return {"tweet" : tweet, "lang" : guess}
Ejemplo n.º 55
0
 def __init__(self, encoding, substitutions, verbose, log):
     Language.__init__(self, encoding, substitutions, verbose, log)
Ejemplo n.º 56
0
    def get(self, photo_id):
        # Session request handler
        current_session = Session(self)
        JINJA_ENVIRONMENT.globals['session'] = current_session
        # Language request handler
        Language.language(self)
        # Load jinja template
        template = JINJA_ENVIRONMENT.get_template('static/templates/photo.html')

        # Check permission
        photo = database.PhotosManager.get_photo_by_id(int(photo_id))
        if current_session.get_id() is None:
            request_user = None
        else:
            request_user = database.UserManager.select_by_id(current_session.get_id())
        if not security.PhotoSecurity.user_is_allowed_to_watch_photo(photo, request_user):
            self.redirect("/")

        # Get photo info to display
        user = photo.owner.get()
        privacy = photo.privacy
        date = photo.date
        # Check if user can edit photo attributes
        edition_permission = (current_session.get_role_level() is 3) or (photo.owner == current_session.get_user_key())

        # Get user allowed to watch photo
        if privacy == 1:
            allowed_users = database.PhotoUserPermissionManager.get_allowed_users_by_photo(photo)
        else:
            allowed_users = None

        # Count photo visited by user
        if current_session.get_id() is None:
            database.PhotoViewManager.newView(photo, None)
        else:
            database.PhotoViewManager.newView(photo, current_session.user)

        # Photo visualization count
        photo_views = database.PhotoViewManager.select_users_by_photo(photo)
        views_counter = {}
        for photo_view in photo_views:
            if photo_view.user is None:
                if "Anonymous" in views_counter:
                    views_counter["Anonymous"]['count'] += 1
                else:
                    views_counter["Anonymous"] = {'count':1,
                                                  'name':"Anonymous",
                                                  'id': None}
            else:
                photo_view_user = photo_view.user
                if photo_view_user.get().name in views_counter:
                    views_counter[photo_view_user.get().name]['count'] += 1
                else:
                    views_counter[photo_view_user.get().name] = {'count':1,
                                                                 'name':photo_view_user.get().name,
                                                                 'id': photo_view_user.id()}
        # Response page
        self.response.write(template.render(
            photo_id=photo_id,
            owner=user,
            name=photo.name,
            edition_permission= edition_permission,
            date= date,
            privacy=privacy,
            views=views_counter,
            every_user_list=database.UserManager.select(),
            allowed_users=allowed_users
        ))
Ejemplo n.º 57
0
    def describe_image(query_item, candidate_items):
        """ Retrieve relevant descriptions for a query image """

        vision = Vision()
        language = Language()
        utilities = Utilities()

        settings = utilities.load_settings('settings.ini', 'Settings')
        neighbours = vision.retrieve_visually_similar_images(query_item, candidate_items, int(settings['farneighborsize']), settings)

        # ---------------------------------VISUALLY CLOSEST--------------------------------
        visually_closest_captions = []
        for vcCaption in neighbours[0][int(settings["captionindex"])]:  # find visually closest candidates' captions
            visually_closest_captions.append(vcCaption)

        neighbours, dist_min, dist_max = vision.remove_outliers(neighbours, int(settings["vdsindex"]),
                                                                float(settings["epsilon"]), int(settings["neighborsize"]))

        # ---------------------------- START MAIN PROCEDURE ----------------------------------

        w, model, vocab = language.load_word_models(settings)
        word_list_to_exclude = language.load_word_list_to_exclude(settings)
        sample_word_vector = language.get_word_vector('a', settings['method'], model, w, vocab)
        zeros = np.zeros(sample_word_vector.shape)

        number_of_tokens = []
        if settings['usesentencelengthpenalty'] == '1':
            for i, neighbour in enumerate(neighbours):
                for j, caption in enumerate(neighbour[int(settings["captionindex"])]):
                    if len(caption) == 1:
                        tokens = caption.split()  # tokenize by whitespace
                    else:
                        tokens = caption  # sentence is already tokenized
                    token_count = len(tokens)
                    number_of_tokens.append(token_count)
            average_token_count = int(np.mean(number_of_tokens))

        # --------------------COMPUTE QUERY VECTOR------------------------------
        total_vector_sum = zeros
        all_caption_vector_items = []
        oov_count = 0
        token_count = 0

        for i, neighbour in enumerate(neighbours):  # for each candidate
            caption_vector_sum = zeros  # to store caption vectors

            if settings['usevisualsimilarityscores'] == '1':
                visual_similarity_score = vision.compute_visual_similarity(neighbour[int(settings["vdsindex"])], dist_min, dist_max)
            else:
                visual_similarity_score = 1  # no effect

            for j, caption in enumerate(neighbour[int(settings["captionindex"])]):
                caption_vector, number_of_tokens, number_of_oovs = language.compute_sentence_vector(caption, model, w, vocab, zeros,
                                                                                                    word_list_to_exclude, settings)
                token_count = token_count + number_of_tokens
                oov_count = oov_count + number_of_oovs
                index_of_item = i + 1  # the index of item in the original list
                caption_vector = caption_vector * visual_similarity_score  # weighted summation with visual distance

                if settings['usesentencelengthpenalty'] == '1':
                    penalty = language.compute_sentence_length_penalty(number_of_tokens, average_token_count)
                    caption_vector = caption_vector * penalty

                caption_vector_item = [index_of_item, caption_vector, caption]
                all_caption_vector_items.append(caption_vector_item)
                caption_vector_sum += caption_vector

            total_vector_sum = total_vector_sum + caption_vector_sum

        query_vector = np.divide(total_vector_sum, len(all_caption_vector_items))

        cosine_similarities = []

        for caption_vector_item in all_caption_vector_items:
            cosine_similarity = language.compute_cosine_similarity(query_vector, caption_vector_item[1])  # 2nd index holds caption vector
            cosine_similarities.append(cosine_similarity)
            caption_vector_item.append(cosine_similarity)

        all_caption_vector_items.sort(key=lambda x: x[3], reverse=True)  # sort by 4th column, that is cosine similarity

        candidate_translations = []  # select top N descriptions from the results
        for i, caption_vector_item in enumerate(all_caption_vector_items[0:int(settings['numberofcaptionstoreturn'])]):
            candidate_translations.append(caption_vector_item[2])

        reference_translations = []
        for i, query_caption in enumerate(query_item[int(settings["captionindex"])]):
            reference_translations.append(query_caption)

        oov_rate = oov_count * 100 / token_count

        return [candidate_translations, reference_translations, visually_closest_captions, oov_rate]
Ejemplo n.º 58
0
    def post(self):
        # Session request handler
        current_session = Session(self)
        JINJA_ENVIRONMENT.globals['session'] = current_session
        # Language request handler
        Language.language(self)
        # Check if user is already logged in
        if current_session.get_id() is not None:
            self.redirect("/")
            return None
        # Retrieve request data
        username = cgi.escape(self.request.get('username'))
        password1 = cgi.escape(self.request.get('password1'))
        password2 = cgi.escape(self.request.get('password2'))
        email = cgi.escape(self.request.get('email'))

        # Load success and fail templates
        register_template = JINJA_ENVIRONMENT.get_template('static/templates/register.html')
        registered_template = JINJA_ENVIRONMENT.get_template('static/templates/registered.html')

        # Check email is well formed
        if not re.match(r"[^@]+@[^@]+\.[^@]+", email):
            self.response.write(register_template.render(error=_("BadEmail.")))
            return None
        # Check passwords min size is 6
        if len(password1) < 6:
            self.response.write(register_template.render(error=_("PasswordMinLengthNotReached.")))
            return None
        # Check passwords match
        if password1 != password2:
            self.response.write(register_template.render(error=_("PasswordMissmatch")))
            return None
        # Username not empty
        if len(username) < 1:
            self.response.write(register_template.render(error=_("EmptyUsername.")))
            return None
        # Check user exists
        user = database.UserManager.select_by_username(username)
        if user is not None:
            self.response.write(register_template.render(error=_("UsernameExists")))
            return None
        # Check email exists
        user = database.UserManager.select_by_email(email)
        if user is not None:
            self.response.write(register_template.render(error=_("EmailExists")))
            return None

        # Save new user in DB
        user_key = database.UserManager.create(username, password1, email)

        if user_key:
            # Create activation token
            token_key = database.TokenManager.create_token(user_key)
            # Send activation email
            email_handler.Email.send_activation(username, str(token_key.id()), email)
            # Autologin new user
            current_session.set(self, user_key.id())
            JINJA_ENVIRONMENT.globals['session'] = current_session
            self.response.write(registered_template.render(username=username))
        else:
            self.response.write(register_template.render(error=_("DatabaseError")))
            return None
 def __init__(self):
     Language.__init__(self, "English")
     f = open("../res/english.txt")
     self.generate_table(f.read())
     f.close()
Ejemplo n.º 60
0
def main():
    global checkpoint, waiting, best_loss, start_epoch
    # Vocabulary
    if checkpoint is None:
        init_vectors_map()
    language = Language(file_path_tokens_map=file_path_tokens_map, file_path_vectors_map=file_path_vectors_map)
    vocab_size = language.get_n_tokens()
    print('total vocab_size:', vocab_size)

    # Dataset
    korean_dataset = KoreanDataset(file_path_data=file_path_data, file_path_tokens_map=file_path_tokens_map,
                                   max_len_sentence=max_len_sentence, max_len_morpheme=max_len_morpheme,
                                   noise=noise, continuous=continuous)
    dataset_size = len(korean_dataset)
    print('total dataset_size:', dataset_size)
    indices = list(range(dataset_size))
    split = int(np.floor(validation_split * dataset_size))  # split for training and validation set

    # Model
    if checkpoint is None:
        model = AnomalyKoreanDetector(len_morpheme=max_len_morpheme,
                                      len_sentence=max_len_sentence,
                                      syllable_layer_type=syllable_layer_type,
                                      syllable_num_layers=syllable_num_layers,
                                      vocab_size=vocab_size,
                                      attention_num_layer=attention_num_layer,
                                      morpheme_layer_type=morpheme_layer_type,
                                      morpheme_num_layers=morpheme_num_layers,
                                      sentence_layer_type=sentence_layer_type,
                                      sentence_num_layers=sentence_num_layers,
                                      classifier_num_layer=classifier_num_layer,
                                      embedding_size=embedding_dim,
                                      phoneme_in_size=phoneme_in_size,
                                      phoneme_out_size=phoneme_out_size,
                                      morpheme_out_size=morpheme_out_size,
                                      sentence_out_size=sentence_out_size,
                                      attention_type=attention_type)

        # Optimizer
        model_optimizer = torch.optim.Adam(params=filter(lambda p: p.requires_grad, model.parameters()), lr=model_lr)
    else:
        checkpoint = torch.load(checkpoint)
        start_epoch = checkpoint['epoch'] + 1
        waiting = checkpoint['waiting']
        model = checkpoint['model']
        model_optimizer = checkpoint['model_optimizer']

    model = model.to(device)

    # Loss function
    criterion_is_noise = nn.BCELoss().to(device)
    criterion_is_next = nn.BCELoss().to(device)

    for epoch in range(start_epoch, epochs):
        # Creating data indices for training and validation splits:
        if shuffle_dataset:
            np.random.seed(random_seed)
            np.random.shuffle(indices)
        train_indices, val_indices = indices[split:], indices[:split]

        # Creating data samplers and loaders:
        train_sampler = SubsetRandomSampler(train_indices)
        valid_sampler = SubsetRandomSampler(val_indices)
        train_loader = torch.utils.data.DataLoader(korean_dataset, batch_size=batch_size, sampler=train_sampler,
                                                   pin_memory=True, drop_last=True)
        validation_loader = torch.utils.data.DataLoader(korean_dataset, batch_size=batch_size, sampler=valid_sampler,
                                                        pin_memory=True, drop_last=True)

        if waiting >= patience:
            break
        if waiting > 0 and waiting % weight_decay_per_epoch == 0:
            adjust_learning_rate(optimizer=model_optimizer, shrink_factor=weight_decay_percentage)

        train(train_loader=train_loader,
              model=model,
              optimizer=model_optimizer,
              criterion_is_noise=criterion_is_noise,
              criterion_is_next=criterion_is_next,
              epoch=epoch)

        with torch.no_grad():
            mean_loss = validate(validation_loader=validation_loader,
                                 model=model,
                                 criterion_is_noise=criterion_is_noise,
                                 criterion_is_next=criterion_is_next)

        is_best = mean_loss < best_loss
        best_loss = min(mean_loss, best_loss)
        if not is_best:
            waiting += 1
        else:
            waiting = 0

        # Save checkpoint
        filepath = os.path.join(here, now, 'checkpoint.pth')
        save_checkpoint(filepath, epoch, waiting, model, model_optimizer, mean_loss, is_best)