if、else condition、forループを含む関数があります。この関数をラムダ式の中に記述したいと思います。私はこのラムダ関数を作るために多くの方法から試みました。しかし、まだ私はそれをすることができませんでした。これは私の別のルールによる機能です。Pythonのラムダ式の中で関数を呼び出すことはできますか?
negation ='no,not,never'.split(',')
list2 = 'miss,loss,gone,give up,lost'.split(',')
def f(sentence):
s = sentence.split()
l = [s.index(word) for word in s if word in list2]
# Will returns list of indices (of sentence) where word is in list2
if len(l) > 0:
for e in l:
# Check previous word
if s[e-1] not in negation:
print 'sad'
私は、幸せな悲しい、怒ったような文から感情を検出するためのルールに基づく分類器を開発して以来、ラムダ式内でこの機能を表現することができます。以下は私のラムダ機能です。代わりに、ラムダ式にすべてを詰め込むの
rules = [(lambda x: word_tokenize(x)[-1] == '?', "neutral"),
(lambda x: word_tokenize(x)[0] in question, "neutral"),
(lambda x: any(word in list2 for word in [WordNetLemmatizer().lemmatize(word,'v') for word in word_tokenize(x)]), "sad"),
(lambda x: any(word in list1 for word in [WordNetLemmatizer().lemmatize(word,'v') for word in word_tokenize(x)]), "happy")]
print classify("I miss you", rules)
ルールベースの分類子を開発しました。ラムダ式の中に別のルールセットがあります。ですから、私はこれにその表現にも含めたいと思います。 –
'' lambda sentence:f(sentence) 'のような意味ですか? – MSeifert
'lambda:f(" Some text ")'?私はあなたがしようとしていることについてより詳細に説明する必要があると思います。 – Carcigenicate