Home>

I am trying to output a specific part of speech using Mecab.

Error message

If i set a stop word and do not remove unknown words,
I get the following error and I'm not sure how to fix it.

-------------------------------------------- -------------------------------
AttributeError Traceback (most recent call last)
<ipython-input-11-bef4bebf1ee9>in<module>()
     27 return keywords
     28 text = "Tokyo (Japan)"
--->29 extractKeyword (text)
<ipython-input-11-bef4bebf1ee9>in extractKeyword (text)
     17 keywords = []
     18 while node:
--->19 features = tuple (node.feature.split (","))
     20 if features [0] == u "noun" and features [: 3] not in STOP_POS:
     21 keywords.append (node.surface)
AttributeError: 'str' object has no attribute 'feature'
Applicable source code
import sys
import MeCab
STOP_POS = {
    ('Noun', 'adverbable', '*'),
    ('Noun', 'non-independent', 'adverbable'),
    ('Noun', 'Independence', 'General'),
    ('Noun', 'suffix', 'adverb possible'),
    ('Noun', 'Suffix', 'Classifier'),
    ('Noun', 'number', '*'),
}

def extractKeyword (text):
    tagger = MeCab.Tagger ('-Ochasen')
    tagger.parse ('')
    node = text
    keywords = []
    while node:
        features = tuple (node.feature.split (","))
        if features [0] == u "noun" and features [: 3] not in STOP_POS:
            keywords.append (node.surface)
        elif features [0] == u "adjective" and features [1] == u "self-standing":
            keywords.append (node.surface)
        elif features [0] == u "verb" and features [1] == u "independence":
            keywords.append (node.surface)
        node = node.next
        return keywords
text = "Tokyo (Japan)"
extractKeyword (text)
Tried

The following code was executable without error.

import sys
import MeCab
def extractKeyword (text):
    tagger = MeCab.Tagger ('-Ochasen')
    tagger.parse ('')
    node = tagger.parseToNode (text)
    keywords = []
    while node:
        if node.stat! = 0:
            node = node.next
            continue
        if node.feature.split (",") [0] == u "noun":
            keywords.append (node.surface)
        elif node.feature.split (",") [0] == u "adjective":
             keywords.append (node.surface)
        elif node.feature.split (",") [0] == u "verb":
             keywords.append (node.surface)
        node = node.next
    return keywords
text = "Tokyo (Japan)"
extractKeyword (text)


Output

['Japan', 'Tokyo']
Supplemental information (FW/tool version etc.)

python3.6