2017-06-02 13 views
0

私は文章を持っており、代名詞、名詞、形容詞のみを対応するPOSタグに置き換えたいと思います。私の文章がある 例えば:文中の代名詞、名詞、動詞、形容詞のみを対応するタグに置き換えると、どのようにして効率的にPythonでそれを行うことができますか?

"私が最も美しい都市、イスラマバードに行きます" と、ほとんどのJJ NNに「PRP午前VBG結果

をしたいです、NNP "

+0

そこで質問は何ですか?どこに問題がありますか? –

+0

Python NLTKで効率的に行うにはどうすればいいですか?ありがとう@トーマスプラスコタ – Irfanullah

答えて

0

TL; DR

>>> from nltk import pos_tag, word_tokenize 
>>> sent = "I am going to the most beautiful city, Islamabad" 
>>> [pos if any(p for p in wanted_tags if pos.startswith(p)) else word for word, pos in pos_tag(word_tokenize(sent))] 
['PRP', 'VBP', 'VBG', 'to', 'the', 'RBS', 'JJ', 'NN', ',', 'NNP'] 

>>> from nltk.corpus import stopwords 
>>> stoplist = stopwords.words('english') 
>>> [pos if any(p for p in wanted_tags if pos.startswith(p)) and word not in stoplist else word for word, pos in pos_tag(word_tokenize(sent))] 
['PRP', 'am', 'VBG', 'to', 'the', 'most', 'JJ', 'NN', ',', 'NNP'] 
関連する問題