You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

95 lines
2.8 KiB

2 years ago
2 years ago
2 years ago
2 years ago
2 years ago
2 years ago
2 years ago
2 years ago
2 years ago
  1. from collections import defaultdict
  2. from pathlib import Path
  3. from typing import Dict, List, Type
  4. from projects.models import (
  5. DOCUMENT_CLASSIFICATION,
  6. IMAGE_CLASSIFICATION,
  7. INTENT_DETECTION_AND_SLOT_FILLING,
  8. SEQ2SEQ,
  9. SEQUENCE_LABELING,
  10. SPEECH2TEXT,
  11. )
  12. EXAMPLE_DIR = Path(__file__).parent.resolve() / "examples"
  13. class Format:
  14. name = ""
  15. @classmethod
  16. def dict(cls):
  17. return {
  18. "name": cls.name,
  19. }
  20. class CSV(Format):
  21. name = "CSV"
  22. class FastText(Format):
  23. name = "fastText"
  24. class JSON(Format):
  25. name = "JSON"
  26. class JSONL(Format):
  27. name = "JSONL"
  28. class Options:
  29. options: Dict[str, List] = defaultdict(list)
  30. @classmethod
  31. def filter_by_task(cls, task_name: str, use_relation: bool = False):
  32. options = cls.options[task_name]
  33. return [
  34. {**file_format.dict(), "example": example}
  35. for file_format, example, use_rel in options
  36. if use_rel == use_relation
  37. ]
  38. @classmethod
  39. def register(cls, task: str, file_format: Type[Format], file: Path, use_relation: bool = False):
  40. example = cls.load_example(file)
  41. cls.options[task].append((file_format, example, use_relation))
  42. @staticmethod
  43. def load_example(file):
  44. with open(file, encoding="utf-8") as f:
  45. return f.read()
  46. # Text Classification
  47. TEXT_CLASSIFICATION_DIR = EXAMPLE_DIR / "text_classification"
  48. Options.register(DOCUMENT_CLASSIFICATION, CSV, TEXT_CLASSIFICATION_DIR / "example.csv")
  49. Options.register(DOCUMENT_CLASSIFICATION, FastText, TEXT_CLASSIFICATION_DIR / "example.txt")
  50. Options.register(DOCUMENT_CLASSIFICATION, JSON, TEXT_CLASSIFICATION_DIR / "example.json")
  51. Options.register(DOCUMENT_CLASSIFICATION, JSONL, TEXT_CLASSIFICATION_DIR / "example.jsonl")
  52. # Sequence Labeling
  53. SEQUENCE_LABELING_DIR = EXAMPLE_DIR / "sequence_labeling"
  54. RELATION_EXTRACTION_DIR = EXAMPLE_DIR / "relation_extraction"
  55. Options.register(SEQUENCE_LABELING, JSONL, SEQUENCE_LABELING_DIR / "example.jsonl")
  56. Options.register(SEQUENCE_LABELING, JSONL, RELATION_EXTRACTION_DIR / "example.jsonl", True)
  57. # Sequence to sequence
  58. SEQ2SEQ_DIR = EXAMPLE_DIR / "sequence_to_sequence"
  59. Options.register(SEQ2SEQ, CSV, SEQ2SEQ_DIR / "example.csv")
  60. Options.register(SEQ2SEQ, JSON, SEQ2SEQ_DIR / "example.json")
  61. Options.register(SEQ2SEQ, JSONL, SEQ2SEQ_DIR / "example.jsonl")
  62. # Intent detection and slot filling
  63. INTENT_DETECTION_DIR = EXAMPLE_DIR / "intent_detection"
  64. Options.register(INTENT_DETECTION_AND_SLOT_FILLING, JSONL, INTENT_DETECTION_DIR / "example.jsonl")
  65. # Image Classification
  66. IMAGE_CLASSIFICATION_DIR = EXAMPLE_DIR / "image_classification"
  67. Options.register(IMAGE_CLASSIFICATION, JSONL, IMAGE_CLASSIFICATION_DIR / "example.jsonl")
  68. # Speech to Text
  69. SPEECH2TEXT_DIR = EXAMPLE_DIR / "speech_to_text"
  70. Options.register(SPEECH2TEXT, JSONL, SPEECH2TEXT_DIR / "example.jsonl")