令牌有时也叫作标志,在从源读取文本时,有时我们需要找出有关所用单词的一些统计信息。 这使得有必要计算单词的数量以及计算给定文本中具有特定类型单词的行数。 在下面的示例中,我们展示了使用两种不同方法计算段落中单词的程序。假设这个示例文本中包含好莱坞电影的摘要。 读取文件 FileName = (“Path\GodFather.txt”)
with open(FileName, ‘r’) as file: lines_in_file = file.read() print lines_in_file Python 当运行上面的程序时,得到以下输出 - Vito Corleone is the aging don (head) of the Corleone Mafia Family. His youngest son Michael has returned from WWII just in time to see the wedding of Connie Corleone (Michael’s sister) to Carlo Rizzi. All of Michael’s family is involved with the Mafia, but Michael just wants to live a normal life. Drug dealer Virgil Sollozzo is looking for Mafia families to offer him protection in exchange for a profit of the drug money. He approaches Don Corleone about it, but, much against the advice of the Don’s lawyer Tom Hagen, the Don is morally against the use of drugs, and turns down the offer. This does not please Sollozzo, who has t
|