Python - просмотр списка ключевых слов, поиск количества совпадений в строке, итоговое итоговое количество - PullRequest
0 голосов
/ 25 июня 2019

У меня есть несколько слов, которые я хочу проверить и посмотреть, встречаются ли они в реферате исследования, и если да, подсчитать количество вхождений.Не уверен, что я делаю не так с моим кодом, но он не считается правильно.Заранее спасибо!

 mh_terms = ['mental', 'ptsd', 'sud', 'substance abuse', 'drug abuse', 
  'alcohol', 'alcoholism', 'anxiety', 'depressing', 'bipolar', 'mh', 
  'smi', 'oud', 'opioid' ]

  singleabstract = 'This is a research abstract that includes words like 
  mental health and anxiety.  My hope is that I get my code to work and 
  not resort to alcohol.'

  for mh in mh_terms: 
       mh = mh.lower
       mh = str(mh)
       number_of_occurences = 0
       for word in singleabstract.split():
          if mh in word:
          number_of_occurences += 1
  print(number_of_occurences)

Ответы [ 2 ]

1 голос
/ 25 июня 2019

Во-первых, print(number_of_occurences) должен быть ограничен для каждого mh, чтобы напечатать вхождения для этого конкретного слова.Во-вторых, напечатайте часть слова нашего печатного сообщения.Я думаю, что основная проблема с вашей программой заключается в том, что вы должны использовать mh.lower() вместо mh.lower

1 голос
/ 25 июня 2019

Обычно, для группировки, dict - хороший путь.Для подсчета вы можете использовать реализацию, подобную следующей:

c = {}

singleabstract = 'This is a research abstract that includes words like 
  mental health and anxiety.  My hope is that I get my code to work and 
  not resort to alcohol.'

for s in singleabstract.split():
    s = ''.join(char.lower() for char in s if char.isalpha()) # '<punctuation>'.isalpha() yields False
    # you'll need to check if the word is in the dict
    # first, and set it to 1
    if s not in c:
        c[s] = 1
    # otherwise, increment the existing value by 1
    else:
        c[s] += 1

# You can sum the number of occurrences, but you'll need
# to use c.get to avoid KeyErrors
occurrences = sum(c.get(term, 0) for term in mh_terms)

occurrences
3

# or you can use an if in the generator expression
occurrences = sum(c[term] for term in mh_terms if term in c)

Наиболее оптимальный способ подсчета вхождений - это использование collections.Counter.Это словарь, который позволяет вам O (1) проверять ключи:

from collections import Counter

singleabstract = 'This is a research abstract that includes words like 
  mental health and anxiety.  My hope is that I get my code to work and 
  not resort to alcohol.'

# the Counter can consume a generator expression analogous to
# the for loop in the dict implementation
c = Counter(''.join(char.lower() for char in s if char.isalpha()) 
            for s in singleabstract.split())

# Then you can iterate through
for term in mh_terms:
    # don't need to use get, as Counter will return 0
    # for missing keys, rather than raising KeyError 
    print(term, c[term]) 

mental 1
ptsd 0
sud 0
substance abuse 0
drug abuse 0
alcohol 1
alcoholism 0
anxiety 1
depressing 0
bipolar 0
mh 0
smi 0
oud 0
opioid 0

Чтобы получить желаемый результат, вы можете суммировать значения для объекта Counter:

total_occurrences = sum(c[v] for v in mh_terms)

total_occurrences
3
Добро пожаловать на сайт PullRequest, где вы можете задавать вопросы и получать ответы от других членов сообщества.
...