Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Python 2.x sublist parameter equivalent in 3.x

Tags:

python

nlp

nltk

I'm working some NLP experiments in Python and wanted to implement this function in Python 3.x though the lambda expansion here is giving me a hard time and I'm not sure how it'd be implemented in 3.x; any suggestions?

    candidates = [' '.join(word for word, pos, chunk in group).lower()
              for key, group in itertools.groupby(all_chunks, lambda (word,pos,chunk): chunk != 'O') if key]

The error I get is on this piece since sublist parameters are not supported in 3.x

lambda (word,pos,chunk)

The original source is the extract_candidate_words function here: http://bdewilde.github.io/blog/2014/09/23/intro-to-automatic-keyphrase-extraction/

like image 947
iivel Avatar asked Apr 14 '26 10:04

iivel


1 Answers

The syntax in Python3 would be:

lambda word__pos__chunk: word__pos__chunk[2] != 'O'

A lambda function with a three item tuple (word, pos, chunk) gets converted to word__pos__chunk with positional arguments, word__pos__chunk[2] access the third item for example.

more information: pep-3113

like image 74
Javier Clavero Avatar answered Apr 16 '26 00:04

Javier Clavero



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!