Parse two words in Persian which are concatenated by half space (\u200c)

Viewed 267

I'm dealing with Persian language poems (The language alphabet is almost the same as Arabic). For each line of the poem in my file, I want to parse its words and hold them in another list as monolithic words. The problem is that some words are separated by space, which I can easily handle with split(), but very few are separated by half-space or - \u200c.

For example, this is a string in the Persian language:

s = "سنگی‌تری"

The first word is "سنگی" and the second word is "تری". I wanted to separate each of them, but my problem is that I don't know how, and if I use s.split(), I get ['سنگی\u200cتری'] which is one word and also has \u200c, which should not be. (The two words in s are separated by \u200c instead of space and this is where the problem arises).

I should also repeat that I need words that are separated by space to be parsed too. So if it was s = "سنگی ‌تری" (this time separated by space), I also need to handle it and parse it into "سنگی" and "تری". As I said, the latter is achievable by split() method.

2 Answers

Simple solution:

s.replace("\u200c", " ").split()

If you have a longer or more complicated list of boundaries you need to split on, then using re.split would be my recommendation.

You can either use re.split:

>>> re.split(" |\u200c", "سنگیتری")
['سنگی', 'تری']

Or use split twice:

>>> [x for full in "سنگیتری".split() for x in full.split("\u200c")]
['سنگی', 'تری']

I think I prefer @wim's answer, though.

Related