-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathabdparser_cc.py
59 lines (44 loc) · 1.24 KB
/
abdparser_cc.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
"""
Abductive Logic File Parser
Typical usage:
listOfFacts = getFactsFromFile('abductive.pl')
"""
import re
PREDICATE_RE = r"[a-z]+\([a-z]+\)\.$"
def getLines(aFile):
"""
Returns list of file contents.
"""
with open(aFile) as f:
contents = f.readlines()
return contents
def getPredicatesFrom(aList):
"""
returns all valid predicates contained in a list of strings with newlines
>>> aList = ['one string\\n','one other string\\n','predicate(string).\\n']
>>> getPredicatesFrom(aList)
['predicate(string).']
NB that there are escape sequences in the aList example.
This is for docstring purposes and
Not part of the code itself
"""
return [l[:-1] for l in aList if containsFact(l)]
def containsFact(aStr):
"""
returns true if match
>>> True == containsFact('awef(awef).')
True
"""
if re.match(PREDICATE_RE,aStr): # NB re.match only rtrns if at beg of str (so no need for ^)
return True
else :
return False
def getFactsFromFile(aFile):
"""
Wrapper function for this module
"""
c = getLines(aFile)
c = getPredicatesFrom(c)
return c
import doctest
doctest.testmod() # auto validation of embedded docstrings