【发布时间】:2021-05-14 14:32:42
【问题描述】:
在向 Project Gutenberg 发送 get 请求后,我将完整的剧本 Macbeth 作为一个字符串
response = requests.get('https://www.gutenberg.org/cache/epub/2264/pg2264.txt')
full_text = response.text
macbeth = full_text[16648:]
我分了
words_raw = macbeth.split()
word_count = len(words_raw)
print("Macbeth contains {} words".format(word_count))
print("Here are some examples:", words_raw[400:460])
然后我去掉 all 标点符号并将字符串转换为 lower()
import string
punctuation = string.punctuation
words_cleaned = []
for word in words_raw:
# remove punctuation
word = word.strip(punctuation)
# make lowercase
word = word.lower()
words_cleaned.append(word)
print("Cleaned word examples:", words_cleaned[400:460])
但是,我不能去掉所有标点符号,因为我需要名称/缩短名称后面的句点作为字符即将说话的指示符。
课程节选
说话的角色由他们名字的(通常是缩写的)版本表示,后跟一个 . (句号)作为一行的第一件事。例如,当麦克白说话时,它以“Macb”开头。您需要修改处理标点符号的方式,因为您不能只删除所有标点符号
split( ) 后的原始数据切片
名称后跟粗体句点
麦克白包含 17737 个单词 以下是一些例子:['Gashes'、'cry'、'for'、'helpe'、'King.'、'So'、'well'、'thy'、'words'、'become'、'thee, ', 'as', 'thy', 'wounds,', 'they', 'smack', 'of', 'Honor', 'both:', 'Goe', 'get', 'him', 'Surgeons .', 'Enter', 'Rosse', 'and', 'Angus.', 'Who', 'comes', 'here?', 'Mal.', 'The', '值得”, 'Thane', 'of', 'Rosse', 'Lenox.', 'What', 'a', 'haste', 'lookes', 'through', 'his' ,“眼睛?”,“所以”,“应该”,“他”,“看起来”,“那个”,“看起来”,“到”,“说话”,“事情”,“奇怪”,“罗斯。 ','上帝','saue','the','King','King']
words_raw = macbeth.split()
word_count = len(words_raw)
print("Macbeth contains {} words".format(word_count))
print("Here are some examples:", words_raw[400:460])
我们知道“Malcolm”在他的名字出现后跟一个句号时正在说话(上面粗体中的“Mal.”),当他开始说话时“Lenox”也是如此(“Lenox.”)有时角色的名称被缩短,其他人使用全名,后面紧跟一个句点。
麦克白中最常见的名字列表
[“duncan”、“malcolm”、“donalbaine”、“macbeth”、“banquo”、“macduff”、“lenox”、“rosse”、“menteth”、“angus”、“cathnes”、“fleance” ", "seyward", "seyton", "boy", "lady", "messenger", "wife"]
目标
- 从上面的列表中识别所有名称和字符的缩短名称(如果缩短)
- 找到一个角色从哪里开始说话,用句号表示,然后从那里分开
这是我目前尝试过的
尝试隔离非字母数字
print(len(words_raw))
def extra(string):
return list(c for c in string if not c.isalnum() and not c.isspace())
weird = extra(macbeth)
weird
discard = []
for char in weird:
if char != '.':
discard.append(char)
print(len(weird))
print(len(discard))
print(discard)
revised_macbeth = []
for character in words_raw:
if not character in discard:
revised_macbeth.append(character)
print(len(revised_macbeth))
# for character in words_raw:
# if not character.isalnum():
# print("found: \'{}\'".format(character))
它的输出
17737
4788
3553
['?', ',', ',', '?', '-', "'", ',', "'", ',', '?', ',', '-', ':', ',', ',', ',', ',', ',', ',', ',', '?', ',', ',', ',', "'", ':', ';', ',', ',', ',', ',', ',', ':', '(', ',', ')', "'", ',', ',', "'", ':', "'", ':', '(', ')', ',', ',', "'", '(', ')', "'", ',', "'", ':', "'", ',', ',', "'", "'", ',', "'", ',', "'", ',', ',', ':', ',', "'", ',', ':', ',', ',', ',', "'", ',', "'", ',', ',', ',', ',', ',', "'", ',', '?', ',', ',', ';', ',', ':', ',', '-', "'", ',', ':', ',', ',', ':', ',', ',', ',', ':', '?', '?', ',', "'", ',', '?', ',', ',', ',', ',', ',', ',', ',', ',', "'", ',', ',', '-', ',', ',', "'", ',', ':', ',', ',', ',', ':', ',', ',', ',', ',', ':', ',', ',', ',', '?', ',', '?', ',', ',', '&', ',', ':', ',', ',', ',', '-', "'", ',', "'", "'", ':', ',', ',', ',', ',', "'", ',', ',', ',', "'", "'", '-', ':', '-', ':', ':', "'", ',', ',', ',', ',', ':', ',', '-', ',', ',', ',', ',', ':', ',', ',', ',', ',', ',', ',', ',', ',', ',', ',', "'", "'", "'", '?', ',', "'", ',', ',', "'", "'", "'", ',', "'", '?', ',', '?', ',', ':', ',', ':', '?', ',', ',', ',', ',', ',', '?', "'", "'", ',', '?', ',', ',', ',', ':', ',', ',', ',', ',',
比较
print(macbeth)
The Tragedie of Macbeth
Actus Primus. Scoena Prima.
Thunder and Lightning. Enter three Witches.
1. When shall we three meet againe?
In Thunder, Lightning, or in Raine?
2. When the Hurley-burley's done,
When the Battaile's lost, and wonne
3. That will be ere the set of Sunne
1. Where the place?
2. Vpon the Heath
3. There to meet with Macbeth
1. I come, Gray-Malkin
print(revised_macbeth)
['The', 'Tragedie', 'of', 'Macbeth', 'Actus', 'Primus.', 'Scoena', 'Prima.', 'Thunder', 'and', 'Lightning.', 'Enter', 'three', 'Witches.', '1.', 'When', 'shall', 'we', 'three', 'meet', 'againe?', 'In', 'Thunder,', 'Lightning,', 'or', 'in', 'Raine?', '2.', 'When', 'the', "Hurley-burley's", 'done,', 'When', 'the', "Battaile's", 'lost,', 'and', 'wonne', '3.', 'That', 'will', 'be', 'ere', 'the', 'set', 'of', 'Sunne', '1.', 'Where', 'the', 'place?', '2.', 'Vpon', 'the', 'Heath', '3.', 'There', 'to', 'meet', 'with', 'Macbeth', '1.', 'I', 'come,', 'Gray-Malkin', 'All.', 'Padock', 'calls', 'anon:', 'faire', 'is', 'foule,', 'and', 'foule', 'is', 'faire,', 'Houer', 'through', 'the', 'fogge', 'and', 'filthie', 'ayre.', 'Exeunt.', 'Scena', 'Secunda.', 'Alarum', 'within.', 'Enter', 'King,', 'Malcome,', 'Donalbaine,', 'Lenox,', 'with', 'attendants,', 'meeting', 'a', 'bleeding', 'Captaine.', 'King.', 'What', 'bloody', 'man', 'is', 'that?', 'he', 'can', 'report,', 'As', 'seemeth', 'by', 'his', 'plight,', 'of', 'the', 'Reuolt', 'The', 'newest', 'state', 'Mal.', 'This', 'is', 'the', 'Serieant,', 'Who', 'like', 'a', 'good', 'and', 'hardie', 'Souldier', 'fought', "'Gainst", 'my', 'Captiuitie:', 'Haile', 'braue', 'friend;', 'Say', 'to', 'the', 'King,', 'the', 'knowledge', 'of', 'the', 'Broyle,', 'As', 'thou', 'didst', 'leaue', 'it', 'Cap.', 'Doubtfull', 'it', 'stood,', 'As', 'two', 'spent', 'Swimmers,', 'that', 'doe', 'cling', 'together,', 'And', 'choake', 'their', 'Art:', 'The', 'me
【问题讨论】:
-
您的问题是什么? “为我实现此功能”与此站点无关。你必须诚实地尝试,然后就你的算法或技术提出一个具体问题。请填写tour,阅读what's on-topic here、How to Ask 和question checklist,并提供minimal reproducible example 复制您的特定问题 的特定 问题地址。
-
@PranavHosangadi,感谢您的意见。现在添加我的代码。
-
如果你先分成几行,然后然后分成单词,你可能会更容易,因为我希望缩写的字符名称总是在开头一条线?另外,我注意到当一个新角色开始说话时,该行缩进了几个空格。这可能是另一回事。
-
我不确定如何分割成行,@PranavHosangadi,或者如何按缩进空格分割。任何建议将不胜感激。