Separating words in a string

How do I separate words in a string?

In the following I have a random sample of words in a string extracted from text file with over a million words.

Here’s the string:
“intervene Pockets Jerusalemand tissues powerful days gadgets rate invention heated Stewartis find communal working lots internal They caused noise offer goand big jumps lose galaxies All Mall birth child world spin killed produced great center Dont pretend Africa solution times cursor good night professional problem refugees talk”

As you can see, the third word is “Jerusalemand.” My goal is to separate “Jerusalem” and “and”, and do the same thing for any other words that are stuck together.

Only thing that I could think of as of now is to compare every word in the string to a dictionary (SCOWL perhaps?), and if a segment of the word matches a word in the dictionary, I’ll have to split the words to make them independent.

As an example, for “Jerusalemand” I’ll loop through every character until a match is found in the dictionary for “Jerusalem,” then I can separate that from “and,” in turn completing the separation.

Shouldn’t there be a better way to do this?

3

Solutions to this problem are often ambiguous, and it’s sometimes difficult to decide an optimum solution, even for a human.

If you need only a single solution where all words exist in a dictionary, then a naive approach will fail as soon as you encounter a word which prefix is also a valid dictionary entry.

For example, if your input string was:

tryingtomessitup

Unless you backtrack, your algorithm would probably come up with:

try in gtomessitup

instead of

trying to mess it up

Correct solution to this problem needs to be done using dynamic programming, or even a brute force recursive algorithm which can decide if a prefix indeed leads to the optimal solution.

A DP example is mentioned in this StackOverflow thread. This answer explains it even more thoroughly (and leads to this article: Splitting words joined into a single string (compound-splitter)).

I can’t think of any sensible alternatives to the general approach you describe. You’re probably going to want to focus your attention on exactly what steps you’re going to use to (try to) transform a potential word into one or more verified words.

One thing you could definitely do to speed things up is to check the whole word first, and only check substrings if that lookup fails. This assumes that most potential words are valid words, as appears to be the case in your example.

Some things to consider, in no particular order:

  1. Will there ever be more than 2 words jammed together?
  2. Make sure your dictionary lookup is fast!
  3. A capital letter in the middle of the word could be a good indication of where to start trying to split a word.
  4. Is a split only valid if both the parts are valid words?
  5. What do you want to do if there’s a potential word which you can’t split into 2 (or more) valid words?
  6. If you really care about efficiency, you may find it’s faster to try splitting potential words in half first, rather than starting with the first character and going from there. Valid splits are going to tend to be near the middle of potential words, rather than at the beginning or end. This would make the code responsible for trying all the potential splits a bit more complex though.

1

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