{"action":"created","issue":{"url":"https://api.github.com/repos/honnibal/spaCy/issues/105","labels_url":"https://api.github.com/repos/honnibal/spaCy/issues/105/labels{/name}","comments_url":"https://api.github.com/repos/honnibal/spaCy/issues/105/comments","events_url":"https://api.github.com/repos/honnibal/spaCy/issues/105/events","html_url":"https://github.com/honnibal/spaCy/issues/105","id":108501107,"number":105,"title":"Matcher sample code","user":{"login":"sdenning","id":5341007,"avatar_url":"https://avatars.githubusercontent.com/u/5341007?v=3","gravatar_id":"","url":"https://api.github.com/users/sdenning","html_url":"https://github.com/sdenning","followers_url":"https://api.github.com/users/sdenning/followers","following_url":"https://api.github.com/users/sdenning/following{/other_user}","gists_url":"https://api.github.com/users/sdenning/gists{/gist_id}","starred_url":"https://api.github.com/users/sdenning/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/sdenning/subscriptions","organizations_url":"https://api.github.com/users/sdenning/orgs","repos_url":"https://api.github.com/users/sdenning/repos","events_url":"https://api.github.com/users/sdenning/events{/privacy}","received_events_url":"https://api.github.com/users/sdenning/received_events","type":"User","site_admin":false},"labels":[],"state":"open","locked":false,"assignee":null,"milestone":null,"comments":2,"created_at":"2015-09-26T22:56:28Z","updated_at":"2015-09-27T15:19:19Z","closed_at":null,"body":"Can you provide a code snippet of how to use the NER matcher?"},"comment":{"url":"https://api.github.com/repos/honnibal/spaCy/issues/comments/143567188","html_url":"https://github.com/honnibal/spaCy/issues/105#issuecomment-143567188","issue_url":"https://api.github.com/repos/honnibal/spaCy/issues/105","id":143567188,"user":{"login":"sdenning","id":5341007,"avatar_url":"https://avatars.githubusercontent.com/u/5341007?v=3","gravatar_id":"","url":"https://api.github.com/users/sdenning","html_url":"https://github.com/sdenning","followers_url":"https://api.github.com/users/sdenning/followers","following_url":"https://api.github.com/users/sdenning/following{/other_user}","gists_url":"https://api.github.com/users/sdenning/gists{/gist_id}","starred_url":"https://api.github.com/users/sdenning/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/sdenning/subscriptions","organizations_url":"https://api.github.com/users/sdenning/orgs","repos_url":"https://api.github.com/users/sdenning/repos","events_url":"https://api.github.com/users/sdenning/events{/privacy}","received_events_url":"https://api.github.com/users/sdenning/received_events","type":"User","site_admin":false},"created_at":"2015-09-27T15:19:19Z","updated_at":"2015-09-27T15:19:19Z","body":"I think I figured it out, see below.\r\n\r\nfrom __future__ import unicode_literals\r\n\r\nimport spacy.en\r\nfrom spacy.en import English\r\nfrom spacy.matcher import *\r\n\r\n\r\nartists = []\r\nbands = []\r\nalbums = []\r\ntracks = []\r\n\r\nnlp = English()\r\n\r\ndoc = nlp('One of the most popular bands of all time is U2; Bono and The Edge are simply amazing. ' +\r\n          'My favorite of all their albums is Songs of Innocence though The Joshua Tree is a close second. ' +\r\n          'Every breaking wave, Song for Someone, and California are some of my favorite songs.')\r\n\r\n# create the patterns for the matcher\r\npatterns = {\r\n\r\n  'Band':['BAND',{},[[{'orth': 'U2'}]]],\r\n\r\n  'Albums':[\r\n    'ALBUM',{},[\r\n      [{'lower': 'songs'},{'lower': 'of'},{'lower': 'innocence'}],\r\n      [{'lower': 'the'},{'lower': 'joshua'},{'lower': 'tree'}]\r\n    ]\r\n  ],\r\n\r\n  'Tracks':  [\r\n    'TRACK',{},[\r\n      [{'lower': 'california'}],\r\n      [{'lower': 'cedarwood'},{'lower': 'road'}],\r\n      [{'lower': 'every'},{'lower': 'breaking'},{'lower': 'wave'}],\r\n      [{'lower': 'iris'}],\r\n      [{'lower': 'the'},{'lower': 'miricle'}], \r\n      [{'lower': 'raised'},{'lower': 'by'},{'lower': 'wolves'}],\r\n      [{'lower': 'sleep'},{'lower': 'like'},{'lower': 'a'},{'lower': 'baby'},{'lower': 'tonight'}],  \r\n      [{'lower': 'song'},{'lower': 'for'},{'lower': 'someone'}],\r\n      [{'lower': 'this'},{'lower': 'is'},{'lower': 'where'},{'lower': 'yo'},{'lower': 'can'},{'lower': 'reach'},{'lower': 'me'},{'lower': 'now'}], \r\n      [{'lower': 'the'},{'lower': 'troubles'}],\r\n      [{'lower': 'volcano'}]\r\n    ]\r\n  ]\r\n}\r\n\r\n#create the matcher\r\nmatcher = Matcher(doc.vocab, patterns)\r\n\r\n# let's add a new pattern to the matcher\r\nkey = 'Artists'\r\netype = 'ARTIST'\r\nattrs = {}\r\nspecs = [[{'orth': 'Bono'}],[{'lower': 'the'},{'orth': 'Edge'}]]\r\n\r\nmatcher.add(key, etype, attrs, specs) \r\n\r\n# get the matcher to look at the doc and update the ents\r\nmatcher.__call__(doc)\r\n\r\nfor ent in doc.ents:\r\n\r\n  if ent.label_ == 'BAND':\r\n    bands.append(ent.orth_)\r\n  elif ent.label_ == 'ARTIST':\r\n    artists.append(ent.orth_)\r\n  elif ent.label_ == 'ALBUM':\r\n    albums.append(ent.orth_)\r\n  elif ent.label_ == 'TRACK': \r\n    tracks.append(ent.orth_) \r\n\r\nprint 'bands: ' + str(bands)\r\nprint 'artists: ' + str(artists)\r\nprint 'albums: ' + str(albums)\r\nprint 'tracks: ' + str(tracks)\r\n"}}