【发布时间】:2016-06-08 05:14:39
【问题描述】:
我对 linux 和 python 都不是很熟悉。我正在学习这门课,该课程在 python 上具有倒排索引程序的示例代码。我想知道如何运行和测试代码。这是提供给我的代码。
这是映射文件的代码。 (inverted_index_map.py)
import sys
for line in sys.stdin:
#print(line)
key, value = line.split('\t', 1)
for word in value.strip().split():
if len(word) <=5 and len(word) >= 3:
print '%s\t%s' % (word, key.split(':', 1)[0]) #what are we emitting?
这是reduce程序的代码。 (inverted_index_reduce.py)
import sys
key = None
total = ''
for line in sys.stdin:
k, v = line.split('\t', 1)
if key == k:
total += v.strip() #what are we accumulating?
else:
if key:
print '%s\t%s' % (key, total) #what are we printing?
key = k
total = v
if key:
print '%s\t%s' % (key, total) #what are we printing?
它不是可执行文件,所以我尝试了
chmod +x inverted_index_map.py
然后我尝试运行程序:
./inverted_index_map.py testfilename.txt
但我不确定程序是否正在等待来自键盘的某种输入或其他东西。所以我的问题是如何测试这段代码并查看结果?我对python真的不熟悉。
【问题讨论】:
标签: python linux mapreduce inverted-index