MCPcopy Create free account
hub / github.com/InternScience/SciReason / Alignment

Class Alignment

opencompass/datasets/lawbench/utils/modules/alignment.py:49–323  ·  view source on GitHub ↗

对齐错误句子和正确句子, 使用编辑距离算法抽取编辑操作

Source from the content-addressed store, hash-verified

47 return confusion_dict
48
49class Alignment:
50 """
51 对齐错误句子和正确句子,
52 使用编辑距离算法抽取编辑操作
53 """
54
55 def __init__(
56 self,
57 semantic_dict: Dict,
58 confusion_dict: Dict,
59 granularity: str = "word",
60 ) -> None:
61 """
62 构造函数
63 :param semantic_dict: 语义词典(大词林)
64 :param confusion_dict: 字符混淆集
65 """
66 self.insertion_cost = 1
67 self.deletion_cost = 1
68 self.semantic_dict = semantic_dict
69 self.confusion_dict = confusion_dict
70 # Because we use character level tokenization, this doesn't currently use POS
71 self._open_pos = {} # 如果是词级别,还可以利用词性是否相同来计算cost
72 self.granularity = granularity # word-level or character-level
73 self.align_seqs = []
74
75 def __call__(self,
76 src: List[Tuple],
77 tgt: List[Tuple],
78 verbose: bool = False):
79 cost_matrix, oper_matrix = self.align(src, tgt)
80 align_seq = self.get_cheapest_align_seq(oper_matrix)
81
82 if verbose:
83 print("========== Seg. and POS: ==========")
84 print(src)
85 print(tgt)
86 print("========== Cost Matrix ==========")
87 print(cost_matrix)
88 print("========== Oper Matrix ==========")
89 print(oper_matrix)
90 print("========== Alignment ==========")
91 print(align_seq)
92 print("========== Results ==========")
93 for a in align_seq:
94 print(a[0], src[a[1]: a[2]], tgt[a[3]: a[4]])
95 return align_seq
96
97 def _get_semantic_class(self, word):
98 """
99 NOTE: Based on the paper:
100 Improved-Edit-Distance Kernel for Chinese Relation Extraction
101 获取每个词语的语义类别(基于大词林,有三个级别)
102 """
103 if word in self.semantic_dict:
104 code = self.semantic_dict[word]
105 high, mid, low = code[0], code[1], code[2:4]
106 return high, mid, low

Callers 3

create_defaultMethod · 0.90
merger.pyFile · 0.90
alignment.pyFile · 0.85

Calls

no outgoing calls

Tested by

no test coverage detected