【问题标题】:Trying to print 3 results into a table using re尝试使用 re 将 3 个结果打印到表中
【发布时间】:2018-09-01 13:25:01
【问题描述】:

我有一个有效的代码,直到我将熵部分添加到它。现在它在打印行上给了我一个无效的语法错误。怎么会?

import nltk, math, re, numpy
from nltk import word_tokenize
from nltk.tokenize import RegexpTokenizer

def entropy(labels):
    freqdist = nltk.FreqDist(labels)
    probs = [freqdist.freq(1) for l in freqdist]
    return -sum(p * math.log(p,2) for p in probs)

def sents():
    fileObj = open('1865-Lincoln.txt', 'r')
    text = fileObj.read()
    tokens = nltk.sent_tokenize(text)
    for name in tokens:
        words = ' '.join(name.split()[:4])
        count = len(name.split())
        entro = entropy(len(name.split())
        print('{:<35} {:^15} {:>15}'.format(words, count, entro))

【问题讨论】:

    标签: python regex math nltk


    【解决方案1】:

    上一行缺少右括号:

    entro = entropy(len(name.split()))
    

    【讨论】:

    • 天哪。我现在几乎想删除它。我不敢相信我没有看到。谢谢你的眼睛。
    • 我实际上继续编辑它,因为运行它时出现了其他问题,但这更像是一个数学问题。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2015-06-18
    • 2015-06-04
    • 2022-07-15
    • 2020-04-12
    • 2013-08-14
    • 1970-01-01
    相关资源
    最近更新 更多