Skip to content

Commit 5db94fd

Browse files
authored
docs: add getting started section and remove outdated docs (#277)
* add getting started section to the docs * remove old examples * update example notebook * change to convert_to_dict * various and sundry edits
1 parent ee8739d commit 5db94fd

File tree

4 files changed

+165
-149
lines changed

4 files changed

+165
-149
lines changed

docs/source/examples.rst

Lines changed: 0 additions & 116 deletions
Original file line numberDiff line numberDiff line change
@@ -119,122 +119,6 @@ the project.
119119
120120
At this point, you're good to go to start labeling in the LabelStudio UI.
121121

122-
###########
123-
PDF Parsing
124-
###########
125-
126-
Once installed, you can try the following using the
127-
`layoutparser <https://arxiv.org/pdf/2103.15348.pdf>`_ paper as an example. The PDF
128-
of the paper is available in the
129-
`example-docs <https://github.com/Unstructured-IO/unstructured/tree/main/example-docs>`_ directory.
130-
131-
.. code:: python
132-
133-
from unstructured.documents.pdf import PDFDocument
134-
135-
doc = PDFDocument.from_file("example-docs/layout-parser-paper.pdf")
136-
print(doc)
137-
138-
At this point, ``print(doc)`` will print out a string representation of the PDF file. The
139-
first page of output looks like the following:
140-
141-
.. code:: python
142-
143-
"""
144-
LayoutParser : A Unified Toolkit for Deep Learning Based Document Image Analysis
145-
146-
Zejiang Shen 1 ( (cid:0) ), Ruochen Zhang 2 , Melissa Dell 3 , Benjamin Charles Germain Lee 4 , Jacob Carlson 3 , and
147-
Weining Li 5
148-
149-
Abstract. Recent advances in document image analysis (DIA) have been primarily driven by the application of neural
150-
networks. Ideally, research outcomes could be easily deployed in production and extended for further investigation.
151-
However, various factors like loosely organized codebases and sophisticated model configurations complicate the easy
152-
reuse of im- portant innovations by a wide audience. Though there have been on-going efforts to improve reusability and
153-
simplify deep learning (DL) model development in disciplines like natural language processing and computer vision, none
154-
of them are optimized for challenges in the domain of DIA. This represents a major gap in the existing toolkit, as DIA
155-
is central to academic research across a wide range of disciplines in the social sciences and humanities. This paper
156-
introduces LayoutParser , an open-source library for streamlining the usage of DL in DIA research and applica- tions.
157-
The core LayoutParser library comes with a set of simple and intuitive interfaces for applying and customizing DL models
158-
for layout de- tection, character recognition, and many other document processing tasks. To promote extensibility,
159-
LayoutParser also incorporates a community platform for sharing both pre-trained models and full document digiti- zation
160-
pipelines. We demonstrate that LayoutParser is helpful for both lightweight and large-scale digitization pipelines in
161-
real-word use cases. The library is publicly available at https://layout-parser.github.io
162-
163-
Keywords: Document Image Analysis · Deep Learning · Layout Analysis · Character Recognition · Open Source library ·
164-
Toolkit.
165-
166-
Introduction
167-
168-
Deep Learning(DL)-based approaches are the state-of-the-art for a wide range of document image analysis (DIA) tasks
169-
including document image classification [11,
170-
"""
171-
172-
The ``Document`` has a ``pages`` attribute consisting of ``Page`` object and the ``Page`` object
173-
has an ``elements`` attribute consisting of ``Element`` objects. Sub-types of the ``Element`` class
174-
represent different components of a document, such as ``NarrativeText`` and ``Title``. You can use
175-
these normalized elements to zero in on the components of a document you most care about.
176-
177-
############
178-
HTML Parsing
179-
############
180-
181-
You can parse an HTML document using the following command.
182-
183-
.. code:: python
184-
185-
from unstructured.documents.html import HTMLDocument
186-
187-
doc = HTMLDocument.from_file("example-docs/example-10k.html")
188-
print(doc.pages[2])
189-
190-
191-
You can also instantiate a document directly from an HTML string using the ``from_string`` method.
192-
The output of this will be the following:
193-
194-
.. code:: python
195-
196-
"""
197-
SPECIAL NOTE REGARDING FORWARD-LOOKING STATEMENTS
198-
This report contains statements that do not relate to historical or current facts but are “forward-looking” statements. These statements relate to analyses and other information based on forecasts of future results and estimates of amounts not yet determinable. These statements may also relate to future events or trends, our future prospects and proposed new products, services, developments or business strategies, among other things. These statements can generally (although not always) be identified by their use of terms and phrases such as anticipate, appear, believe, could, would, estimate, expect, indicate, intent, may, plan, predict, project, pursue, will continue and other similar terms and phrases, as well as the use of the future tense.
199-
200-
Actual results could differ materially from those expressed or implied in our forward-looking statements. Our future financial condition and results of operations, as well as any forward-looking statements, are subject to change and to inherent known and unknown risks and uncertainties. You should not assume at any point in the future that the forward-looking statements in this report are still valid. We do not intend, and undertake no obligation, to update our forward-looking statements to reflect future events or circumstances.
201-
"""
202-
203-
If you then run:
204-
205-
.. code:: python
206-
207-
doc.pages[2].elements
208-
209-
You'll get the following output, showing that the parser successfully differentiated between
210-
titles and narrative text.
211-
212-
.. code:: python
213-
214-
[<unstructured.documents.base.Title at 0x169cbe820>,
215-
<unstructured.documents.base.NarrativeText at 0x169cbe8e0>,
216-
<unstructured.documents.base.NarrativeText at 0x169cbe3a0>]
217-
218-
219-
Creating HTML from XML with XSLT
220-
--------------------------------
221-
222-
You can also convert XML files to HTML with the appropriate XSLT stylesheet. Note, XSLT
223-
converts arbitrary XML to XML, so there's no guarantee the result will be HTML. Ensure
224-
you're using a stylesheet designed to convert your specific XML to HTML. The workflow
225-
for reading in a document with an XSLT stylesheet is as follows:
226-
227-
.. code:: python
228-
229-
from unstructured.document.html import HTMLDocument
230-
231-
doc = HTMLDocument.from_file(filename="example-docs/factbook.xml",
232-
stylesheet="example-docs/factbook.xsl")
233-
234-
If you read from a stylesheet ``HTMLDocument`` will use the ``etree.XMLParser`` by default
235-
instead of the ``etree.HTMLParser`` because ``HTMLDocument`` assumes you want to convert
236-
your raw XML to HTML.
237-
238122

239123
##################################
240124
Extracting Metadata from Documents

docs/source/getting_started.rst

Lines changed: 161 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,161 @@
1+
Getting Started
2+
===============
3+
4+
The following section will cover basic concepts and usage patterns in ``unstructured``.
5+
After reading this section, you should be able to:
6+
7+
* Partitioning a document with the ``partition`` function.
8+
* Understand how documents are structured in ``unstructured``.
9+
* Convert a document to a dictionary and/or save it as a JSON.
10+
11+
The example documents in this section come from the
12+
`example-docs <https://github.com/Unstructured-IO/unstructured/tree/main/example-docs>`_
13+
directory in the ``unstructured`` repo.
14+
15+
Before running the code in this make sure you've installed the ``unstructured`` library
16+
and all dependencies using the instructions in the **Quick Start** section.
17+
18+
19+
#######################
20+
Partitioning a document
21+
#######################
22+
23+
In this section, we'll cut right to the chase and get to the most important part of the library: partitioning a document.
24+
The goal of document partitioning is to read in a source document, split the document into sections, categorize those sections,
25+
and extract the text associated with those sections. Depending on the document type, unstructured uses different methods for
26+
partitioning a document. We'll cover those in a later section. For now, we'll use the simplest API in the library,
27+
the ``partition`` function. The ``partition`` function will detect the filetype of the source document and route it to the appropriate
28+
partitioning function. You can try out the partition function by running the cell below.
29+
30+
31+
.. code:: python
32+
33+
34+
from unstructured.partition.auto import partition
35+
36+
elements = partition(filename="example-10k.html")
37+
38+
39+
You can also pass in a file as a file-like object using the following workflow:
40+
41+
42+
.. code:: python
43+
44+
with open("example-10k.html", "rb") as f:
45+
elements = partition(file=f)
46+
47+
48+
The ``partition`` function uses `libmagic <https://formulae.brew.sh/formula/libmagic>`_ for filetype detection. If ``libmagic`` is
49+
not present and the user passes a filename, ``partition`` falls back to detecting the filetype using the file extension.
50+
``libmagic`` is required if you'd lke to pass a file-like object to ``partition``.
51+
We highly recommend installing ``libmagic`` and you may observe different file detection behaviors
52+
if ``libmagic`` is not installed`.
53+
54+
55+
##################
56+
Document elements
57+
##################
58+
59+
60+
When we partition a document, the output is a list of document ``Element`` objects.
61+
These element objects represent different components of the source document. Currently, the ``unstructured`` library supports the following element types:
62+
63+
64+
65+
* ``Element``
66+
* ``Text``
67+
* ``FigureCaption``
68+
* ``NarrativeText``
69+
* ``ListItem``
70+
* ``Title``
71+
* ``Address``
72+
* ``PageBreak``
73+
* ``CheckBox``
74+
* ``Image``
75+
76+
77+
Other element types that we will add in the future include tables and figures.
78+
Different partitioning functions use different methods for determining the element type and extracting the associated content.
79+
Document elements have a ``str`` representation. You can print them using the snippet below.
80+
81+
82+
83+
.. code:: python
84+
85+
elements = partition(filename="example-10k.html")
86+
87+
for element in elements[:5]:
88+
print(element)
89+
print("\n")
90+
91+
92+
One helpful aspect of document elements is that they allow you to cut a document down to the elements that you need for your particular use case.
93+
For example, if you're training a summarization model you may only want to include narrative text for model training.
94+
You'll notice that the output above includes a lot of titles and other content that may not be suitable for a summarization model.
95+
The following code shows how you can limit your output to only narrative text with at least two sentences. As you can see, the output now only contains narrative text.
96+
97+
98+
99+
.. code:: python
100+
101+
from unstructured.documents.elements import NarrativeText
102+
from unstructured.partition.text_type import sentence_count
103+
104+
for element in elements[:100]:
105+
if isinstance(element, NarrativeText) and sentence_count(element.text) > 2:
106+
print(element)
107+
print("\n")
108+
109+
110+
###########################################
111+
Converting elements to a dictionary or JSON
112+
###########################################
113+
114+
The final step in the process for most users is to convert the output to JSON.
115+
You can convert a list of document elements to a list of dictionaries using the ``convert_to_dict`` function.
116+
The workflow for using ``convert_to_dict`` appears below.
117+
118+
119+
.. code:: python
120+
121+
122+
from unstructured.staging.base import convert_to_dict
123+
124+
convert_to_dict(elements)
125+
126+
127+
The ``unstructured`` library also includes utilities for saving a list of elements to JSON and reading
128+
a list of elements from JSON, as seen in the snippet below
129+
130+
131+
132+
.. code:: python
133+
134+
from unstructured.staging.base import elements_to_json, elements_from_json
135+
136+
137+
filename = "outputs.json"
138+
elements_to_json(elements, filename=filename)
139+
elements = elements_from_json(filename=filename)
140+
141+
142+
143+
##################
144+
Wrapping it all up
145+
##################
146+
147+
To conclude, the basic workflow for reading in a document and converting it to a JSON in ``unstructured``
148+
looks like the following:
149+
150+
151+
152+
.. code:: python
153+
154+
from unstructured.partition.auto import partition
155+
from unstructured.staging.base import elements_to_json
156+
157+
input_filename = "example-10k.html"
158+
output_filename = "outputs.json"
159+
160+
elements = partition(filename=input_filename)
161+
elements_to_json(elements, filename=output_filename)

docs/source/index.rst

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@ Library Documentation
2323
:hidden:
2424

2525
installing
26+
getting_started
2627
elements
2728
bricks
2829
examples

examples/training/0-Core Concepts.ipynb

Lines changed: 3 additions & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -274,7 +274,7 @@
274274
"source": [
275275
"## Converting to a dictionary <a id=\"dict\"></a>\n",
276276
"\n",
277-
"The final step in the process for most users is to convert the output to JSON. You can convert a list of document elements to a list of dictionaries using the `convert_to_isd` function. ISD stands for \"initial structured data\", our common format for representing text data. The workflow for using `convert_to_isd` appears below."
277+
"The final step in the process for most users is to convert the output to JSON. You can convert a list of document elements to a list of dictionaries using the `convert_to_dict` function. The workflow for using `convert_to_dict` appears below."
278278
]
279279
},
280280
{
@@ -284,7 +284,7 @@
284284
"metadata": {},
285285
"outputs": [],
286286
"source": [
287-
"from unstructured.staging.base import convert_to_isd"
287+
"from unstructured.staging.base import convert_to_dict"
288288
]
289289
},
290290
{
@@ -1565,38 +1565,8 @@
15651565
}
15661566
],
15671567
"source": [
1568-
"convert_to_isd(elements)"
1568+
"convert_to_dict(elements)"
15691569
]
1570-
},
1571-
{
1572-
"cell_type": "code",
1573-
"execution_count": 10,
1574-
"id": "16f028ec",
1575-
"metadata": {},
1576-
"outputs": [
1577-
{
1578-
"ename": "NameError",
1579-
"evalue": "name 'output' is not defined",
1580-
"output_type": "error",
1581-
"traceback": [
1582-
"\u001b[0;31m---------------------------------------------------------------------------\u001b[0m",
1583-
"\u001b[0;31mNameError\u001b[0m Traceback (most recent call last)",
1584-
"Cell \u001b[0;32mIn [10], line 1\u001b[0m\n\u001b[0;32m----> 1\u001b[0m \u001b[43moutput\u001b[49m[:\u001b[38;5;241m10\u001b[39m]\n",
1585-
"\u001b[0;31mNameError\u001b[0m: name 'output' is not defined"
1586-
]
1587-
}
1588-
],
1589-
"source": [
1590-
"output[:10]"
1591-
]
1592-
},
1593-
{
1594-
"cell_type": "code",
1595-
"execution_count": null,
1596-
"id": "d9f24b63",
1597-
"metadata": {},
1598-
"outputs": [],
1599-
"source": []
16001570
}
16011571
],
16021572
"metadata": {

0 commit comments

Comments
 (0)