Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

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:

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

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 you could use:

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ı']]
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading