| 1 |
|
| 2 |
|
| 3 |
""" |
| 4 |
Code for computing the pattern score. |
| 5 |
|
| 6 |
Author: Gertjan van den Burg |
| 7 |
|
| 8 |
""" |
| 9 |
|
| 10 |
import collections |
| 11 |
import re |
| 12 |
|
| 13 |
from typing import Optional |
| 14 |
from typing import Pattern |
| 15 |
|
| 16 |
from .cabstraction import base_abstraction |
| 17 |
from .cabstraction import c_merge_with_quotechar |
| 18 |
from .dialect import SimpleDialect |
| 19 |
|
| 20 |
DEFAULT_EPS_PAT: float = 1e-3 |
| 21 |
|
| 22 |
RE_MULTI_C: Pattern[str] = re.compile(r"C{2,}") |
| 23 |
|
| 24 |
|
| 25 |
def pattern_score( |
| 26 |
data: str, dialect: SimpleDialect, eps: float = DEFAULT_EPS_PAT |
| 27 |
) -> float: |
| 28 |
""" |
| 29 |
Compute the pattern score for given data and a dialect. |
| 30 |
|
| 31 |
Parameters |
| 32 |
---------- |
| 33 |
|
| 34 |
data : str |
| 35 |
The data of the file as a raw character string |
| 36 |
|
| 37 |
dialect: dialect.Dialect |
| 38 |
The dialect object |
| 39 |
|
| 40 |
Returns |
| 41 |
------- |
| 42 |
score : float |
| 43 |
the pattern score |
| 44 |
|
| 45 |
""" |
| 46 |
A = make_abstraction(data, dialect) |
| 47 |
row_patterns = collections.Counter(A.split("R")) |
| 48 |
P = 0.0 |
| 49 |
for pat_k, Nk in row_patterns.items(): |
| 50 |
Lk = len(pat_k.split("D")) |
| 51 |
P += Nk * (max(eps, Lk - 1) / Lk) |
| 52 |
P /= len(row_patterns) |
| 53 |
return P |
| 54 |
|
| 55 |
|
| 56 |
def make_abstraction(data: str, dialect: SimpleDialect) -> str: |
| 57 |
"""Create an abstract representation of the CSV file based on the dialect. |
| 58 |
|
| 59 |
This function constructs the basic abstraction used to compute the row |
| 60 |
patterns. |
| 61 |
|
| 62 |
Parameters |
| 63 |
---------- |
| 64 |
data : str |
| 65 |
The data of the file as a string. |
| 66 |
|
| 67 |
dialect : SimpleDialect |
| 68 |
A dialect to parse the file with. |
| 69 |
|
| 70 |
Returns |
| 71 |
------- |
| 72 |
abstraction : str |
| 73 |
An abstract representation of the CSV file. |
| 74 |
|
| 75 |
""" |
| 76 |
A = base_abstraction( |
| 77 |
data, dialect.delimiter, dialect.quotechar, dialect.escapechar |
| 78 |
) |
| 79 |
A = merge_with_quotechar(A) |
| 80 |
A = fill_empties(A) |
| 81 |
A = strip_trailing(A) |
| 82 |
return A |
| 83 |
|
| 84 |
|
| 85 |
def merge_with_quotechar( |
| 86 |
S: str, dialect: Optional[SimpleDialect] = None |
| 87 |
) -> str: |
| 88 |
"""Merge quoted blocks in the abstraction |
| 89 |
|
| 90 |
This function takes the abstract representation and merges quoted blocks |
| 91 |
(``QC...CQ``) to a single cell (``C``). The function takes nested quotes |
| 92 |
into account. |
| 93 |
|
| 94 |
Parameters |
| 95 |
---------- |
| 96 |
S : str |
| 97 |
The data of a file as a string |
| 98 |
|
| 99 |
dialect : SimpleDialect |
| 100 |
The dialect used to make the abstraction. This is not used but kept for |
| 101 |
backwards compatibility. Will be removed in a future version. |
| 102 |
|
| 103 |
Returns |
| 104 |
------- |
| 105 |
abstraction : str |
| 106 |
A simplified version of the abstraction with quoted blocks merged. |
| 107 |
|
| 108 |
""" |
| 109 |
return c_merge_with_quotechar(S) |
| 110 |
|
| 111 |
|
| 112 |
def fill_empties(abstract: str) -> str: |
| 113 |
"""Fill empty cells in the abstraction |
| 114 |
|
| 115 |
The way the row patterns are constructed assumes that empty cells are |
| 116 |
marked by the letter `C` as well. This function fill those in. The function |
| 117 |
also removes duplicate occurrances of ``CC`` and replaces these with |
| 118 |
``C``. |
| 119 |
|
| 120 |
Parameters |
| 121 |
---------- |
| 122 |
abstract : str |
| 123 |
The abstract representation of the file. |
| 124 |
|
| 125 |
Returns |
| 126 |
------- |
| 127 |
abstraction : str |
| 128 |
The abstract representation with empties filled. |
| 129 |
|
| 130 |
|
| 131 |
""" |
| 132 |
while "DD" in abstract: |
| 133 |
abstract = abstract.replace("DD", "DCD") |
| 134 |
|
| 135 |
while "DR" in abstract: |
| 136 |
abstract = abstract.replace("DR", "DCR") |
| 137 |
|
| 138 |
while "RD" in abstract: |
| 139 |
abstract = abstract.replace("RD", "RCD") |
| 140 |
|
| 141 |
abstract = RE_MULTI_C.sub("C", abstract) |
| 142 |
|
| 143 |
if abstract.startswith("D"): |
| 144 |
abstract = "C" + abstract |
| 145 |
|
| 146 |
if abstract.endswith("D"): |
| 147 |
abstract += "C" |
| 148 |
|
| 149 |
return abstract |
| 150 |
|
| 151 |
|
| 152 |
def strip_trailing(abstract: str) -> str: |
| 153 |
"""Strip trailing row separator from abstraction.""" |
| 154 |
while abstract.endswith("R"): |
| 155 |
abstract = abstract[:-1] |
| 156 |
return abstract |