小编Bri*_*itt的帖子

禁用部分 nlp 管道

我在带有 python3 的 Windows 机器上运行 spaCy v2.x。我没有管理员权限,所以我必须将管道称为:

nlp = en_core_web_sm.load()

当我在 *nix 机器上运行相同的脚本时,我可以将管道加载为:

nlp = spacy.load('en', disable = ['ner', 'tagger', 'parser', 'textcat'])

我所做的只是标记化,所以我不需要整个管道。在 Windows 框中,如果我加载管道,如:

nlp = en_core_web_sm.load(disable = ['ner', 'tagger', 'parser', 'textcat'])

这实际上会禁用组件吗?

nlp 管道上的 spaCy 信息

nlp python-3.x spacy

8
推荐指数
2
解决办法
4483
查看次数

从字典中删除值

我有一个大字典,我试图从键中删除值,如果它们以某些值开头.下面是字典的一个小例子.

a_data = {'78567908': {'26.01.17', '02.03.24', '26.01.12', '04.03.03', '01.01.13', '02.03.01', '01.01.10', '26.01.21'}, '85789070': {'26.01.02', '09.01.04', '02.05.04', '02.03.17', '02.05.01'}, '87140110': {'03.15.25', '03.15.24', '03.15.19'}, '87142218': {'26.17.13', '02.11.01', '02.03.22'}, '87006826': {'28.01.03'}}
Run Code Online (Sandbox Code Playgroud)

在我读完字典后,我想删除所有以'26开头的键的值.或'02.' 可能会留下没有值的键(空集).

我确实有适用的代码:

exclude = ('26.', '02.')
f_a_data = {}
for k, v in a_data.items():
    f_a_data.setdefault(k,[])
    for code in v:
        print (k, code, not code.startswith(exclude))
        if not code.startswith(exclude):
            f_a_data[k].append(code)


print('Filtered dict:')
print(f_a_data)  
Run Code Online (Sandbox Code Playgroud)

这将返回一个过滤的字典:

Filtered dict:
{'78567908': ['04.03.03', '01.01.13', '01.01.10'], '85789070': ['09.01.04'], '87140110': ['03.15.25', '03.15.24', '03.15.19'], '87142218': [], '87006826': ['28.01.03']}
Run Code Online (Sandbox Code Playgroud)

问题1:这是过滤字典的最佳方法吗?

问题2 …

python-3.x

5
推荐指数
1
解决办法
54
查看次数

加速 SpaCy 分词器

我正在使用 SpaCy 对数万个文档进行标记。平均每个文档大约需要 5 秒。关于如何加速标记器有什么建议吗?

一些附加信息:

  • 输入文件是带有换行符的文本文件
  • 文件平均大小约为400KB
  • 每个输入文件的标记都会写入输出文件中的新行(尽管如果有助于提高速度,我可以更改它)
  • 有 1655 个停用词
  • 输出文件被输入到 fasttext

以下是我的代码:

from pathlib import Path, PurePath
from time import time

st = time()
nlp = en_core_web_sm.load(disable = ['ner', 'tagger', 'parser', 'textcat'])
p = Path('input_text/').glob('*.txt')
files = ['input_text/' + x.name for x in p if x.is_file()]

#nlp = spacy.load('en-core-web-sm')

stopwords_file = 'stopwords.txt'

def getStopWords():
    f = open(stopwords_file, 'r')
    stopWordsSet = f.read()
    return stopWordsSet

stopWordsSet = getStopWords()
out_file = 'token_results.txt'
for file in files:
    #print (out_file)
    with open(file, …
Run Code Online (Sandbox Code Playgroud)

python-3.x spacy

3
推荐指数
1
解决办法
4338
查看次数

标签 统计

python-3.x ×3

spacy ×2

nlp ×1