コード例 #1
0
ファイル: chick.py プロジェクト: rflynn/spill-chick
	def correct(self, txt):
		"""
		given a string, identify the least-common n-gram not present in 'skip'
		and return a list of suggested replacements
		"""
		d = Doc(txt, self.w)
		changes = list(self.suggest(d, 1))
		for ch in changes:
			logger.debug('ch=%s' % (ch,))
			change = [ch[0].ngd]
			logger.debug('change=%s' % (change,))
			d.applyChanges(change)
			logger.debug('change=%s after applyChanges d=%s' % (change, d))
			d = Doc(d, self.w)
			break # FIXME: loops forever
			changes = list(self.suggest(d, 1))
		res = str(d).decode('utf8')
		logger.debug('correct res=%s %s' % (type(res),res))
		return res
コード例 #2
0
ファイル: chick.py プロジェクト: rflynn/spill-chick
    def correct(self, txt):
        """
		given a string, identify the least-common n-gram not present in 'skip'
		and return a list of suggested replacements
		"""
        d = Doc(txt, self.w)
        changes = list(self.suggest(d, 1))
        for ch in changes:
            logger.debug('ch=%s' % (ch, ))
            change = [ch[0].ngd]
            logger.debug('change=%s' % (change, ))
            d.applyChanges(change)
            logger.debug('change=%s after applyChanges d=%s' % (change, d))
            d = Doc(d, self.w)
            break  # FIXME: loops forever
            changes = list(self.suggest(d, 1))
        res = str(d).decode('utf8')
        logger.debug('correct res=%s %s' % (type(res), res))
        return res
コード例 #3
0
ファイル: code.py プロジェクト: rflynn/spill-chick
    def POST(self):
        start_time = time()
        text = unicode(web.input().get("text", ""))
        lines = text.split("\r\n")

        act = web.input().get("act", "")
        if act == "Replace":
            # FIXME: if replacement takes place, update location/offsets
            # of all remaining session['suggestions']
            replacement_index = int(web.input().get("replacement_index", "0"))
            if replacement_index:
                d = Doc(lines, chick.w)
                replacements = session.get("replacements")
                if replacement_index <= len(replacements):
                    replacement = replacements[replacement_index - 1]
                    d.applyChanges([replacement])
                    text = str(d)
                    lines = d.lines
                    logger.debug("after replacement lines=%s" % (lines,))
                session["suggestions"].pop(0)
        elif act == "Skip to next...":
            session["skip"].append(session["target"])
            session["suggestions"].pop(0)
        elif act == "Done":
            # nuke target, replacements, skip, etc.
            session.kill()

        sugg2 = []
        suggs = []
        suggestions = []
        replacements = []

        if act and act != "Done":
            suggestions = session["suggestions"]
            if not suggestions:
                logger.debug("suggest(lines=%s)" % (lines,))
                suggestions = list(chick.suggest(lines, 5, session["skip"]))
            if not suggestions:
                target, suggs, sugg2 = None, [], []
            else:
                # calculate offsets based on line length so we can highlight target substring in <texarea>
                off = [len(l) + 1 for l in lines]
                lineoff = [0] + [sum(off[:i]) for i in range(1, len(off) + 1)]
                changes = suggestions[0]
                target = changes[0].ngd.oldtoks()
                for ch in changes:
                    ngd = ch.ngd
                    replacements.append(ngd)
                    o = ngd.old()
                    r = ngd.new()
                    linestart = o[0][1]
                    lineend = o[-1][1]
                    start = o[0][3]
                    end = o[-1][3] + len(o[-1][0])
                    sugg2.append((" ".join(ngd.newtoks()), lineoff[linestart] + start, lineoff[lineend] + end))
            session["target"] = target
            session["replacements"] = replacements
            session["suggestions"] = suggestions

        elapsed = round(time() - start_time, 2)
        return render.check(text, sugg2, lines, elapsed, suggestions)
コード例 #4
0
    def POST(self):
        start_time = time()
        text = unicode(web.input().get('text', ''))
        lines = text.split('\r\n')

        act = web.input().get('act', '')
        if act == 'Replace':
            # FIXME: if replacement takes place, update location/offsets
            # of all remaining session['suggestions']
            replacement_index = int(web.input().get('replacement_index', '0'))
            if replacement_index:
                d = Doc(lines, chick.w)
                replacements = session.get('replacements')
                if replacement_index <= len(replacements):
                    replacement = replacements[replacement_index - 1]
                    d.applyChanges([replacement])
                    text = str(d)
                    lines = d.lines
                    logger.debug('after replacement lines=%s' % (lines, ))
                session['suggestions'].pop(0)
        elif act == 'Skip to next...':
            session['skip'].append(session['target'])
            session['suggestions'].pop(0)
        elif act == 'Done':
            # nuke target, replacements, skip, etc.
            session.kill()

        sugg2 = []
        suggs = []
        suggestions = []
        replacements = []

        if act and act != 'Done':
            suggestions = session['suggestions']
            if not suggestions:
                logger.debug('suggest(lines=%s)' % (lines, ))
                suggestions = list(chick.suggest(lines, 5, session['skip']))
            if not suggestions:
                target, suggs, sugg2 = None, [], []
            else:
                # calculate offsets based on line length so we can highlight target substring in <texarea>
                off = [len(l) + 1 for l in lines]
                lineoff = [0] + [sum(off[:i]) for i in range(1, len(off) + 1)]
                changes = suggestions[0]
                target = changes[0].ngd.oldtoks()
                for ch in changes:
                    ngd = ch.ngd
                    replacements.append(ngd)
                    o = ngd.old()
                    r = ngd.new()
                    linestart = o[0][1]
                    lineend = o[-1][1]
                    start = o[0][3]
                    end = o[-1][3] + len(o[-1][0])
                    sugg2.append(
                        (' '.join(ngd.newtoks()), lineoff[linestart] + start,
                         lineoff[lineend] + end))
            session['target'] = target
            session['replacements'] = replacements
            session['suggestions'] = suggestions

        elapsed = round(time() - start_time, 2)
        return render.check(text, sugg2, lines, elapsed, suggestions)