import{s as ce,n as Me,o as ge}from"../chunks/scheduler.9991993c.js";import{S as ke,i as he,g as r,s as a,r as m,A as ye,h as p,f as s,c as n,j as oe,u as o,x as f,k as fe,y as je,a as l,v as c,d as M,t as g,w as k}from"../chunks/index.7fc9a5e7.js";import{C as D}from"../chunks/CodeBlock.e11cba92.js";import{H as ee,E as ue}from"../chunks/EditOnGithub.84ab7f0e.js";function Te(te){let i,C,F,I,h,P,y,se='<a href="/docs/transformers/pr_30356/zh/main_classes/tokenizer#transformers.PreTrainedTokenizerFast">PreTrainedTokenizerFast</a> 依赖于 <a href="https://huggingface.co/docs/tokenizers" rel="nofollow">🤗 Tokenizers</a> 库。从 🤗 Tokenizers 库获得的分词器可以被轻松地加载到 🤗 Transformers 中。',N,j,le="在了解具体内容之前，让我们先用几行代码创建一个虚拟的分词器：",Q,u,W,T,ae="现在，我们拥有了一个针对我们定义的文件进行训练的分词器。我们可以在当前运行时中继续使用它，或者将其保存到一个 JSON 文件以供将来重复使用。",E,$,G,d,ne='让我们看看如何利用 🤗 Transformers 库中的这个分词器对象。<a href="/docs/transformers/pr_30356/zh/main_classes/tokenizer#transformers.PreTrainedTokenizerFast">PreTrainedTokenizerFast</a> 类允许通过接受已实例化的 <em>tokenizer</em> 对象作为参数，进行轻松实例化：',X,z,R,b,re='现在可以使用这个对象，使用 🤗 Transformers 分词器共享的所有方法！前往<a href="main_classes/tokenizer">分词器页面</a>了解更多信息。',x,U,q,Z,pe="为了从 JSON 文件中加载分词器，让我们先保存我们的分词器：",H,w,S,_,ie='我们保存此文件的路径可以通过 <code>tokenizer_file</code> 参数传递给 <a href="/docs/transformers/pr_30356/zh/main_classes/tokenizer#transformers.PreTrainedTokenizerFast">PreTrainedTokenizerFast</a> 初始化方法：',L,J,A,V,me='现在可以使用这个对象，使用 🤗 Transformers 分词器共享的所有方法！前往<a href="main_classes/tokenizer">分词器页面</a>了解更多信息。',Y,B,K,v,O;return h=new ee({props:{title:"使用 🤗 Tokenizers 中的分词器",local:"使用--tokenizers-中的分词器",headingTag:"h1"}}),u=new D({props:{code:"ZnJvbSUyMHRva2VuaXplcnMlMjBpbXBvcnQlMjBUb2tlbml6ZXIlMEFmcm9tJTIwdG9rZW5pemVycy5tb2RlbHMlMjBpbXBvcnQlMjBCUEUlMEFmcm9tJTIwdG9rZW5pemVycy50cmFpbmVycyUyMGltcG9ydCUyMEJwZVRyYWluZXIlMEFmcm9tJTIwdG9rZW5pemVycy5wcmVfdG9rZW5pemVycyUyMGltcG9ydCUyMFdoaXRlc3BhY2UlMEElMEF0b2tlbml6ZXIlMjAlM0QlMjBUb2tlbml6ZXIoQlBFKHVua190b2tlbiUzRCUyMiU1QlVOSyU1RCUyMikpJTBBdHJhaW5lciUyMCUzRCUyMEJwZVRyYWluZXIoc3BlY2lhbF90b2tlbnMlM0QlNUIlMjIlNUJVTkslNUQlMjIlMkMlMjAlMjIlNUJDTFMlNUQlMjIlMkMlMjAlMjIlNUJTRVAlNUQlMjIlMkMlMjAlMjIlNUJQQUQlNUQlMjIlMkMlMjAlMjIlNUJNQVNLJTVEJTIyJTVEKSUwQSUwQXRva2VuaXplci5wcmVfdG9rZW5pemVyJTIwJTNEJTIwV2hpdGVzcGFjZSgpJTBBZmlsZXMlMjAlM0QlMjAlNUIuLi4lNUQlMEF0b2tlbml6ZXIudHJhaW4oZmlsZXMlMkMlMjB0cmFpbmVyKQ==",highlighted:`<span class="hljs-meta">&gt;&gt;&gt; </span><span class="hljs-keyword">from</span> tokenizers <span class="hljs-keyword">import</span> Tokenizer
<span class="hljs-meta">&gt;&gt;&gt; </span><span class="hljs-keyword">from</span> tokenizers.models <span class="hljs-keyword">import</span> BPE
<span class="hljs-meta">&gt;&gt;&gt; </span><span class="hljs-keyword">from</span> tokenizers.trainers <span class="hljs-keyword">import</span> BpeTrainer
<span class="hljs-meta">&gt;&gt;&gt; </span><span class="hljs-keyword">from</span> tokenizers.pre_tokenizers <span class="hljs-keyword">import</span> Whitespace

<span class="hljs-meta">&gt;&gt;&gt; </span>tokenizer = Tokenizer(BPE(unk_token=<span class="hljs-string">&quot;[UNK]&quot;</span>))
<span class="hljs-meta">&gt;&gt;&gt; </span>trainer = BpeTrainer(special_tokens=[<span class="hljs-string">&quot;[UNK]&quot;</span>, <span class="hljs-string">&quot;[CLS]&quot;</span>, <span class="hljs-string">&quot;[SEP]&quot;</span>, <span class="hljs-string">&quot;[PAD]&quot;</span>, <span class="hljs-string">&quot;[MASK]&quot;</span>])

<span class="hljs-meta">&gt;&gt;&gt; </span>tokenizer.pre_tokenizer = Whitespace()
<span class="hljs-meta">&gt;&gt;&gt; </span>files = [...]
<span class="hljs-meta">&gt;&gt;&gt; </span>tokenizer.train(files, trainer)`,wrap:!1}}),$=new ee({props:{title:"直接从分词器对象加载",local:"直接从分词器对象加载",headingTag:"h2"}}),z=new D({props:{code:"ZnJvbSUyMHRyYW5zZm9ybWVycyUyMGltcG9ydCUyMFByZVRyYWluZWRUb2tlbml6ZXJGYXN0JTBBJTBBZmFzdF90b2tlbml6ZXIlMjAlM0QlMjBQcmVUcmFpbmVkVG9rZW5pemVyRmFzdCh0b2tlbml6ZXJfb2JqZWN0JTNEdG9rZW5pemVyKQ==",highlighted:`<span class="hljs-meta">&gt;&gt;&gt; </span><span class="hljs-keyword">from</span> transformers <span class="hljs-keyword">import</span> PreTrainedTokenizerFast

<span class="hljs-meta">&gt;&gt;&gt; </span>fast_tokenizer = PreTrainedTokenizerFast(tokenizer_object=tokenizer)`,wrap:!1}}),U=new ee({props:{title:"从 JSON 文件加载",local:"从-json-文件加载",headingTag:"h2"}}),w=new D({props:{code:"dG9rZW5pemVyLnNhdmUoJTIydG9rZW5pemVyLmpzb24lMjIp",highlighted:'<span class="hljs-meta">&gt;&gt;&gt; </span>tokenizer.save(<span class="hljs-string">&quot;tokenizer.json&quot;</span>)',wrap:!1}}),J=new D({props:{code:"ZnJvbSUyMHRyYW5zZm9ybWVycyUyMGltcG9ydCUyMFByZVRyYWluZWRUb2tlbml6ZXJGYXN0JTBBJTBBZmFzdF90b2tlbml6ZXIlMjAlM0QlMjBQcmVUcmFpbmVkVG9rZW5pemVyRmFzdCh0b2tlbml6ZXJfZmlsZSUzRCUyMnRva2VuaXplci5qc29uJTIyKQ==",highlighted:`<span class="hljs-meta">&gt;&gt;&gt; </span><span class="hljs-keyword">from</span> transformers <span class="hljs-keyword">import</span> PreTrainedTokenizerFast

<span class="hljs-meta">&gt;&gt;&gt; </span>fast_tokenizer = PreTrainedTokenizerFast(tokenizer_file=<span class="hljs-string">&quot;tokenizer.json&quot;</span>)`,wrap:!1}}),B=new ue({props:{source:"https://github.com/huggingface/transformers/blob/main/docs/source/zh/fast_tokenizers.md"}}),{c(){i=r("meta"),C=a(),F=r("p"),I=a(),m(h.$$.fragment),P=a(),y=r("p"),y.innerHTML=se,N=a(),j=r("p"),j.textContent=le,Q=a(),m(u.$$.fragment),W=a(),T=r("p"),T.textContent=ae,E=a(),m($.$$.fragment),G=a(),d=r("p"),d.innerHTML=ne,X=a(),m(z.$$.fragment),R=a(),b=r("p"),b.innerHTML=re,x=a(),m(U.$$.fragment),q=a(),Z=r("p"),Z.textContent=pe,H=a(),m(w.$$.fragment),S=a(),_=r("p"),_.innerHTML=ie,L=a(),m(J.$$.fragment),A=a(),V=r("p"),V.innerHTML=me,Y=a(),m(B.$$.fragment),K=a(),v=r("p"),this.h()},l(e){const t=ye("svelte-u9bgzb",document.head);i=p(t,"META",{name:!0,content:!0}),t.forEach(s),C=n(e),F=p(e,"P",{}),oe(F).forEach(s),I=n(e),o(h.$$.fragment,e),P=n(e),y=p(e,"P",{"data-svelte-h":!0}),f(y)!=="svelte-40ojj2"&&(y.innerHTML=se),N=n(e),j=p(e,"P",{"data-svelte-h":!0}),f(j)!=="svelte-opvc5k"&&(j.textContent=le),Q=n(e),o(u.$$.fragment,e),W=n(e),T=p(e,"P",{"data-svelte-h":!0}),f(T)!=="svelte-17x9tu2"&&(T.textContent=ae),E=n(e),o($.$$.fragment,e),G=n(e),d=p(e,"P",{"data-svelte-h":!0}),f(d)!=="svelte-1x979ag"&&(d.innerHTML=ne),X=n(e),o(z.$$.fragment,e),R=n(e),b=p(e,"P",{"data-svelte-h":!0}),f(b)!=="svelte-tb93lc"&&(b.innerHTML=re),x=n(e),o(U.$$.fragment,e),q=n(e),Z=p(e,"P",{"data-svelte-h":!0}),f(Z)!=="svelte-lr3arf"&&(Z.textContent=pe),H=n(e),o(w.$$.fragment,e),S=n(e),_=p(e,"P",{"data-svelte-h":!0}),f(_)!=="svelte-gwg9ut"&&(_.innerHTML=ie),L=n(e),o(J.$$.fragment,e),A=n(e),V=p(e,"P",{"data-svelte-h":!0}),f(V)!=="svelte-tb93lc"&&(V.innerHTML=me),Y=n(e),o(B.$$.fragment,e),K=n(e),v=p(e,"P",{}),oe(v).forEach(s),this.h()},h(){fe(i,"name","hf:doc:metadata"),fe(i,"content",$e)},m(e,t){je(document.head,i),l(e,C,t),l(e,F,t),l(e,I,t),c(h,e,t),l(e,P,t),l(e,y,t),l(e,N,t),l(e,j,t),l(e,Q,t),c(u,e,t),l(e,W,t),l(e,T,t),l(e,E,t),c($,e,t),l(e,G,t),l(e,d,t),l(e,X,t),c(z,e,t),l(e,R,t),l(e,b,t),l(e,x,t),c(U,e,t),l(e,q,t),l(e,Z,t),l(e,H,t),c(w,e,t),l(e,S,t),l(e,_,t),l(e,L,t),c(J,e,t),l(e,A,t),l(e,V,t),l(e,Y,t),c(B,e,t),l(e,K,t),l(e,v,t),O=!0},p:Me,i(e){O||(M(h.$$.fragment,e),M(u.$$.fragment,e),M($.$$.fragment,e),M(z.$$.fragment,e),M(U.$$.fragment,e),M(w.$$.fragment,e),M(J.$$.fragment,e),M(B.$$.fragment,e),O=!0)},o(e){g(h.$$.fragment,e),g(u.$$.fragment,e),g($.$$.fragment,e),g(z.$$.fragment,e),g(U.$$.fragment,e),g(w.$$.fragment,e),g(J.$$.fragment,e),g(B.$$.fragment,e),O=!1},d(e){e&&(s(C),s(F),s(I),s(P),s(y),s(N),s(j),s(Q),s(W),s(T),s(E),s(G),s(d),s(X),s(R),s(b),s(x),s(q),s(Z),s(H),s(S),s(_),s(L),s(A),s(V),s(Y),s(K),s(v)),s(i),k(h,e),k(u,e),k($,e),k(z,e),k(U,e),k(w,e),k(J,e),k(B,e)}}}const $e='{"title":"使用 🤗 Tokenizers 中的分词器","local":"使用--tokenizers-中的分词器","sections":[{"title":"直接从分词器对象加载","local":"直接从分词器对象加载","sections":[],"depth":2},{"title":"从 JSON 文件加载","local":"从-json-文件加载","sections":[],"depth":2}],"depth":1}';function de(te){return ge(()=>{new URLSearchParams(window.location.search).get("fw")}),[]}class we extends ke{constructor(i){super(),he(this,i,de,Te,ce,{})}}export{we as component};
