Sid
Sid

Reputation: 259

String and word manipulation in Python

Example:

I have a sentence 'Face book is a social networking company', which I want to clean by concatenating 'Face' and 'book' into 'Facebook'. I would like to check and perform this for numerous sentences. Any suggestions on how can I do this?

I thought of something on the lines of this: first tokenzing the sentence and then looping over every word and check if the token (word) after 'face' is 'book' and then delete the two elements and all 'Facebook'.

Upvotes: 0

Views: 179

Answers (3)

Caleb H.
Caleb H.

Reputation: 1717

In Python, this might look something like this: (Keep in mind this is only a rough idea, it won’t be perfect in all cases)

——————————

string = “I use Face book”
tokenized = string.split(“ “)
for i in range(0,len(tokenized)-1):
    if tokenized[i].lower()==“face” and tokenized[i+1].lower()==“book”:
        del tokenized[i+1]
        tokenized[i] = “Facebook”
    if i > len(tokenized):
        break

———————————

Upvotes: -1

Sunitha
Sunitha

Reputation: 12015

Wouldn't a simple regex based approach be sufficient?

>>> import re
>>> s='Face book is a social networking company'
>>> re.sub(r'[Ff]ace [Bb]ook', 'Facebook', s)
'Facebook is a social networking company'

Upvotes: 1

user9948596
user9948596

Reputation: 11

The most straight forward way of doing this in python, for me, would be using a tuple. Just pack all your strings into a tuple and loop through while applying the str.replace(old,new) method. str.replace(old,new) replaces a substring in the string str, with a new substring you specify. Example below:

Code:

string1 = "Face book is a social networking company1"
string2 = "Face book is a social networking company2"
string3 = "Face book is a social networking company3"
old = "Face book"
new = "Facebook"

superdupletuple = (string1, string2,string3)

for i in superdupletuple:
    print(i.replace(old, new))

Output:

Facebook is a social networking company1
Facebook is a social networking company2
Facebook is a social networking company3

Upvotes: 1

Related Questions