Как посчитать 150 лучших слов и удалить общие слова из 2 списков? - PullRequest
0 голосов
/ 08 июня 2018

Этот код ниже предназначен для определения 150 лучших слов, которые встречаются чаще всего в 2 строках.

pwords = re.findall(r'\w+',p)
ptop150words=Counter(pwords).most_common(150)
sorted(ptop150words)

nwords = re.findall(r'\w+',n)
ntop150words=Counter(nwords).most_common(150)
sorted(ntop150words)

Этот код ниже предназначен для удаления общих слов, встречающихся в 2 строках.

def new(ntopwords,ptopwords):
    for i in ntopwords[:]:
        if i in potopwords:
            ntopwords.remove(i)
            ptopwords.remove(i)
print(i)

Однако вывод на печать (i) отсутствует.что не так?

Ответы [ 3 ]

0 голосов
/ 08 июня 2018

Код, который вы опубликовали, не вызывает функцию new(negativetop150words, positivetop150words) Также, согласно комментарию Джесси, команда print (i) находится за пределами функции.Вот код, который работал для меня:

import re
from collections import Counter

def new(negativetop150words, positivetop150words):
    for i in negativetop150words[:]:
        if i in positivetop150words:
            negativetop150words.remove(i)
            positivetop150words.remove(i)
            print(i)

    return negativetop150words, positivetop150words

positive = 'The FDA is already fairly gung-ho about providing this. It receives about 1,000 applications a year and approves all but 1%. The agency makes sure there is sound science behind the request, and no obvious indication that the medicine would harm the patient.'
negative = 'Thankfully these irritating bits of bureaucracy have been duly dispatched. This victory comes courtesy of campaigning work by a libertarian think-tank, the Goldwater Institute, based in Arizona. It has been pushing right-to-try legislation for around four years, and it can now be found in 40 states. Speaking about the impact of these laws on patients, Arthur Caplan, a professor of bioethics at NYU School of Medicine in New York, says he can think of one person who may have been helped.'

positivewords = re.findall(r'\w+', positive)
positivetop150words = Counter(positivewords).most_common(150)
sorted(positivetop150words)

negativewords = re.findall(r'\w+', negative)
negativetop150words = Counter(negativewords).most_common(150)

words = new(negativewords, positivewords)

Это печатает:

a
the
It
and
about
the
0 голосов
/ 08 июня 2018

Вы можете положиться на set методы.Когда у вас есть оба списка, вы конвертируете их в наборы.Общее подмножество является пересечением двух наборов, и вы можете просто взять разницу от обоих исходных наборов:

positiveset = set(positivewords)
negativeset = set(negativewords)
commons = positiveset & negativeset
positivewords = sorted(positiveset - commons)
negativewords = sorted(negativeset - commons)
commonwords = sorted(commons)
0 голосов
/ 08 июня 2018

Скорее всего, ваш отступ.

new(negativetop150words,positivetop150words):
    for i in negativetop150words[:]:
        if i in positivetop150words:
            negativetop150words.remove(i)
            positivetop150words.remove(i)
            print(i)
...