Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Import Documents: Read metas as the right type #677

Merged
merged 3 commits into from
Jul 21, 2021
Merged
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
22 changes: 16 additions & 6 deletions orangecontrib/text/import_documents.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import contextlib
import datetime
import fnmatch
import logging
import os
Expand Down Expand Up @@ -29,8 +30,10 @@

import serverfiles

from Orange.data import DiscreteVariable, Domain, StringVariable
from Orange.data.io import detect_encoding, UrlReader as CoreUrlReader
from Orange.data import DiscreteVariable, Domain, StringVariable, \
guess_data_type
from Orange.data.io import detect_encoding, sanitize_variable,\
UrlReader as CoreUrlReader
from Orange.data.util import get_unique_names
from Orange.util import Registry

Expand Down Expand Up @@ -284,6 +287,8 @@ def _read_meta_data(self):
content = data.content
if isinstance(content, dict):
content = pd.DataFrame(content, index=[0])
# if reader is YamlMetaReader:
# content = content.replace("None", np.nan)
Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@VesnaT Should we keep this? Currently, null values in yamls are treated as "None" (strings).

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'd rather put fixes like this inside the YamlMetaReader.
Besides, I'm not sure the fix works correctly for theStringVariables.

Copy link
Contributor

@VesnaT VesnaT Jul 21, 2021

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Something like this could work:

class YamlMetaReader(Reader):
    ext = [".yaml"]

    def read_file(self):
        with open(self.path, "r") as f:
            self.content = yaml.safe_load(f)
            for k in self.content:
                if self.content[k] is None:
                    self.content[k] = ""
   

meta_dfs.append(content)
else:
errors.append(error)
Expand Down Expand Up @@ -345,13 +350,18 @@ def _add_metadata(self, corpus: Corpus) -> Corpus:
if len(df.index.drop_duplicates()) != len(df.index):
df = df[~df.index.duplicated(keep='first')]
filtered = df.reindex(path_column)
for column in filtered.columns:
for name, column in filtered.iteritems():
data = column.astype(str).values
val_map, vals, var_type = guess_data_type(data)
values, variable = sanitize_variable(val_map, vals, data,
var_type, {},
name=get_unique_names(
corpus.domain, name))
corpus = corpus.add_column(
StringVariable(get_unique_names(corpus.domain, column)),
filtered[column].to_numpy(),
variable,
values,
to_metas=True
)

return corpus

@staticmethod
Expand Down
8 changes: 4 additions & 4 deletions orangecontrib/text/tests/test_import_documents.py
Original file line number Diff line number Diff line change
Expand Up @@ -130,16 +130,16 @@ def test_run_url(self):
importer = ImportDocuments(path, True)
corpus2, _ = importer.run()
self.assertGreater(len(corpus1), 0)
self.assertEqual(corpus1.metas[mask].tolist(),
corpus2.metas[mask].tolist())
np.testing.assert_array_equal(corpus1.metas[mask].tolist(),
corpus2.metas[mask].tolist())

path = "http://file.biolab.si/text-semantics/data" \
"/predlogi-vladi-sample"
importer = ImportDocuments(path, True)
corpus3, _ = importer.run()
self.assertGreater(len(corpus2), 0)
self.assertEqual(corpus1.metas[mask].tolist(),
corpus3.metas[mask].tolist())
np.testing.assert_array_equal(corpus1.metas[mask].tolist(),
corpus3.metas[mask].tolist())

def test_run_url_special_characters(self):
path = "http://file.biolab.si/text-semantics/data/" \
Expand Down