-
Notifications
You must be signed in to change notification settings - Fork 0
/
WFF.py
167 lines (167 loc) · 6.48 KB
/
WFF.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
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
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
import re
__all__= "WFF"
class WFF:
def __int__(self,setString:str):
self.target=setString
self.errors=[]
def printRule(self)->str:
rules="请保证输入的语句中,原子命题,括号,联结符之间存在空格\
为保证程序的正确运行,请将'→‘用LaTex语句 \\rightarrow替代,双条件用\\leftrightarrow替代\
命题请使用大写字母,暂且不支持希伯来字母,后续学会后将完全使用正则表达式\
合取请使用\\vee,析取请使用\\wedge,暂时不支持异或、同或, 表示“非”请使用/nor\
样例:\
( P \leftarrow Q ) \\vee R \\wedge P"
return rules
def searchBracket(self):
first="("
second=")"
targetList=list(self.target.split(" "))
countF=targetList.count(first)
countS=targetList.count(second)
if countS!=countF:
self.errors.append('wrong')
return "括号没有封闭"
else:
self.errors.append(True)
return True
def findWrongChar(self):
exceptions=[
"\\vee )",
"( \\vee",
"\\wedge )",
"( \\bigwedge",
"/nor )",#这里可能还需要改动
]
for i in exceptions:
if self.target.find(i)!=-1:
self.errors.append('wrong')
return "语法错误,请检查一下符号位置"
self.errors.append(True)
return True
def findWrongArrow(self):
exceptions=[
"\\rightarrow )",
"( \\rightarrow",
"\\leftarrow )",
"( \\leftarrow",
"( \\leftrightarrow",
"\\leftrightarrow )"
]
for i in exceptions:
if self.target.find(i)!=-1:
self.errors.append("Wrong")
return "检查一下箭头语法是否有错"
self.errors.append(True)
return True
def searchConnectAlpha(self):
pureAlpha=r"[A-Z] [A-Z]"
norAlpha=r"[A-Z] /nor [A-Z]"
connectAl=re.compile(pureAlpha)
resultAlpha=re.search(connectAl,self.target,flags=0)
norAlpha=re.search(norAlpha,self.target,flags=0)
if resultAlpha is None and norAlpha is None:
self.errors.append(True)
return True
else:
self.errors.append("wrong")
return "命题之间确失联结词"
def searchConnectConjuction(self):
patterns=[
r"\\.\w+\s{1}\\",#两个或者多个连续的联结词
r"/nor.\\",#非的符号和联结词写在一起,这是一种情况
r"\\.\w+./nor\s{1}\\",#多个符号之间出现/nor的情况
]
for item1 in patterns:
eachResult=re.search(re.compile(item1), self.target, flags=0)
if eachResult is not None:
self.errors.append("wrong")
return "存在联结词连续性错误"
self.errors.append(True)
return True
def findBlankBracket(self):
blankBracket=re.compile(r"\( \)")
result=re.search(blankBracket,self.target)
if result is not None:
self.errors.append("wrong")
return "括号中缺少表达"
self.errors.append(True)
return True
def findBracketFirSec(self):
targetList=self.target.split(" ")
firstBrack=0
secondBrack=0
falg=0
# if targetList[-1].find('\\') is not None:
# self.errors.append('wrong')
# falg=1
for item in targetList:
if item== "(" :
firstBrack+=1
elif item ==")":
secondBrack+=1
if firstBrack<secondBrack:
self.errors.append('wrong')
return "存在不正确的括号"
self.errors.append(True)
return True
#上面这个方法用于判断括号的个数和方向
def findGap(self):
gapBrack=r"\) \("#中途出现无效括号
norGap=r"\) /nor \("
result=re.search(gapBrack,self.target,flags=0)
norGAp=re.search(norGap,self.target,flags=0)
if result is not None or norGAp is not None:
self.errors.append('wrong')
return "括号之间缺少联结词"
else:
self.errors.append(True)
return True
def norText(self):
searchNor=r"[A-Z] /nor"
behindNor=r'\) /nor'
result=re.search(searchNor,self.target,flags=0)
result2=re.search(behindNor,self.target,flags=0)
if result is not None or result2 is not None:
self.errors.append('wrong')
return "命题变元后面不能出现‘非’"
else:
self.errors.append(True)
return True
def linkWord(self):
endWord=self.target.split(" ")
pat=r'\\.*'
end=re.search(pat,endWord[-1])
if end is not None:
self.errors.append('wrong')
return "结尾存在多余联结词"
else:
self.errors.append(True)
return True
def outcome(self)->list:
answerList=[]
if self.findGap() is not True:
answerList.append(self.findGap())
if self.findBracketFirSec() is not True:
answerList.append(self.findBracketFirSec())
if self.findBlankBracket() is not True:
answerList.append(self.findBlankBracket())
if self.searchBracket() is not True:
answerList.append(self.searchBracket())
if self.findWrongArrow() is not True:
answerList.append(self.findWrongArrow())
if self.findWrongChar() is not True:
answerList.append(self.findWrongChar())
if self.searchConnectAlpha() is not True:
answerList.append(self.searchConnectAlpha())
if self.searchConnectConjuction() is not True:
answerList.append(self.searchConnectConjuction())
if self.norText() is not True:
answerList.append(self.norText())
if self.linkWord() is not True:
answerList.append(self.linkWord())
elif 'wrong' not in self.errors:
endLine="这是一个合式公式"
answerList.append(endLine)
if "这是一个合式公式" not in answerList:
answerList.append("这不是一个合式公式")
return answerList