如果我想返回一个列表上的操作,但当它返回 null 时它保持不变,我该怎么说呢?

发布于 2025-01-17 06:56:45 字数 733 浏览 2 评论 0原文

我有一个土耳其语词组列表。我想应用词干分析,我找到了 turkishnlp 包。尽管它有一些缺点,但它经常返回正确的单词。但是,当我将其应用于列表时,我不希望列表的结构发生变化,并且我希望他不知道的单词保持不变。

例如,我有这个列表: mylist = [['yolda','gelirken','kopek', 'gördüm'],['cok', 'tatlıydı']]

我写了这个函数:

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))

这个函数返回这个列表:

tr_stemming(mylist)

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

但是,我想得到这是输出: [['yol', 'gelir', 'kopek', 'gör'], ['cok', 'tatlı']]

如何更新我的函数? 感谢您的帮助!

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!

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

幻梦 2025-01-24 06:56:45

IIUC,您可以将函数修改为:

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)

输出:

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

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ı']]
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文