簡體   English   中英

內聯 Python 如何將過濾器對象從 Python 轉換為 arrays 或至少在 Z01144EF306D73E5768 中的迭代器

[英]Inline Python how to convert filter objects from Python to arrays or iterator at least in Perl

我在 Perl 中使用帶有 Inline::Python 的 spacy,最后我得到了過濾器 object,我完全不知道如何將它轉換為數組,首先, 我試過 grep,map,加入,但沒有,仍然是一個過濾器 object。 我不能使用列表,因為數據流太重要了。 這是我的一段代碼:

use Inline Python => <<'END_OF_PYTHON';

import spacy
from spacy.lang.fr.stop_words import STOP_WORDS as fr_stop
nlp = spacy.load('fr_core_news_md')
nlp.max_length = 40000000

fr_stop = set(fr_stop)
def lemmatizer(words):
    doc = nlp(words)
    yield from filter(lambda x: x not in fr_stop, map(lambda token: token.lemma_ , doc))

END_OF_PYTHON

您是否有任何想法或其他解決方案,例如 IPC::Run 我不知道。

我不知道 Inline::Python 增加了多少開銷,但這完全是矯枉過正。

use IPC::Run qw( run );

my $in = ...;

utf8::encode($in);

run [ "lemmatizer.py" ], \$in, \$out
   or die($?);

utf8::decode($out);
my @lemmas = split /\n/, $out;

您甚至可以避免一次將整個響應加載到 memory 中。

use IPC::Run qw( run );

my $in = ...;

utf8::encode($in);

run([ "lemmatizer.py" ],
   '<', \$in,
   '>', new_chunker, sub {
      my $lemma = shift;
      utf8::decode($lemma);
      ...
   },
)
   or die($?);

On the Python side, simply read from STDIN until EOF and decode from UTF-8 to get the input, and send the output by writing each lemma encoded using UTF-8 as a separate line.

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM