s = “超過 20 年,這項投資是成本中性的,因為它由適度的‘舒適費用?’覆寫,低于基于經過充分驗證的 EnergieSprong 模型的同等能源費用。資本預算而不是投機性投資在商業地產中,商業案例尚不清楚,我們建議理事會加入越來越多的地方當局的行列,開發新的太陽能發電場。這符合我們的政策目標,并提供適度但安全的回報(扣除借款) . 我們建議投資的 5100 萬英鎊(類似于最初用于商業地產的金額)”
這是使用基本 python 及其 PyPDF 庫從 web pdf 中洗掉的文本
我想洗掉粗體字中不需要的空格。
注意:我手動將它們加粗只是為了解釋我的問題。如果有人可以提供幫助,我將不勝感激。提前非常感謝!
uj5u.com熱心網友回復:

uj5u.com熱心網友回復:
簡單的手工方法
如果您已經確定'pr operty'傾向于用額外的空格撰寫,這里有一個簡單的函式,它將從所有出現的 中洗掉空格pr operty:
def remove_whitespace_in_word(text, word):
return text.replace(word, ''.join(word.split()))
s = "The pr operty. Over 20 years of pr operty, this investment is cost neutral as it is covered by a modest ?comfort ch arge? ? less than the equivalent energy bills would have been ? based on the well -proven EnergieSprong model. Capital Budget Rather than speculatively invest ing in commercial property, for which the business case is unclear, we propose that the Council j oin the growing ranks of local authorities developing new solar farms. This meets our pr operty policy objectives and provides a modest, but secure, return (net of borrowing). The £51m we propose to invest in pr operty (similar to the amount originally intended for commercial pr operty)"
new_text = remove_whitespace_in_word(s, 'pr operty')
print(new_text)
# 'The property. Over 20 years of property, this investment is cost neutral as it is covered by a modest ?comfort ch arge? ? less than the equivalent energy bills would have been ? based on the well -proven EnergieSprong model. Capital Budget Rather than speculatively invest ing in commercial property, for which the business case is unclear, we propose that the Council j oin the growing ranks of local authorities developing new solar farms. This meets our property policy objectives and provides a modest, but secure, return (net of borrowing). The £51m we propose to invest in property (similar to the amount originally intended for commercial property)'
您只需呼叫一次即可修復所有出現的pr operty; 但是您需要為每個其他冒犯性的單詞再次呼叫它,例如ch arge.
復雜的自動化方法
這是一個建議的演算法。它并不完美,但應該處理許多錯誤:
- 加載一個包含所有已知英語單詞的資料結構,例如Scrabble words 字典。
- 在您的文本中查找字典中沒有的單詞。
- 嘗試通過將每個違規單詞與之前的相鄰單詞或之后的相鄰單詞合并來修復它。
- 嘗試合并時,有多種可能性。如果后面的詞也有冒犯性,并且將它們合并成一個非冒犯性的詞,那么它可能很合適。如果后面的詞沒有冒犯,但合并它們會產生一個不冒犯的詞,那么它可能仍然很合適。如果后面的詞沒有冒犯,并且將它們合并不會導致不冒犯的詞,那么它可能不太合適。
- 生成所有已執行修復的日志,以便用戶可以閱讀日志并確保修復看起來合法。生成日志非常重要;您不希望您的演算法在不跟蹤已編輯內容的情況下編輯文本。
- 您甚至可以執行互動式步驟,計算機提出修復建議但等待用戶驗證它。當用戶驗證修復時,記住它,以便如果另一個修復相同,則不需要再次詢問用戶。例如,如果
"pr operty"文本中有多次出現,您只需要求確認一次。
uj5u.com熱心網友回復:
您可以split將格式錯誤的句子放在空格上,并檢查拆分串列中的每對單詞/標記,以查看它們本身是否是有效的單詞,或者它們的組合是否是有效的單詞。
對于有效單詞,根據您使用的作業系統,您可以找到內置的單詞串列。在 Linux 上,這些詞通常位于usr/share/dict/words. 或者,您可以從 Internet 下載單詞串列。
from itertools import pairwise
with open('/usr/share/dict/words') as f:
word_file = set(_.strip() for _ in f.readlines())
def fix_spaces(iterable):
it = iter(pairwise(iterable))
while True:
try:
word1, word2 = next(it)
if word1 not in word_file or word2 not in word_file:
if word1 word2 in word_file:
yield word1 word2
word1, word2 = next(it)
else:
yield word1
except StopIteration:
yield word2
break
sentence = "A sent ence w ith wei rd spaces"
' '.join(fix_spaces(sentence.split()))
# 'A sentence with weird spaces'
請注意,這仍然會有邊緣情況,具體取決于您的單詞串列以及可以以多種方式洗掉空格的邊緣情況(例如,像這樣的句子s="tube light speed"可以是tubelight speed或tube lightspeed?)
轉載請註明出處,本文鏈接:https://www.uj5u.com/caozuo/448271.html
標籤:Python 网页抓取 nlp pdf-scraping
