2016-10-31 21:04:15 +03:00
|
|
|
//- 💫 DOCS > USAGE > PROCESSING TEXT
|
|
|
|
|
|
|
|
include ../../_includes/_mixins
|
|
|
|
|
2017-05-26 14:17:48 +03:00
|
|
|
+under-construction
|
|
|
|
|
2016-10-31 21:04:15 +03:00
|
|
|
+h(2, "multithreading") Multi-threading with #[code .pipe()]
|
|
|
|
|
|
|
|
p
|
|
|
|
| If you have a sequence of documents to process, you should use the
|
2017-05-26 14:17:48 +03:00
|
|
|
| #[+api("language#pipe") #[code Language.pipe()]] method. The method takes
|
|
|
|
| an iterator of texts, and accumulates an internal buffer,
|
2016-10-31 21:04:15 +03:00
|
|
|
| which it works on in parallel. It then yields the documents in order,
|
|
|
|
| one-by-one. After a long and bitter struggle, the global interpreter
|
|
|
|
| lock was freed around spaCy's main parsing loop in v0.100.3. This means
|
2017-05-26 14:17:48 +03:00
|
|
|
| that #[code .pipe()] will be significantly faster in most
|
2016-11-20 20:02:45 +03:00
|
|
|
| practical situations, because it allows shared memory parallelism.
|
2016-10-31 21:04:15 +03:00
|
|
|
|
|
|
|
+code.
|
|
|
|
for doc in nlp.pipe(texts, batch_size=10000, n_threads=3):
|
|
|
|
pass
|
|
|
|
|
|
|
|
p
|
|
|
|
| To make full use of the #[code .pipe()] function, you might want to
|
2017-05-26 14:17:48 +03:00
|
|
|
| brush up on #[strong Python generators]. Here are a few quick hints:
|
2016-10-31 21:04:15 +03:00
|
|
|
|
|
|
|
+list
|
|
|
|
+item
|
2017-05-26 14:17:48 +03:00
|
|
|
| Generator comprehensions can be written as
|
|
|
|
| #[code (item for item in sequence)].
|
2016-10-31 21:04:15 +03:00
|
|
|
|
|
|
|
+item
|
2017-05-26 14:17:48 +03:00
|
|
|
| The
|
|
|
|
| #[+a("https://docs.python.org/2/library/itertools.html") #[code itertools] built-in library]
|
|
|
|
| and the
|
|
|
|
| #[+a("https://github.com/pytoolz/cytoolz") #[code cytoolz] package]
|
|
|
|
| provide a lot of handy #[strong generator tools].
|
2016-10-31 21:04:15 +03:00
|
|
|
|
|
|
|
+item
|
|
|
|
| Often you'll have an input stream that pairs text with some
|
2017-05-26 14:17:48 +03:00
|
|
|
| important meta data, e.g. a JSON document. To
|
|
|
|
| #[strong pair up the meta data] with the processed #[code Doc]
|
|
|
|
| object, you should use the #[code itertools.tee] function to split
|
|
|
|
| the generator in two, and then #[code izip] the extra stream to the
|
|
|
|
| document stream.
|
2016-10-31 21:04:15 +03:00
|
|
|
|
|
|
|
+h(2, "own-annotations") Bringing your own annotations
|
|
|
|
|
|
|
|
p
|
|
|
|
| spaCy generally assumes by default that your data is raw text. However,
|
|
|
|
| sometimes your data is partially annotated, e.g. with pre-existing
|
|
|
|
| tokenization, part-of-speech tags, etc. The most common situation is
|
|
|
|
| that you have pre-defined tokenization. If you have a list of strings,
|
|
|
|
| you can create a #[code Doc] object directly. Optionally, you can also
|
|
|
|
| specify a list of boolean values, indicating whether each word has a
|
|
|
|
| subsequent space.
|
|
|
|
|
|
|
|
+code.
|
|
|
|
doc = Doc(nlp.vocab, words=[u'Hello', u',', u'world', u'!'], spaces=[False, True, False, False])
|
|
|
|
|
|
|
|
p
|
|
|
|
| If provided, the spaces list must be the same length as the words list.
|
|
|
|
| The spaces list affects the #[code doc.text], #[code span.text],
|
|
|
|
| #[code token.idx], #[code span.start_char] and #[code span.end_char]
|
|
|
|
| attributes. If you don't provide a #[code spaces] sequence, spaCy will
|
|
|
|
| assume that all words are whitespace delimited.
|
|
|
|
|
|
|
|
+code.
|
|
|
|
good_spaces = Doc(nlp.vocab, words=[u'Hello', u',', u'world', u'!'], spaces=[False, True, False, False])
|
|
|
|
bad_spaces = Doc(nlp.vocab, words=[u'Hello', u',', u'world', u'!'])
|
|
|
|
assert bad_spaces.text == u'Hello , world !'
|
|
|
|
assert good_spaces.text == u'Hello, world!'
|
|
|
|
|
|
|
|
p
|
|
|
|
| Once you have a #[+api("doc") #[code Doc]] object, you can write to its
|
|
|
|
| attributes to set the part-of-speech tags, syntactic dependencies, named
|
|
|
|
| entities and other attributes. For details, see the respective usage
|
|
|
|
| pages.
|