Пример #1
0
    return {'b': b, 'db': db, 'tags': unicode(tags)}


def per(st, n):  # :: State -> Note -> State
    #n.delTag( st['tags'] ) # clear tags if they already exist?

    notecfg = getFilter(n)
    if notecfg is None: return st
    morphemizer = getMorphemizerByName(notecfg['Morphemizer'])
    for field in notecfg['Fields']:
        for m in getMorphemes(morphemizer, n[field]):
            if m in st['db'].db:
                n.addTag(st['tags'])
                break

    n.flush()
    return st


def post(st):  # :: State -> State
    infoMsg('Tagged all notes containing morphemes in that db')
    return st


addBrowserSelectionCmd('MorphMan: Mass Tagger',
                       pre,
                       per,
                       post,
                       tooltip='Tag all cards that contain morphemes from db',
                       shortcut=None)
Пример #2
0
    return {'vid2nid': {}}


def per(st, n):
    for f in cfg(n.mid, None, 'batch media fields'):
        try:
            r = re.search(anki.sound._soundReg, n[f])
            if r:
                st['vid2nid'][r.group(1)] = n.id
                break
        except KeyError:
            pass
    return st


def post(st):
    #TODO: queue all the files in a big list with `loadfile {filename} 1` so you can skip back and forth easily
    # when user chooses, use `get_file_name`
    for vid, nid in st['vid2nid'].iteritems():
        anki.sound.play(vid)
    st['__reset'] = False
    return st


addBrowserSelectionCmd('MorphMan: Batch Play',
                       pre,
                       per,
                       post,
                       tooltip='Play all the videos for the selected cards',
                       shortcut=('Ctrl+Alt+P', ))
#-*- coding: utf-8 -*-
import os

from morphemes import AnkiDeck, MorphDb, getMorphemes, ms2str
from util import addBrowserSelectionCmd, cfg, cfg1, mw, infoMsg, QFileDialog

def pre( b ):
    from util import dbsPath # not defined until late, so don't import at top of module
    path = QFileDialog.getSaveFileName( caption='Save morpheme db to?', directory=dbsPath + os.sep + 'exportedMorphs.db' )
    if not path: return
    return { 'dbpath':str(path), 'morphDb':MorphDb() }

def per( st, n ):
    mats = mw.col.db.list( 'select ivl from cards where nid = :nid', nid=n.id )
    for f in cfg( n.mid, None, 'morph_fields' ):
        ms = getMorphemes( n[ f ], None, cfg1('morph_blacklist') )
        loc = AnkiDeck( n.id, f, n[ f ], n.guid, mats )
        st['morphDb'].addMsL( ms, loc )
    return st

def post( st ):
    st['morphDb'].save( st['dbpath'] )
    infoMsg( 'DB saved with extracted morphemes' )

addBrowserSelectionCmd( 'Extract Morphemes', pre, per, post, tooltip='Extract morphemes in selected notes to a MorphMan db', shortcut=('Ctrl+Shift+E',) )
Пример #4
0
#-*- coding: utf-8 -*-
from morphemes import getMorphemes, ms2str, MorphDb
from util import addBrowserSelectionCmd, cfg, cfg1, infoMsg, QInputDialog, QFileDialog, QLineEdit
import util

def pre( b ):
    tags, ok = QInputDialog.getText( b, 'Enter tags', 'Tags', QLineEdit.Normal, 'hasMorph' )
    if not ok or not tags: return
    path = QFileDialog.getOpenFileName( caption='Open db', directory=util.dbsPath )
    if not path: return
    db = MorphDb( path )
    return { 'b':b, 'db':db, 'tags':unicode(tags) }

def per( st, n ):
    n.delTag( st['tags'] )

    if n['k+N'] == '1': # FIXME this special but commonly wanted logic must be a cfg option
        ms = getMorphemes( n['focusMorph'], None, cfg1('morph_blacklist') )
        for m in ms:
            if m in st['db'].db:
                n.addTag( st['tags'] )
                break

    n.flush()
    return st

def post( st ):
    return st

addBrowserSelectionCmd( 'Mass Tagger', pre, per, post, tooltip='Tag all cards that contain morphemes from db', shortcut=None )
Пример #5
0
#-*- coding: utf-8 -*-
from morphemes import getMorphemes, ms2str
from morphemizer import getMorphemizerForNote
from util import addBrowserSelectionCmd, cfg, cfg1, infoMsg


def pre(b):
    return {'txt': '', 'morphemizer': None}


def per(st, n):
    st['morphemizer'] = getMorphemizerForNote(n)
    for f in cfg(n.mid, None, 'morph_fields'):
        st['txt'] += n[f] + '  '
    return st


def post(st):
    ms = getMorphemes(st['morphemizer'], st['txt'])
    s = ms2str(ms)
    infoMsg('----- All -----\n' + s)


addBrowserSelectionCmd('MorphMan: View Morphemes',
                       pre,
                       per,
                       post,
                       tooltip='View Morphemes for selected note',
                       shortcut=('Ctrl+Shift+V', ))
Пример #6
0

def post(st):
    i = 0
    for n in st['notes']:
        for c in n.cards():
            mw.reviewer.cardQueue.append(c)
            i += 1
    st['browser'].close()
    st['__reset'] = False
    tooltip(_('Immediately reviewing %d cards' % i))


addBrowserSelectionCmd('MorphMan: Learn Now',
                       pre,
                       per,
                       post,
                       tooltip='Immediately review the selected new cards',
                       shortcut=('Ctrl+Shift+N', ))

########## 5 - highlight morphemes using morphHighlight
import re


def isNoteSame(note, fieldDict):
    # compare fields
    same_as_note = True
    items = note.items()
    for (key, value) in items:
        if key not in fieldDict or value != fieldDict[key]:
            return False
Пример #7
0
########## 4 - immediately review selected cards
def pre( b ): return { 'notes':[], 'browser':b }
def per( st, n ):
    st['notes'].append( n )
    return st
def post( st ):
    i = 0
    for n in st['notes']:
        for c in n.cards():
            mw.reviewer.cardQueue.append( c )
            i += 1
    st['browser'].close()
    st['__reset'] = False
    tooltip( _( 'Immediately reviewing %d cards' % i ) )

addBrowserSelectionCmd( 'Learn Now', pre, per, post, tooltip='Immediately review the selected new cards', shortcut=('Ctrl+Shift+N',) )

########## 5 - highlight morphemes using morphHighlight
import re
def highlight( txt, extra, fieldDict, field, mod_field ):
    '''When a field is marked with the 'focusMorph' command, we format it by
    wrapping all the morphemes in <span>s with attributes set to its maturity'''
    # must avoid formatting a smaller morph that is contained in a bigger morph
    # => do largest subs first and don't sub anything already in <span>
    def nonSpanSub( sub, repl, string ):
        return u''.join( re.sub( sub, repl, s ) if not s.startswith('<span') else s for s in re.split( '(<span.*?</span>)', string ) )
    from morphemes import getMorphemes
    ms = getMorphemes( txt )
    for m in sorted( ms, key=lambda x: len(x.inflected), reverse=True ): # largest subs first
        locs = allDb().db.get( m, set() )
        mat = max( loc.maturity for loc in locs ) if locs else 0
Пример #8
0
#-*- coding: utf-8 -*-
from util import addBrowserSelectionCmd, cfg, cfg1
import anki.sound
import re

def pre( b ): return { 'vid2nid':{} }

def per( st, n ):
    for f in cfg( n.mid, None, 'batch media fields' ):
        try:
            r = re.search( anki.sound._soundReg, n[ f ] )
            if r:
                st['vid2nid'][ r.group(1) ] = n.id
                break
        except KeyError: pass
    return st

def post( st ):
    #TODO: queue all the files in a big list with `loadfile {filename} 1` so you can skip back and forth easily
    # when user chooses, use `get_file_name`
    for vid, nid in st['vid2nid'].iteritems():
        anki.sound.play( vid )
    st['__reset'] = False
    return st

addBrowserSelectionCmd( 'Batch Play', pre, per, post, tooltip='Play all the videos for the selected cards', shortcut=('Ctrl+Shift+P',) )
Пример #9
0
#-*- coding: utf-8 -*-
from morphemes import getMorphemes, ms2str
from util import addBrowserSelectionCmd, cfg, cfg1, infoMsg

def pre( b ): return { 'txt':'' }

def per( st, n ):
    for f in cfg( n.mid, None, 'morph_fields' ):
        st['txt'] += n[ f ] + '  '
    return st

def post( st ):
    ms = getMorphemes( st['txt'], None, cfg1('morph_blacklist') )
    s = ms2str( ms )
    infoMsg( '----- All -----\n' + s )

addBrowserSelectionCmd( 'View Morphemes', pre, per, post, tooltip='View Morphemes for selected note', shortcut=('Ctrl+Shift+V',) )