-1
def fetch_name(resume_text):
  tokenized_sentences = nltk.sent_tokenize(resume_text)
  for sentence in tokenized_sentences:
    for chunk in nltk.ne_chunk(nltk.pos_tag(nltk.word_tokenize(sentence), tagset='universal')):
      if hasattr(chunk, 'label'):# and chunk.label() == 'PERSON':
        chunk = chunk[0]
      (name, tag) = chunk
      if tag == 'NOUN':
          #print(name)
          #z.extend(name)
          return name             

path = '/home/python/resumes/*.txt'
files = glob.glob(path)
for n in files:
    with io.open(n,'r') as f:
        data=f.read()
        print(fetch_name(data))

Following is the output that i obtain from the code that follows. I need the output in a form of list ['Sharayu','neeraj'.....]

SHARAYU
Neeraj
Gregory
MEhul
Shashank
Sandeep
Atul
ADITYA
Manoj
SAGAR
Shreya
Shivkumar Birnale
  • 2,906
  • 1
  • 11
  • 17
sharayu salunkhe
  • 119
  • 1
  • 11

4 Answers4

0

Do it like this:

path = '/home/python/resumes/*.txt'
files = glob.glob(path)
data_list = []
for n in files:
    with io.open(n,'r') as f:
        data=f.read()
        data_list.append(fetch_name(data))
    print(data_list)
Nitin Pawar
  • 168
  • 1
  • 10
0

Using a list() would do that as below:

namelist=list()    
for n in files:
    with io.open(n,'r') as f:
        data=f.read()
        namelist.append(fetch_name(data))
print(namelist)
Shivkumar Birnale
  • 2,906
  • 1
  • 11
  • 17
0

If you want your function to return a list instead, you can initialize a list and append the names to the list:

def fetch_name(resume_text):
    names = []
    tokenized_sentences = nltk.sent_tokenize(resume_text)
    for sentence in tokenized_sentences:
        for chunk in nltk.ne_chunk(nltk.pos_tag(nltk.word_tokenize(sentence), tagset='universal')):
            if hasattr(chunk, 'label'):  # and chunk.label() == 'PERSON':
                chunk = chunk[0]
            (name, tag) = chunk
            if tag == 'NOUN':
                # print(name)
                # z.extend(name)
                names.append(name)
    return names
blhsing
  • 91,368
  • 6
  • 71
  • 106
0

You can use the append method to add an entry to a list.

resumes = []
for path in glob.glob('/home/python/resumes/*.txt'):
    with open(path, 'r', encoding='utf-8') as f:
        resumes.append(fetch_name(f.read()))
print(resumes)
Jack Taylor
  • 5,588
  • 19
  • 35
  • it returns me a list like this [u'SHARAYU', u'Neeraj', u'Gregory', u'MEhul', u'Shashank', u'Sandeep', u'Atul', u'ADITYA', u'Manoj', u'SAGAR', u'Shreya'] i want the list as ['SHARAYU', 'Neeraj'.....] – sharayu salunkhe Oct 15 '18 at 12:26
  • Ah, you're using Python 2. I'd recommend switching to Python 3 - then the `u` prefix will go away, as Python 3 uses Unicode by default. – Jack Taylor Oct 15 '18 at 12:32
  • can you suggest something that can work with python 2 – sharayu salunkhe Oct 15 '18 at 12:37
  • If you use Python 2, then using the Unicode strings like `[u'SHARAYU', u'Neeraj', ...]` is better practice than using byte strings like `['SHARAYU', 'Neeraj', ...]`. If you have any non-Ascii characters in any of your files, then using Unicode strings will make things easier. – Jack Taylor Oct 15 '18 at 12:46