nltk.word_tokenize 应用于路径

nltk.word_tokenize apply at path

您好,有没有办法将此代码应用到我的文件路径?而不是“现在完全不同”这句话?

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

假设您有一个 .txt 文件,您可以直接打开并阅读其中的内容。

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'), ('.', '.')]