简体   繁体   中英

Number of preceeding words before a phrase in a string

Assuming I have a list of phrases:

list = ['new york', 'school', 'new']

and a string

text = 'i am going to a school in new york and therefore i have to buy a new uniform to go to new york'

I would like to find the number of words preceeding each phrase (just for first appearance) ie output should be:

new york = 7
school = 5
new = 7

Any idea how I can effectively achieve this?

Naive approach, without any performance or NLP considerations:

lst = ['new york', 'school', 'new']  # do not use 'list' as a name
text = 'i am going to a school in new york and therefore i have to buy a new uniform to go to new york'

{p: len(text[:text.find(p)].strip().split()) for p in lst}
# {'new york': 7, 'school': 5, 'new': 7}

Using count and index :

lst = ['new york', 'school', 'new']
text = 'i am going to a school in new york and therefore i have to buy a new uniform to go to new york'

for x in lst:
    print(f"{x} = {text.count(' ', 0, text.index(x))}")

# new york = 7
# school = 5                                                   
# new = 7

count counts whitespaces in text from start till you meet the first appearance of phrase which is same as the number of words preceding that phrase.

lst = ['new york', 'school', 'new']
text = 'i am going to a school in new york and therefore i have to buy a new uniform to go to new york'

This will give you the string whose count you are searching and count of string

for x in lst:
    print(x +": "+str(len(text[0:text.index(x)].split(' ')) -1))

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM