Extracting all Nouns from a text file using nltk

前端 未结 7 1848
清歌不尽
清歌不尽 2020-12-08 08:35

Is there a more efficient way of doing this? My code reads a text file and extracts all Nouns.

import nltk

File = open(fileName) #open file
lines = File.rea         


        
7条回答
  •  借酒劲吻你
    2020-12-08 08:50

    import nltk
    lines = 'lines is some string of words'
    tokenized = nltk.word_tokenize(lines)
    nouns = [word for (word, pos) in nltk.pos_tag(tokenized) if(pos[:2] == 'NN')]
    print (nouns)
    

    Just simplied abit more.

提交回复
热议问题