Как посчитать количество словосочетаний?
Нужно, что бы код считал определённые слова, например "you know"
Sample Input:
I called him, you know, 5 times this morning and like, he never answered.
Sample Output:
1
import numpy as np
n = "I called him, you know, 5 times this morning and like, he never answered."
text = str.split(n)
y = np.array(text)
x = np.count_nonzero(y == "you" and "know")
print(x)
Ответы (3 шт):
Автор решения: Zhihar
→ Ссылка
почему бы не сделать так:
text = "I called him, you know, 5 times this morning and like, he never answered."
res = len(text.split("you know")) - 1
дешево и очень сердито :)
Автор решения: Deus
→ Ссылка
text = "I called him, you know, 5 times this morning and like, he never answered."
res = len(text.split("you know")) // 2
print(res)
Автор решения: MaxU
→ Ссылка
import re
text = """I called him, you know, 5 times this morning and like, he never answered.
You know him, right?
What about 'you knowledge' or 'babayou know' - are you guys going to count them as well? ;)
"""
entries = re.findall(r"\byou know\b", text.lower())
res = len(entries) if entries else 0
print(res)
результат
2