簡體   English   中英

字符串中短語之前的前序單詞數

[英]Number of preceeding words before a phrase in a string

假設我有一個短語列表:

list = ['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'

我想找到每個短語之前的單詞數量(僅針對首次出現),即輸出應為:

new york = 7
school = 5
new = 7

知道我怎樣才能有效地做到這一點嗎?

幼稚的方法,不考慮任何性能或NLP:

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}

使用countindex

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 ,直到遇到詞組的首次出現為止, count計算text空格,該空格與該詞組前面的單詞數相同。

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(x +": "+str(len(text[0:text.index(x)].split(' ')) -1))

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM