Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/regex/19.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python 如何避免在spark中将新行计算为单词?_Python_Regex_Apache Spark - Fatal编程技术网

Python 如何避免在spark中将新行计算为单词?

Python 如何避免在spark中将新行计算为单词?,python,regex,apache-spark,Python,Regex,Apache Spark,我试图在lorem ipsum上运行单词计数示例;i、 e.计算给定文本文件中的单词频率。作为分词规则,我想使用任何非字符的实例。我有以下python代码: import re from pyspark import SparkContext print "-----------------===========================-----------------" print "-----------------==========Staring==========-------

我试图在lorem ipsum上运行单词计数示例;i、 e.计算给定文本文件中的单词频率。作为分词规则,我想使用任何非字符的实例。我有以下python代码:

import re
from pyspark import SparkContext
print "-----------------===========================-----------------"
print "-----------------==========Staring==========-----------------"
print "-----------------===========================-----------------"
sc = SparkContext(appName = "simple app")

print "-----------------===========================-----------------"
print "-----------------==========Loaded file======-----------------"
print "-----------------===========================-----------------"
text_file = sc.textFile("lorem.txt")

print "-----------------===========================-----------------"
print "-----------------==========  Process  ======-----------------"
print "-----------------===========================-----------------"
counts = text_file.flatMap(lambda line: re.split(r'\W*', line.rstrip())) \
         .map(lambda word: (word, 1)) \
         .reduceByKey(lambda a, b: a + b) \
         .map(lambda (a,b): (b, a)) \
         .sortByKey(False)

output = counts.collect()
counts.saveAsTextFile("test.txt")
sc.stop()
for x in output:
    print (x[0], x[1])

它几乎像预期的那样工作。主要的问题是它计算新线路的数量。如果我理解正确,这是由于正则表达式的工作方式,但我找不到解决方法。我做错了什么?

请注意,
line.rstrip()
仅去除空白。但它可以采用
line.rstrip(badchars)
中的参数,该参数将剥离
badchars
中的所有内容

即使换行符与空话和其他垃圾一起进入RDD,您也可以通过向工作流中添加步骤来过滤掉它们
filter
为RDD的每个元素调用一个函数,并返回返回
true
的元素RDD

有两种方法可以将newline作为一个词去掉:

明确地寻找它

counts = text_file.flatMap(lambda line: re.split(r'\W*', line.rstrip())) \
         .filter(lambda word: word!="\n") \
         .map(lambda word: (word, 1)) \
         .reduceByKey(lambda a, b: a + b) \
         .map(lambda (a,b): (b, a)) \
         .sortByKey(False)
按字长>1个字符筛选

counts = text_file.flatMap(lambda line: re.split(r'\W*', line.rstrip())) \
         .filter(lambda word: len(word)>1) \
         .map(lambda word: (word, 1)) \
         .reduceByKey(lambda a, b: a + b) \
         .map(lambda (a,b): (b, a)) \
         .sortByKey(False)

你想达到什么目的…你想数一数所有的单词吗?我正在努力数一数单词的频率。但是,上面的代码将换行符视为一个单词。
lorem.txt
的内容是什么。您是否尝试过
\W+
@vks:。也尝试了
\W+
。同样的问题,您可以尝试使用lambda函数作为过滤器,以获得您想要的结果。第一个仅阻止完全等于“\n”的字。也许
“\n”不在word中
会是一个更好的测试?第二个将阻止短单词,包括一些实际单词,如“a”、“I”和“O”。