<s>
The	O
bag-of-words	B-General_Concept
model	I-General_Concept
is	O
a	O
simplifying	O
representation	O
used	O
in	O
natural	B-Language
language	I-Language
processing	I-Language
and	O
information	B-Library
retrieval	I-Library
(	O
IR	O
)	O
.	O
</s>
<s>
In	O
this	O
model	O
,	O
a	O
text	O
(	O
such	O
as	O
a	O
sentence	O
or	O
a	O
document	O
)	O
is	O
represented	O
as	O
the	O
bag	O
(	O
multiset	B-Language
)	O
of	O
its	O
words	O
,	O
disregarding	O
grammar	O
and	O
even	O
word	O
order	O
but	O
keeping	O
multiplicity	O
.	O
</s>
<s>
The	O
bag-of-words	B-General_Concept
model	I-General_Concept
has	O
also	O
been	O
used	B-General_Concept
for	I-General_Concept
computer	I-General_Concept
vision	I-General_Concept
.	O
</s>
<s>
The	O
bag-of-words	B-General_Concept
model	I-General_Concept
is	O
commonly	O
used	O
in	O
methods	O
of	O
document	B-Algorithm
classification	I-Algorithm
where	O
the	O
(	O
frequency	O
of	O
)	O
occurrence	O
of	O
each	O
word	O
is	O
used	O
as	O
a	O
feature	B-Algorithm
for	O
training	O
a	O
classifier	B-General_Concept
.	O
</s>
<s>
An	O
early	O
reference	O
to	O
"	O
bag	B-General_Concept
of	I-General_Concept
words	I-General_Concept
"	O
in	O
a	O
linguistic	O
context	O
can	O
be	O
found	O
in	O
Zellig	O
Harris	O
's	O
1954	O
article	O
on	O
Distributional	O
Structure	O
.	O
</s>
<s>
The	O
Bag-of-words	B-General_Concept
model	I-General_Concept
is	O
one	O
example	O
of	O
a	O
Vector	O
space	O
model	O
.	O
</s>
<s>
The	O
following	O
models	O
a	O
text	O
document	O
using	O
bag-of-words	B-General_Concept
.	O
</s>
<s>
Representing	O
each	O
bag-of-words	B-General_Concept
as	O
a	O
JSON	B-General_Concept
object	I-General_Concept
,	O
and	O
attributing	O
to	O
the	O
respective	O
JavaScript	B-Language
variable	O
:	O
</s>
<s>
It	O
is	O
also	O
what	O
we	O
expect	O
from	O
a	O
strict	O
JSON	B-General_Concept
object	I-General_Concept
representation	O
.	O
</s>
<s>
its	O
JavaScript	B-Language
representation	O
will	O
be	O
:	O
</s>
<s>
So	O
,	O
as	O
we	O
see	O
in	O
the	O
bag	B-Language
algebra	I-Language
,	O
the	O
"	O
union	O
"	O
of	O
two	O
documents	O
in	O
the	O
bags-of-words	O
representation	O
is	O
,	O
formally	O
,	O
the	O
disjoint	O
union	O
,	O
summing	O
the	O
multiplicities	O
of	O
each	O
element	O
.	O
</s>
<s>
In	O
practice	O
,	O
the	O
Bag-of-words	B-General_Concept
model	I-General_Concept
is	O
mainly	O
used	O
as	O
a	O
tool	O
of	O
feature	B-Algorithm
generation	O
.	O
</s>
<s>
After	O
transforming	O
the	O
text	O
into	O
a	O
"	O
bag	B-General_Concept
of	I-General_Concept
words	I-General_Concept
"	O
,	O
we	O
can	O
calculate	O
various	O
measures	O
to	O
characterize	O
the	O
text	O
.	O
</s>
<s>
The	O
most	O
common	O
type	O
of	O
characteristics	O
,	O
or	O
features	O
calculated	O
from	O
the	O
Bag-of-words	B-General_Concept
model	I-General_Concept
is	O
term	O
frequency	O
,	O
namely	O
,	O
the	O
number	O
of	O
times	O
a	O
term	O
appears	O
in	O
the	O
text	O
.	O
</s>
<s>
This	O
is	O
just	O
the	O
main	O
feature	B-Algorithm
of	O
the	O
Bag-of-words	B-General_Concept
model	I-General_Concept
.	O
</s>
<s>
Additionally	O
,	O
for	O
the	O
specific	O
purpose	O
of	O
classification	O
,	O
supervised	B-General_Concept
alternatives	O
have	O
been	O
developed	O
to	O
account	O
for	O
the	O
class	O
label	O
of	O
a	O
document	O
.	O
</s>
<s>
Lastly	O
,	O
binary	O
(	O
presence/absence	O
or	O
1/0	O
)	O
weighting	O
is	O
used	O
in	O
place	O
of	O
frequencies	O
for	O
some	O
problems	O
(	O
e.g.	O
,	O
this	O
option	O
is	O
implemented	O
in	O
the	O
WEKA	B-Language
machine	O
learning	O
software	O
system	O
)	O
.	O
</s>
<s>
The	O
Bag-of-words	B-General_Concept
model	I-General_Concept
is	O
an	O
orderless	O
document	O
representation	O
—	O
only	O
the	O
counts	O
of	O
words	O
matter	O
.	O
</s>
<s>
Mary	O
likes	O
movies	O
too	O
"	O
,	O
the	O
bag-of-words	B-General_Concept
representation	O
will	O
not	O
reveal	O
that	O
the	O
verb	O
"	O
likes	O
"	O
always	O
follows	O
a	O
person	O
's	O
name	O
in	O
this	O
text	O
.	O
</s>
<s>
As	O
an	O
alternative	O
,	O
the	O
n-gram	B-Language
model	O
can	O
store	O
this	O
spatial	O
information	O
.	O
</s>
<s>
Conceptually	O
,	O
we	O
can	O
view	O
bag-of-word	O
model	O
as	O
a	O
special	O
case	O
of	O
the	O
n-gram	B-Language
model	O
,	O
with	O
n	O
=	O
1	O
.	O
</s>
<s>
For	O
n1	O
the	O
model	O
is	O
named	O
w-shingling	B-General_Concept
(	O
where	O
w	O
is	O
equivalent	O
to	O
n	O
denoting	O
the	O
number	O
of	O
grouped	O
words	O
)	O
.	O
</s>
<s>
See	O
language	B-Language
model	I-Language
for	O
a	O
more	O
detailed	O
discussion	O
.	O
</s>
<s>
A	O
common	O
alternative	O
to	O
using	O
dictionaries	O
is	O
the	O
hashing	B-Algorithm
trick	I-Algorithm
,	O
where	O
words	O
are	O
mapped	O
directly	O
to	O
indices	O
with	O
a	O
hashing	O
function	O
.	O
</s>
<s>
In	O
practice	O
,	O
hashing	O
simplifies	O
the	O
implementation	O
of	O
bag-of-words	B-General_Concept
models	O
and	O
improves	O
scalability	O
.	O
</s>
<s>
In	O
Bayesian	B-Application
spam	I-Application
filtering	I-Application
,	O
an	O
e-mail	O
message	O
is	O
modeled	O
as	O
an	O
unordered	O
collection	O
of	O
words	O
selected	O
from	O
one	O
of	O
two	O
probability	O
distributions	O
:	O
one	O
representing	O
spam	O
and	O
one	O
representing	O
legitimate	O
e-mail	O
(	O
"	O
ham	O
"	O
)	O
.	O
</s>
<s>
To	O
classify	O
an	O
e-mail	O
message	O
,	O
the	O
Bayesian	B-Application
spam	I-Application
filter	I-Application
assumes	O
that	O
the	O
message	O
is	O
a	O
pile	O
of	O
words	O
that	O
has	O
been	O
poured	O
out	O
randomly	O
from	O
one	O
of	O
the	O
two	O
bags	O
,	O
and	O
uses	O
Bayesian	O
probability	O
to	O
determine	O
which	O
bag	O
it	O
is	O
more	O
likely	O
to	O
be	O
in	O
.	O
</s>
