sys.path.insert(0, os.path.join(os.path.dirname(__file__), "..", "..")) from pattern.search import search, taxonomy, Classifier from pattern.en import parsetree # The search module includes a Taxonomy class # that can be used to define semantic word types. # For example, consider that you want to extract flower names from a text. # This would make search patterns somewhat unwieldy: # search("rose|lily|daisy|daffodil|begonia", txt). # A better approach is to use the taxonomy: for flower in ("rose", "lily", "daisy", "daffodil", "begonia"): taxonomy.append(flower, type="flower") print taxonomy.children("flower") print taxonomy.parents("rose") print taxonomy.classify("rose") # Yields the most recently added parent. print # Taxonomy terms can be included in a pattern by using uppercase: t = parsetree("A field of white daffodils.", lemmata=True) m = search("FLOWER", t) print t print m print # Another example: taxonomy.append("chicken", type="food") taxonomy.append("chicken", type="bird") taxonomy.append("penguin", type="bird")
from pattern.search import search, taxonomy for f in ('rose', 'lily', 'daisy', 'daffodil', 'begonia'): taxonomy.append(f, type='flower') for f in ('flower', 'tree'): taxonomy.append(f, type='plant') t = parsetree('A field of daffodils is white.', lemmata=True) print search('PLANT', t) taxonomy.parents('daffodil', recursive=True) taxonomy.children('plant', recursive=False) #def taxonomy_normalize(sentence): # bp_match = search('BEAUTY_PARTS', parsetree(sentence, lemmata=True)) # facial_match = search('MAKEUP', parsetree(sentence, lemmata=True)) # feet_match = search('FEET', parsetree(sentence, lemmata=True)) # body_match = search('BODY', parsetree(sentence, lemmata=True)) # # matches = [ [ 'BEAUTY_PARTS-'+word.lemma for word in m] for m in bp_match ] \ # + [ [ 'MAKEUP-'+word.lemma for word in m] for m in facial_match ] \ # + [ [ 'FEET-'+word.lemma for word in m] for m in feet_match ] \ # + [ [ 'BODY-'+word.lemma for word in m] for m in body_match ] # # return matches
import os, sys; sys.path.append(os.path.join("..", "..", "..")) from pattern.search import Pattern, Constraint, Classifier, taxonomy from pattern.en import Sentence, parse # The search module includes a Taxonomy class # that can be used to define semantic word types. # For example, consider that you want to extract flower names from a text. # This would make patterns somewhat unwieldy, e.g.: # Pattern.fromstring("rose|lily|daisy|daffodil|begonia"). # A better approach is to use the taxonomy: for flower in ("rose", "lily", "daisy", "daffodil", "begonia"): taxonomy.append(flower, type="flower") print taxonomy.children("flower") print taxonomy.parents("rose") print taxonomy.classify("rose") # Yields the most recently added parent. print # Taxonomy terms can be included in a pattern: p = Pattern([Constraint(taxa=["flower"])]) # or p = Pattern.fromstring("FLOWER") s = Sentence(parse("A field of white daffodils.", lemmata=True)) m = p.search(s) print s print m print from pattern.search import search
for f in ('reflect', 'bank'): taxonomy.append(f, type='angle') for f in ('bank', 'financial-institution'): taxonomy.append(f, type='finance') t = parsetree('A field of daffodils is white.', lemmata=True) print search('PLANT', t) taxonomy.parents('daffodil', recursive=True) taxonomy.children('plant', recursive=False) taxonomy.classify('bank') from pattern.en import wordnet a = wordnet.synsets('tone')[4] b = wordnet.synsets('color')[0] wordnet.similarity(a,b)