Search code examples
pythonfunctionnlpmap-functionisinstance

How can I say that if I want to return an operation on a list, but it stays the same when it comes out null?


I have a list-of-list of word groups in Turkish. I want to apply stemming and I found turkishnlp package. Although it has some shortcomings, it often returns the right word. However, when I apply this to the list, I don't want the structure of my list to change and I want the words that he doesn't know to stay the same.

For example, I have this list: mylist = [['yolda','gelirken','kopek', 'gördüm'],['cok', 'tatlıydı']]

And I wrote this function:

from trnlp import TrnlpWord
def tr_stemming(x):
    obj = TrnlpWord()
    obj.setword(x) if isinstance(x, str) else type(x)(map(tr_stemming, x))
    return obj.get_stem if isinstance(x, str) else type(x)(map(tr_stemming, x))

This function returns this list:

tr_stemming(mylist)

[['yol', 'gelir', '', 'gör'], ['', 'tatlı']]

However, I want to get this as the output: [['yol', 'gelir', 'kopek', 'gör'], ['cok', 'tatlı']]

How can I update my function? Thank you for your helps!


Solution

  • IIUC, you could modify your function to:

    def tr_stemming(x):
        if isinstance(x, str):
            obj = TrnlpWord()
            obj.setword(x)
            stem = obj.get_stem
            return stem if stem else x
        elif isinstance(x, list):
            return [tr_stemming(e) for e in x]
        
    out = tr_stemming(mylist)
    

    output:

    [['yol', 'gelir', 'kopek', 'gör'], ['cok', 'tatlı']]