无法弄清楚如何运行此Python脚本

时间:2020-04-15 13:36:20

标签: python

有人可以向我解释(我没有编程能力)如何正确使用此脚本(链接:https://github.com/dumbmatter/find-repeated-words)吗?基本上,它应该通过将一个文本文件作为输入并输出一个HTML文件来工作,该文件的单词被反复使用在一起并突出显示,但是当我运行它(我安装了Pyzo)时,我收到消息:“ SyntaxError:无效语法”。我不知道Python在说什么,我只能假设问题与输入文件有关。

router-view

1 个答案:

答案 0 :(得分:1)

Python对代码的格式非常敏感,因此您不能在python所不期望的位置中断或缩进行。只看第一行:

<template>
  <div id="app">
    <div id="nav">
      <router-link to="/">Home</router-link> |
      <router-link to="/statistics">Statistics</router-link>
    </div>

    <keep-alive>
        <router-view :changesA.sync="changesA" :changesB.sync="changesB" />
    </keep-alive>
  </div>
</template>

应分为3行:

CODE:

#!/usr/bin/env python

import sys from string import punctuation from operator import itemgetter

# Check command line inputs if len(sys.argv) == 1:
    print 'Pass the input text file as the first argument.'
    sys.exit() elif len(sys.argv) == 2:
    infile = sys.argv[1]
    outfile = '%s.html' % (infile.split('.')[0],) else:
    infile = sys.argv[1]
    outfile = sys.argv[2]

print infile, outfile

N = 10 words = {} # Dict of word frequencies pos = {} # Dict of word positions scores = [] # List of word repeatedness scores articles = ['the', 'a', 'of', 'and', 'in', 'et', 'al'] # Common articles to ignore

# Build lists

words_gen = (word.strip(punctuation).lower() for line in open(infile)
                                             for word in line.split())

i = 0 for word in words_gen:
    words[word] = words.get(word, 0) + 1

    # Build a list of word positions
    if words[word] == 1:
        pos[word] = [i]
    else:
        pos[word].append(i)

    i += 1

# Calculate scores

words_gen = (word.strip(punctuation).lower() for line in open(infile)
                                             for word in line.split())

i = 0 for word in words_gen:
    scores.append(0)
#    scores[i] = -1 + sum([pow(2, -abs(d-i)) for d in pos[word]]) # The -1 accounts for the 2^0 for self words
    if word not in articles and len(word) > 2:
        for d in pos[word]:
            if d != i and abs(d-i) < 50:
                scores[i] += 1.0/abs(d-i)
    i += 1

scores = [score*1.0/max(scores) for score in scores] # Scale from 0 to 1

# Write colored output

f = open(outfile, 'w'); i = 0 for line in open(infile):
    for word in line.split():
        f.write('<span style="background: rgb(%i, 255, 255)">%s</span> ' % ((1-scores[i])*255, word))
        i += 1
    f.write('<br /><br />') f.close()

print 'Output saved to %s' % (outfile,)

您粘贴的代码中存在更多此类错误。我已经从链接下载了原始代码,并且工作正常。