title: How to get the location names(only) using Named Entity Recognition?
body: <p>I am working on a project where I need to extract the <strong>locations</strong> in a given text file.
  I tried the Named Entity Recognition example given <a href="https://stackoverflow.com/questions/36255291/extract-city-names-from-text-using-python/36255377#36255377">here</a>. The code snippet of this is given below.
  But here it outputs all the three entities; <strong>names, locations, and organizations</strong>. Is there any solution to extract only the locations using python?</p>
  
  <pre><code> import nltk
  
  def extract_entity_names(t):
      entity_names = []
  
      if hasattr(t, 'label') and t.label:
          if t.label() == 'NE':
              entity_names.append(' '.join([child[0] for child in t]))
          else:
              for child in t:
                  entity_names.extend(extract_entity_names(child))
  
      return entity_names
  
  with open('sample.txt', 'r') as f:
      for line in f:
          sentences = nltk.sent_tokenize(line)
          tokenized_sentences = [nltk.word_tokenize(sentence) for sentence in sentences]
          tagged_sentences = [nltk.pos_tag(sentence) for sentence in tokenized_sentences]
          chunked_sentences = nltk.ne_chunk_sents(tagged_sentences, binary=True)
  
          entities = []
          for tree in chunked_sentences:
              entities.extend(extract_entity_names(tree))
  
          print(entities)
  </code></pre>