简体   繁体   中英

nltk.word_tokenize apply at path

Hi, Is there a method to apply this code to my file path? instead of the sentence "And now something completely different"?

import nltk
text = nltk.word_tokenize("And now for something completely different")
print(nltk.pos_tag(text))

Assuming you have a.txt file, you could just open and read the content of it.

text.txt

This file is for testing purposes.

python file

import nltk

file = open("text.txt", "r")
for line in file:
  text = nltk.word_tokenize(line)
  print(nltk.pos_tag(text))
  # output
  # [('This', 'DT'), ('file', 'NN'), ('is', 'VBZ'), ('for', 'IN'), ('testing', 'VBG'), ('purposes', 'NNS'), ('.', '.')]

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM