gpt4 book ai didi

python - 每个句子有多少个单词,如何得到意思?

转载 作者:行者123 更新时间:2023-12-04 13:37:58 29 4
gpt4 key购买 nike

关闭。这个问题需要更多focused .它目前不接受答案。












想改善这个问题吗?更新问题,使其仅关注一个问题 editing this post .

去年关闭。




Improve this question




我有一个函数,想返回 (a) 每个句子的单词数和 (b) 元组列表中每个句子的平均单词长度。我可以得到(a)。对于(b)我可以得到每个句子的总字符数,但不是平均值。

我已经查看了一些帖子 ( this ,
that
another ) 但我无法理解这最后一块。

我已经将一些失败的尝试注释掉了。

import statistics

def sentence_num_and_mean(text):
""" Output list of, per sentence, number of words and mean length of words """
# Replace ! and ? with .
for ch in ['!', '?']:
if ch in text:
text = text.replace(ch, '.')

# Number of words per sentence
num_words_per_sent = [len(element) for element in (element.split() for element in text.split("."))]

# Mean length of words per sentence

# This gets sum of characters per sentence, so on the right track
mean_len_words_per_sent = [len(w) for w in text.split('.')]

# This gives me "TypeError: unsupported operand type(s) for /: 'int' and 'list'" error
# when trying to get the denominator for the mean
# A couple efforts
#mean_len_words_per_sent = sum(num_words_per_sent) / [len(w) for w in text.split('.')]
#mean_len_words_per_sent = [(num_words_per_sent)/statistics.mean([len(w) for w in text.split()])]

# Return list zipped together
return list(zip(num_words_per_sent, mean_len_words_per_sent))

驱动程序:
split_test = "First sentence ends with a period. Next one ends with a question mark? Another period. Then exclamation! Blah blah blah"
func_test = sentence_num_and_mean(split_test)
print(split_test)
print(func_test)

哪个打印
First sentence ends with a period. Next one ends with a question mark? Another period. Then exclamation! Blah blah blah
[(6, 33), (7, 35), (2, 15), (2, 17), (3, 15)]

一方面,我需要去掉空格和句点,但暂时忽略这一点,如果我正确地进行了简单的数学运算,它应该是:
[(6, 5.5), (7, 5), (2, 7.5), (2, 8.5), (3, 5)]

最佳答案

更好的变量名称可能会帮助您阐明如何表达这些想法。text.split('.')给你什么?句子列表 (str)。
如果在名为 sentence 的变量中有一个句子然后 sentence.split()给你一个单词列表(str)。考虑到这些,这很容易编写。
mean_len_words_per_sent = [statistics.mean(len(word) for word in sentence.split()) for sentence in text.split('.')]

关于python - 每个句子有多少个单词,如何得到意思?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/60711844/

29 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com