File: 05-multiple.py

package info (click to toggle)
python-pattern 2.6%2Bgit20150109-3
  • links: PTS, VCS
  • area: main
  • in suites: buster
  • size: 78,672 kB
  • sloc: python: 53,865; xml: 11,965; ansic: 2,318; makefile: 94
file content (30 lines) | stat: -rw-r--r-- 857 bytes parent folder | download | duplicates (2)
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
import os, sys; sys.path.insert(0, os.path.join(os.path.dirname(__file__), "..", ".."))

from pattern.search import search
from pattern.en     import parsetree

# Constraints ending in "+" match one or more words.
# Pattern.search() uses a "greedy" approach: 
# it will attempt to match as many words as possible.

# The following pattern means:
# one or more words starting with "t", 
# followed by one or more words starting with "f".
t = parsetree("one two three four five six")
m = search("t*+ f*+", t)
print t
print m
print

for w in m[0].words:
    print w, "matches", m[0].constraint(w)

# "*" matches each word in the sentence.
# This yields a list with a Match object for each word.
print
print "* =>",  search("*", t)

# "*+" matches all words.
# This yields a list with one Match object containing all words.
print
print "*+ =>", search("*+", t)