MCPcopy Index your code
hub / github.com/clips/pattern / fromstring

Method fromstring

pattern/text/search.py:691–740  ·  view source on GitHub ↗

Returns a new Pattern from the given string. Constraints are separated by a space. If a constraint contains a space, it must be wrapped in [].

(cls, s, *args, **kwargs)

Source from the content-addressed store, hash-verified

689
690 @classmethod
691 def fromstring(cls, s, *args, **kwargs):
692 """ Returns a new Pattern from the given string.
693 Constraints are separated by a space.
694 If a constraint contains a space, it must be wrapped in [].
695 """
696 s = s.replace("\(", "&lparen;")
697 s = s.replace("\)", "&rparen;")
698 s = s.replace("\[", "[")
699 s = s.replace("\]", "]")
700 s = s.replace("\{", "&lcurly;")
701 s = s.replace("\}", "&rcurly;")
702 p = []
703 i = 0
704 for m in re.finditer(r"\[.*?\]|\(.*?\)", s):
705 # Spaces in a range encapsulated in square brackets are encoded.
706 # "[Windows Vista]" is one range, don't split on space.
707 p.append(s[i:m.start()])
708 p.append(s[m.start():m.end()].replace(" ", "&space;")); i=m.end()
709 p.append(s[i:])
710 s = "".join(p)
711 s = s.replace("][", "] [")
712 s = s.replace(")(", ") (")
713 s = s.replace("\|", "⊢")
714 s = re.sub(r"\s+\|\s+", "|", s)
715 s = re.sub(r"\s+", " ", s)
716 s = re.sub(r"\{\s+", "{", s)
717 s = re.sub(r"\s+\}", "}", s)
718 s = s.split(" ")
719 s = [v.replace("&space;"," ") for v in s]
720 P = cls([], *args, **kwargs)
721 G, O, i = [], [], 0
722 for s in s:
723 constraint = Constraint.fromstring(s.strip("{}"), taxonomy=kwargs.get("taxonomy", TAXONOMY))
724 constraint.index = len(P.sequence)
725 P.sequence.append(constraint)
726 # Push a new group on the stack if string starts with "{".
727 # Parse constraint from string, add it to all open groups.
728 # Pop latest group from stack if string ends with "}".
729 # Insert groups in opened-first order (i).
730 while s.startswith("{"):
731 s = s[1:]
732 G.append((i, [])); i+=1
733 O.append([])
734 for g in G:
735 g[1].append(constraint)
736 while s.endswith("}"):
737 s = s[:-1]
738 if G: O[G[-1][0]] = G[-1][1]; G.pop()
739 P.groups = [g for g in O if g]
740 return P
741
742 def search(self, sentence):
743 """ Returns a list of all matches found in the given sentence.

Callers 11

__init__Method · 0.45
compileFunction · 0.45
test_fromstringMethod · 0.45
test_matchMethod · 0.45
test_fromstringMethod · 0.45
test_matchMethod · 0.45
test_searchMethod · 0.45
test_convergenceMethod · 0.45
test_group_orderingMethod · 0.45
02-constraint.pyFile · 0.45
08-web.pyFile · 0.45

Calls 7

lenFunction · 0.85
stripMethod · 0.80
appendMethod · 0.45
startMethod · 0.45
splitMethod · 0.45
getMethod · 0.45
popMethod · 0.45

Tested by 7

test_fromstringMethod · 0.36
test_matchMethod · 0.36
test_fromstringMethod · 0.36
test_matchMethod · 0.36
test_searchMethod · 0.36
test_convergenceMethod · 0.36
test_group_orderingMethod · 0.36