Fixing Spacing Issues In Python

After extracting lots of text from a PDF using PyPDF2, I had realized that most of the text had spacing issues as PyPDF2 wasn’t able to pick up on a lot of the spaces in the PDF.

This resulted in text being either bunched up together or split apart.

An example of the issue I’m trying to solve is here :
‘thespeedofsoundinsea’

This is one of the strings extracted from a question paper PDF.

After multiple attempts at splitting the text into its constituent words, I was not able to solve the problem due to inconsistencies in the dictionary datasets I was using (namely, english_words, enchant and NLTK wordnet). Additionally, there was an issue mainly in the word ‘a’, because it was difficult to pick up on whether ‘a’ was a standalone word or part of another word.

I haven’t found an answer for the inconsistencies and the ‘a’ issue, but I figured out an answer to the issue where a word would be broken down into other words by using ‘greedy longest match’, basically iterating back to front. Here’s a snippet of the code

from english_words import get_english_words_set
import nltk
from nltk.corpus import wordnet
import enchant

d = enchant.Dict("en_US")
eng_set = set(get_english_words_set(['web2'], lower=True))

def divide_into_largest_words(sequence):
    divided_words = []
    length = len(sequence)
    start = 0

    while start < length:
        end = length
        while not check_dict(sequence[start:end]):
            end -= 1

        divided_words.append(sequence[start:end])

        start = end

    return divided_words

def check_dict(word):
    return word in eng_set or d.check(word) or wordnet.synsets(word)

sequence = "twostudentsaremeasuringthespeedofsound"
largest_words = divide_into_largest_words(sequence)
print("Divided into largest words:", largest_words)

However, this fell short because of the datasets inconsistencies again, the output being
[‘twos’, ‘tu’, ‘dents’, ‘are’, ‘measuring’, ‘the’, ‘speed’, ‘of’, ‘sound’]
as the datasets consider ‘twos’ and ‘tu’ as words (I’m not sure why’

Can someone help me in solving the issues of the dataset inconsistencies (are there and datasets containing words from the English language without as many inconsistencies?) and the issue of checking whether ‘a’ is meant to be standalone or not in the sentence? Thanks!

New contributor

NS270 is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.

Trang chủ Giới thiệu Sinh nhật bé trai Sinh nhật bé gái Tổ chức sự kiện Biểu diễn giải trí Dịch vụ khác Trang trí tiệc cưới Tổ chức khai trương Tư vấn dịch vụ Thư viện ảnh Tin tức - sự kiện Liên hệ Chú hề sinh nhật Trang trí YEAR END PARTY công ty Trang trí tất niên cuối năm Trang trí tất niên xu hướng mới nhất Trang trí sinh nhật bé trai Hải Đăng Trang trí sinh nhật bé Khánh Vân Trang trí sinh nhật Bích Ngân Trang trí sinh nhật bé Thanh Trang Thuê ông già Noel phát quà Biểu diễn xiếc khỉ Xiếc quay đĩa Dịch vụ tổ chức sự kiện 5 sao Thông tin về chúng tôi Dịch vụ sinh nhật bé trai Dịch vụ sinh nhật bé gái Sự kiện trọn gói Các tiết mục giải trí Dịch vụ bổ trợ Tiệc cưới sang trọng Dịch vụ khai trương Tư vấn tổ chức sự kiện Hình ảnh sự kiện Cập nhật tin tức Liên hệ ngay Thuê chú hề chuyên nghiệp Tiệc tất niên cho công ty Trang trí tiệc cuối năm Tiệc tất niên độc đáo Sinh nhật bé Hải Đăng Sinh nhật đáng yêu bé Khánh Vân Sinh nhật sang trọng Bích Ngân Tiệc sinh nhật bé Thanh Trang Dịch vụ ông già Noel Xiếc thú vui nhộn Biểu diễn xiếc quay đĩa Dịch vụ tổ chức tiệc uy tín Khám phá dịch vụ của chúng tôi Tiệc sinh nhật cho bé trai Trang trí tiệc cho bé gái Gói sự kiện chuyên nghiệp Chương trình giải trí hấp dẫn Dịch vụ hỗ trợ sự kiện Trang trí tiệc cưới đẹp Khởi đầu thành công với khai trương Chuyên gia tư vấn sự kiện Xem ảnh các sự kiện đẹp Tin mới về sự kiện Kết nối với đội ngũ chuyên gia Chú hề vui nhộn cho tiệc sinh nhật Ý tưởng tiệc cuối năm Tất niên độc đáo Trang trí tiệc hiện đại Tổ chức sinh nhật cho Hải Đăng Sinh nhật độc quyền Khánh Vân Phong cách tiệc Bích Ngân Trang trí tiệc bé Thanh Trang Thuê dịch vụ ông già Noel chuyên nghiệp Xem xiếc khỉ đặc sắc Xiếc quay đĩa thú vị
Trang chủ Giới thiệu Sinh nhật bé trai Sinh nhật bé gái Tổ chức sự kiện Biểu diễn giải trí Dịch vụ khác Trang trí tiệc cưới Tổ chức khai trương Tư vấn dịch vụ Thư viện ảnh Tin tức - sự kiện Liên hệ Chú hề sinh nhật Trang trí YEAR END PARTY công ty Trang trí tất niên cuối năm Trang trí tất niên xu hướng mới nhất Trang trí sinh nhật bé trai Hải Đăng Trang trí sinh nhật bé Khánh Vân Trang trí sinh nhật Bích Ngân Trang trí sinh nhật bé Thanh Trang Thuê ông già Noel phát quà Biểu diễn xiếc khỉ Xiếc quay đĩa
Thiết kế website Thiết kế website Thiết kế website Cách kháng tài khoản quảng cáo Mua bán Fanpage Facebook Dịch vụ SEO Tổ chức sinh nhật