-
Notifications
You must be signed in to change notification settings - Fork 4
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Implement Analogy and Similarity Queries.
- Loading branch information
Showing
6 changed files
with
322 additions
and
11 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -71,6 +71,10 @@ def run(self): | |
["src/finalfusion/subword/explicit_indexer.c"]) | ||
extensions = [hash_indexers, ngrams, explicit_indexer] | ||
|
||
install_requires = ["numpy", "toml"] | ||
if sys.version_info.major == 3 and sys.version_info.minor == 6: | ||
install_requires.append("dataclasses") | ||
|
||
setup(name='finalfusion', | ||
author="Sebastian Pütz <[email protected]>, Daniël de Kok <[email protected]>", | ||
classifiers=[ | ||
|
@@ -81,7 +85,7 @@ def run(self): | |
cmdclass={'build_ext': cython_build_ext}, | ||
description="Interface to finalfusion embeddings", | ||
ext_modules=extensions, | ||
install_requires=["numpy", "toml"], | ||
install_requires=install_requires, | ||
license='BlueOak-1.0.0', | ||
packages=find_packages('src'), | ||
include_package_data=True, | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Binary file not shown.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,61 @@ | ||
import pytest | ||
|
||
ANALOGY_ORDER = [ | ||
"Deutschland", | ||
"Westdeutschland", | ||
"Sachsen", | ||
"Mitteldeutschland", | ||
"Brandenburg", | ||
"Polen", | ||
"Norddeutschland", | ||
"Dänemark", | ||
"Schleswig-Holstein", | ||
"Österreich", | ||
"Bayern", | ||
"Thüringen", | ||
"Bundesrepublik", | ||
"Ostdeutschland", | ||
"Preußen", | ||
"Deutschen", | ||
"Hessen", | ||
"Potsdam", | ||
"Mecklenburg", | ||
"Niedersachsen", | ||
"Hamburg", | ||
"Süddeutschland", | ||
"Bremen", | ||
"Russland", | ||
"Deutschlands", | ||
"BRD", | ||
"Litauen", | ||
"Mecklenburg-Vorpommern", | ||
"DDR", | ||
"West-Berlin", | ||
"Saarland", | ||
"Lettland", | ||
"Hannover", | ||
"Rostock", | ||
"Sachsen-Anhalt", | ||
"Pommern", | ||
"Schweden", | ||
"Deutsche", | ||
"deutschen", | ||
"Westfalen", | ||
] | ||
|
||
|
||
def test_analogies(analogy_fifu): | ||
for idx, analogy in enumerate( | ||
analogy_fifu.analogy("Paris", "Frankreich", "Berlin", 40)): | ||
assert ANALOGY_ORDER[idx] == analogy.word | ||
|
||
assert analogy_fifu.analogy("Paris", "Frankreich", "Paris", 1, | ||
{"Paris"})[0].word == "Frankreich" | ||
assert analogy_fifu.analogy("Paris", "Frankreich", "Paris", | ||
1)[0].word != "Frankreich" | ||
assert analogy_fifu.analogy("Frankreich", "Frankreich", "Frankreich", 1, | ||
set())[0].word == "Frankreich" | ||
assert analogy_fifu.analogy("Frankreich", "Frankreich", "Frankreich", 1, | ||
{"Frankreich"})[0].word != "Frankreich" | ||
|
||
assert analogy_fifu.analogy("Paris", "OOV", "Paris", 1) is None |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,90 @@ | ||
import pytest | ||
import numpy | ||
|
||
SIMILARITY_ORDER_STUTTGART_10 = [ | ||
"Karlsruhe", | ||
"Mannheim", | ||
"München", | ||
"Darmstadt", | ||
"Heidelberg", | ||
"Wiesbaden", | ||
"Kassel", | ||
"Düsseldorf", | ||
"Leipzig", | ||
"Berlin", | ||
] | ||
|
||
SIMILARITY_ORDER = [ | ||
"Potsdam", | ||
"Hamburg", | ||
"Leipzig", | ||
"Dresden", | ||
"München", | ||
"Düsseldorf", | ||
"Bonn", | ||
"Stuttgart", | ||
"Weimar", | ||
"Berlin-Charlottenburg", | ||
"Rostock", | ||
"Karlsruhe", | ||
"Chemnitz", | ||
"Breslau", | ||
"Wiesbaden", | ||
"Hannover", | ||
"Mannheim", | ||
"Kassel", | ||
"Köln", | ||
"Danzig", | ||
"Erfurt", | ||
"Dessau", | ||
"Bremen", | ||
"Charlottenburg", | ||
"Magdeburg", | ||
"Neuruppin", | ||
"Darmstadt", | ||
"Jena", | ||
"Wien", | ||
"Heidelberg", | ||
"Dortmund", | ||
"Stettin", | ||
"Schwerin", | ||
"Neubrandenburg", | ||
"Greifswald", | ||
"Göttingen", | ||
"Braunschweig", | ||
"Berliner", | ||
"Warschau", | ||
"Berlin-Spandau", | ||
] | ||
|
||
|
||
def test_similarity_berlin_40(similarity_fifu): | ||
for idx, sim in enumerate(similarity_fifu.word_similarity("Berlin", 40)): | ||
assert SIMILARITY_ORDER[idx] == sim.word | ||
|
||
|
||
def test_similarity_stuttgart_10(similarity_fifu): | ||
for idx, sim in enumerate(similarity_fifu.word_similarity("Stuttgart", | ||
10)): | ||
assert SIMILARITY_ORDER_STUTTGART_10[idx] == sim.word | ||
|
||
|
||
def test_embedding_similarity_stuttgart_10(similarity_fifu): | ||
stuttgart = similarity_fifu.embedding("Stuttgart") | ||
sims = similarity_fifu.embedding_similarity(stuttgart, k=10) | ||
assert sims[0].word == "Stuttgart" | ||
|
||
for idx, sim in enumerate(sims[1:]): | ||
assert SIMILARITY_ORDER_STUTTGART_10[idx] == sim.word | ||
|
||
for idx, sim in enumerate( | ||
similarity_fifu.embedding_similarity(stuttgart, | ||
skip={"Stuttgart"}, | ||
k=10)): | ||
assert SIMILARITY_ORDER_STUTTGART_10[idx] == sim.word | ||
|
||
|
||
def test_embedding_similarity_incompatible_shapes(similarity_fifu): | ||
incompatible_embed = numpy.ones(1, dtype=numpy.float32) | ||
with pytest.raises(ValueError): | ||
similarity_fifu.embedding_similarity(incompatible_embed) |