-
-
Notifications
You must be signed in to change notification settings - Fork 85
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
[ENH] Import documents: Import from URL #637
Merged
Merged
Changes from all commits
Commits
Show all changes
3 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
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,141 @@ | ||
import unittest | ||
from unittest.mock import patch | ||
|
||
import numpy as np | ||
import pandas as pd | ||
|
||
from orangecontrib.text.import_documents import ImportDocuments, UrlReader, \ | ||
TxtReader, TextData | ||
|
||
|
||
class TestUrlReader(unittest.TestCase): | ||
def test_init(self): | ||
path = "http://dummy.server.com/data/foo.txt" | ||
reader = UrlReader(path) | ||
self.assertEqual(reader.filename, path) | ||
self.assertEqual(reader.path, path) | ||
|
||
def test_get_reader(self): | ||
path = "http://dummy.server.com/data/foo.txt" | ||
reader = UrlReader.get_reader(path) | ||
self.assertIsInstance(reader, TxtReader) | ||
|
||
def test_read(self): | ||
path = "http://file.biolab.si/text-semantics/data/semeval/C-1.txt" | ||
reader = UrlReader(path) | ||
textdata, error = reader.read() | ||
self.assertIsInstance(textdata, TextData) | ||
self.assertEqual(textdata.name, "C-1") | ||
self.assertEqual(textdata.path, path) | ||
self.assertEqual(textdata.ext, [".txt"]) | ||
self.assertEqual(textdata.category, "semeval") | ||
self.assertTrue(textdata.content.startswith("On The Complexity of Co")) | ||
self.assertEqual(error, "") | ||
|
||
def test_read_file(self): | ||
path = "http://file.biolab.si/text-semantics/data/elektrotehniski-" \ | ||
"vestnik-clanki/detektiranje-utrdb-v-šahu-.txt" | ||
reader = UrlReader(path) | ||
reader.read_file() | ||
self.assertIsInstance(reader.content, str) | ||
|
||
def test_name_text_data(self): | ||
path = "http://dummy.server.com/data/foo.txt" | ||
reader = UrlReader(path) | ||
reader.content = "text" | ||
text_data = reader.make_text_data() | ||
self.assertIsInstance(text_data, TextData) | ||
self.assertEqual(text_data.name, "foo") | ||
self.assertEqual(text_data.path, path) | ||
self.assertEqual(text_data.ext, [".txt"]) | ||
self.assertEqual(text_data.category, "data") | ||
self.assertEqual(text_data.content, "text") | ||
|
||
|
||
class TestImportDocuments(unittest.TestCase): | ||
def test_scan_url(self): | ||
path = "http://file.biolab.si/text-semantics/data/semeval/" | ||
importer = ImportDocuments(path, True) | ||
paths = importer.scan_url(path) | ||
self.assertGreater(len(paths), 0) | ||
|
||
def test_scan_url_txt(self): | ||
path = "http://file.biolab.si/text-semantics/data/semeval/" | ||
importer = ImportDocuments(path, True) | ||
paths = importer.scan_url(path, include_patterns=["*.txt"]) | ||
self.assertGreater(len(paths), 0) | ||
|
||
def test_scan_url_csv(self): | ||
path = "http://file.biolab.si/text-semantics/data/" | ||
importer = ImportDocuments(path, True) | ||
paths = importer.scan_url(path, include_patterns=["*.csv"]) | ||
self.assertGreater(len(paths), 0) | ||
|
||
def test_read_meta_data_url(self): | ||
path = "http://file.biolab.si/text-semantics/data/semeval/" | ||
importer = ImportDocuments(path, True) | ||
data1, err = importer._read_meta_data() | ||
self.assertIsInstance(data1, pd.DataFrame) | ||
self.assertEqual(len(err), 0) | ||
|
||
# @patch("orangecontrib.text.import_documents.ImportDocuments." | ||
# "META_DATA_FILE_KEY", "File") | ||
def test_merge_metadata_url(self): | ||
path = "http://file.biolab.si/text-semantics/data/semeval/" | ||
importer = ImportDocuments(path, True) | ||
text_data, _ = importer._read_text_data() | ||
meta_data, _ = importer._read_meta_data() | ||
|
||
importer._text_data = text_data[:4] # 'C-1', 'C-14', 'C-17', 'C-18' | ||
importer._meta_data = meta_data[:50] | ||
corpus = importer._create_corpus() | ||
corpus = importer._add_metadata(corpus) | ||
self.assertGreater(len(corpus), 0) | ||
columns = ["name", "path", "content", "Content", | ||
"Text file", "Keywords"] | ||
self.assertEqual([v.name for v in corpus.domain.metas], columns) | ||
|
||
importer._text_data = text_data[:4] # 'C-1', 'C-14', 'C-17', 'C-18' | ||
importer._meta_data = None | ||
corpus = importer._create_corpus() | ||
corpus = importer._add_metadata(corpus) | ||
self.assertGreater(len(corpus), 0) | ||
columns = ["name", "path", "content"] | ||
self.assertEqual([v.name for v in corpus.domain.metas], columns) | ||
|
||
def test_run_url(self): | ||
path = "http://file.biolab.si/text-semantics/data" \ | ||
"/predlogi-vladi-sample/" | ||
importer = ImportDocuments(path, True) | ||
corpus1, _ = importer.run() | ||
self.assertGreater(len(corpus1), 0) | ||
|
||
mask = np.ones_like(corpus1.metas, dtype=bool) | ||
mask[:, 1] = False | ||
|
||
path = "http://file.biolab.si/text-semantics/data" \ | ||
"/predlogi-vladi-sample////" | ||
importer = ImportDocuments(path, True) | ||
corpus2, _ = importer.run() | ||
self.assertGreater(len(corpus1), 0) | ||
self.assertEqual(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()) | ||
|
||
def test_run_url_special_characters(self): | ||
path = "http://file.biolab.si/text-semantics/data/" \ | ||
"elektrotehniski-vestnik-clanki/" | ||
importer = ImportDocuments(path, True) | ||
corpus, errors = importer.run() | ||
self.assertGreater(len(corpus), 0) | ||
|
||
|
||
if __name__ == "__main__": | ||
unittest.main() |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Should .yaml also be specified here?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
No, it should not. These formats are 'documents' formats. Each file of these format represents a document. Metadata is considered differently, since it appends data to each document. Its formats are hardcoded in
_read_meta_data()
.