Skip to content
GitLab
Explore
Sign in
Primary navigation
Search or go to…
Project
Semantic Router
Manage
Activity
Members
Labels
Plan
Issues
Issue boards
Milestones
Iterations
Wiki
Code
Merge requests
Repository
Branches
Commits
Tags
Repository graph
Compare revisions
Snippets
Locked files
Build
Pipelines
Jobs
Pipeline schedules
Artifacts
Deploy
Releases
Package registry
Container registry
Model registry
Operate
Environments
Terraform modules
Monitor
Incidents
Service Desk
Analyze
Value stream analytics
Contributor analytics
CI/CD analytics
Repository analytics
Code review analytics
Issue analytics
Model experiments
Help
Help
Support
GitLab documentation
Compare GitLab plans
Community forum
Contribute to GitLab
Provide feedback
Terms and privacy
Keyboard shortcuts
?
Snippets
Groups
Projects
Show more breadcrumbs
mirrored_repos
MachineLearning
aurelio-labs
Semantic Router
Commits
73258344
Commit
73258344
authored
1 year ago
by
“Daniel Griffiths”
Browse files
Options
Downloads
Patches
Plain Diff
removed none types for mypy
parent
a8c64a67
No related branches found
No related tags found
No related merge requests found
Changes
1
Hide whitespace changes
Inline
Side-by-side
Showing
1 changed file
semantic_router/encoders/tfidf.py
+9
-5
9 additions, 5 deletions
semantic_router/encoders/tfidf.py
with
9 additions
and
5 deletions
semantic_router/encoders/tfidf.py
+
9
−
5
View file @
73258344
...
@@ -7,16 +7,16 @@ import string
...
@@ -7,16 +7,16 @@ import string
class
TfidfEncoder
(
BaseEncoder
):
class
TfidfEncoder
(
BaseEncoder
):
idf
:
dict
|
None
=
None
idf
:
np
.
ndarray
word_index
:
dict
|
None
=
None
word_index
:
dict
def
__init__
(
self
,
name
:
str
=
"
tfidf
"
):
def
__init__
(
self
,
name
:
str
=
"
tfidf
"
):
super
().
__init__
(
name
=
name
)
super
().
__init__
(
name
=
name
)
self
.
word_index
=
None
self
.
word_index
=
{}
self
.
idf
=
None
self
.
idf
=
np
.
array
([])
def
__call__
(
self
,
docs
:
list
[
str
])
->
list
[
list
[
float
]]:
def
__call__
(
self
,
docs
:
list
[
str
])
->
list
[
list
[
float
]]:
if
self
.
word_index
is
None
or
self
.
idf
is
None
:
if
len
(
self
.
word_index
)
==
0
or
self
.
idf
.
size
==
0
:
raise
ValueError
(
"
Vectorizer is not initialized.
"
)
raise
ValueError
(
"
Vectorizer is not initialized.
"
)
if
len
(
docs
)
==
0
:
if
len
(
docs
)
==
0
:
raise
ValueError
(
"
No documents to encode.
"
)
raise
ValueError
(
"
No documents to encode.
"
)
...
@@ -43,6 +43,8 @@ class TfidfEncoder(BaseEncoder):
...
@@ -43,6 +43,8 @@ class TfidfEncoder(BaseEncoder):
return
word_index
return
word_index
def
_compute_tf
(
self
,
docs
:
list
[
str
])
->
np
.
ndarray
:
def
_compute_tf
(
self
,
docs
:
list
[
str
])
->
np
.
ndarray
:
if
len
(
self
.
word_index
)
==
0
:
raise
ValueError
(
"
Word index is not initialized.
"
)
tf
=
np
.
zeros
((
len
(
docs
),
len
(
self
.
word_index
)))
tf
=
np
.
zeros
((
len
(
docs
),
len
(
self
.
word_index
)))
for
i
,
doc
in
enumerate
(
docs
):
for
i
,
doc
in
enumerate
(
docs
):
word_counts
=
Counter
(
doc
.
split
())
word_counts
=
Counter
(
doc
.
split
())
...
@@ -54,6 +56,8 @@ class TfidfEncoder(BaseEncoder):
...
@@ -54,6 +56,8 @@ class TfidfEncoder(BaseEncoder):
return
tf
return
tf
def
_compute_idf
(
self
,
docs
:
list
[
str
])
->
np
.
ndarray
:
def
_compute_idf
(
self
,
docs
:
list
[
str
])
->
np
.
ndarray
:
if
len
(
self
.
word_index
)
==
0
:
raise
ValueError
(
"
Word index is not initialized.
"
)
idf
=
np
.
zeros
(
len
(
self
.
word_index
))
idf
=
np
.
zeros
(
len
(
self
.
word_index
))
for
doc
in
docs
:
for
doc
in
docs
:
words
=
set
(
doc
.
split
())
words
=
set
(
doc
.
split
())
...
...
This diff is collapsed.
Click to expand it.
Preview
0%
Loading
Try again
or
attach a new file
.
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Save comment
Cancel
Please
register
or
sign in
to comment