【发布时间】:2021-03-13 13:06:04
【问题描述】:
我有一些文本或一个大文件,我需要使用 NLTK 和 Pyspark 来计算 POS 标签的数量。我找不到导入文本文件的方法,所以我尝试添加一个短字符串,但失败了。
计数线需要合并pyspark。
##textfile = sc.textfile('')
##or
##textstring = """This is just a bunch of words to use for this example. John gave ##them to me last night but Kim took them to work. Hi Stacy. ###'''URL:http://example.com'''"""
tstring = sc.parallelize(List(textstring)).collect()
TOKEN_RE = re.compile(r"\b[\w']+\b")
dropURL=text.filter(lambda x: "URL" not in x)
words = dropURL.flatMap(lambda dropURL: dropURL.split(" "))
nltkwords = words.flatMap(lambda words: nltk.tag.pos_tag(nltk.regexp_tokenize(words, TOKEN_RE)))
#word_counts =nltkwords.map(lambda nltkwords: (ntlkwords,1))
nltkwords.take(50)
【问题讨论】:
-
我会推荐使用 Spark NLP 而不是 NLTK
标签: python apache-spark pyspark nltk rdd