【发布时间】:2016-10-18 15:32:28
【问题描述】:
我希望计算每个句子的单词数,计算每个句子的平均单词数,并将该信息放入 CSV 文件中。这是我到目前为止所拥有的。我可能只需要知道如何计算句号之前的单词数。我也许可以从那里弄清楚。
#Read the data in the text file as a string
with open("PrideAndPrejudice.txt") as pride_file:
pnp = pride_file.read()
#Change '!' and '?' to '.'
for ch in ['!','?']:
if ch in pnp:
pnp = pnp.replace(ch,".")
#Remove period after Dr., Mr., Mrs. (choosing not to include etc. as that often ends a sentence although in can also be in the middle)
pnp = pnp.replace("Dr.","Dr")
pnp = pnp.replace("Mr.","Mr")
pnp = pnp.replace("Mrs.","Mrs")
【问题讨论】:
-
一旦你得到了一个句子列表,这很简单,但是识别句子并不是很简单,因为你不能只在
.上拆分:stackoverflow.com/questions/4576077/…