seen2 = set()

def eliminate_abs(d): ##remove all entries that connect to the abstraction node, type(d) = list
    def rec(x):
        if x not in seen2:
            seen2.add(x)
            a = x.hypernyms()
            if len(a) != 0:
                kk = a[0]
                if re.search('abstraction',str(kk)):
                    syns.remove(ii)
                else:
                    rec(kk)

    for ii in d: ##type(ii) = <class 'nltk.corpus.reader.wordnet.Synset'>
        rec(ii)

eliminate_abs(syns)

The list "syns" will eventually be converted into a tree but I first need to remove all of the items which ultimately connect to the abstraction node. What I want this function to do is recursively look through all of the hypernyms for each item in "syns" and and if "abstraction" is ever found, remove the original term from "syns". For some reason this is only removing some of them.

有帮助吗?

解决方案

Since you are mucking with syns while iterating over it, you should iterate over a slice of syns, i.e. make a copy of the list and iterate over the copy:

for ii in d[:]:
    rec(ii)

其他提示

Figured it out. It works fine but they're are a bunch of repeats in syns so all of them after the first one get skipped. Removing if not x in seen2: seen2.add(x) makes it work fine.

许可以下: CC-BY-SA归因
不隶属于 StackOverflow
scroll top