Skip to content

typosfile

Classes to handle .typos files in $GTFREE.

Typoline

Class to parse a line of a .typos file

Source code in /home/anders/projects/CorpusTools/corpustools/typosfile.py
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
class Typoline:
    """Class to parse a line of a .typos file"""

    def __init__(self, typoline):
        """Parse a typoline

        A typoline has a number showing frequency of the typo, the typo and
        possibly a correction
        """
        parts = typoline.split("\t")

        self.typo = parts[0]

        if len(parts) == 2:
            self.correction = parts[1]
        else:
            self.correction = None

    def getTypo(self):
        return self.typo

    def setCorrection(self, correction):
        self.correction = correction

    def getCorrection(self):
        return self.correction

    def makeTypoline(self):
        """Make a typoline from the three data parts in this class"""
        result = self.typo
        if self.correction and self.correction != self.typo:
            result = f"{result}\t{self.correction}"

        return result

__init__(typoline)

Parse a typoline

A typoline has a number showing frequency of the typo, the typo and possibly a correction

Source code in /home/anders/projects/CorpusTools/corpustools/typosfile.py
26
27
28
29
30
31
32
33
34
35
36
37
38
39
def __init__(self, typoline):
    """Parse a typoline

    A typoline has a number showing frequency of the typo, the typo and
    possibly a correction
    """
    parts = typoline.split("\t")

    self.typo = parts[0]

    if len(parts) == 2:
        self.correction = parts[1]
    else:
        self.correction = None

makeTypoline()

Make a typoline from the three data parts in this class

Source code in /home/anders/projects/CorpusTools/corpustools/typosfile.py
50
51
52
53
54
55
56
def makeTypoline(self):
    """Make a typoline from the three data parts in this class"""
    result = self.typo
    if self.correction and self.correction != self.typo:
        result = f"{result}\t{self.correction}"

    return result

Typos

A class that reads typos and corrections from a .typos files

Source code in /home/anders/projects/CorpusTools/corpustools/typosfile.py
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
class Typos:
    """A class that reads typos and corrections from a .typos files"""

    def __init__(self, typosfile):
        """Read typos from typosfile.

        If a correction exists, insert the typos
        and corrections into self.typos
        """
        self.typos = {}
        typofile = open(typosfile)

        for strline in typofile:
            line = strline.decode("utf-8")
            if line.strip():
                tl = Typoline(line.rstrip())
                if tl.getCorrection() and tl.getTypo() != tl.getCorrection():
                    self.typos[tl.getTypo()] = tl.getCorrection()
        typofile.close()

    def getTypos(self):
        """Return the typos dict"""
        return self.typos

__init__(typosfile)

Read typos from typosfile.

If a correction exists, insert the typos and corrections into self.typos

Source code in /home/anders/projects/CorpusTools/corpustools/typosfile.py
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
def __init__(self, typosfile):
    """Read typos from typosfile.

    If a correction exists, insert the typos
    and corrections into self.typos
    """
    self.typos = {}
    typofile = open(typosfile)

    for strline in typofile:
        line = strline.decode("utf-8")
        if line.strip():
            tl = Typoline(line.rstrip())
            if tl.getCorrection() and tl.getTypo() != tl.getCorrection():
                self.typos[tl.getTypo()] = tl.getCorrection()
    typofile.close()

getTypos()

Return the typos dict

Source code in /home/anders/projects/CorpusTools/corpustools/typosfile.py
79
80
81
def getTypos(self):
    """Return the typos dict"""
    return self.typos