github.com/jakewarren/define@v1.0.1-0.20230216022724-0146b4213218/structs.go (about)

     1  package dictionary
     2  
     3  import (
     4  	"bytes"
     5  	"fmt"
     6  	"strings"
     7  
     8  	"github.com/fatih/color"
     9  )
    10  
    11  type Results struct {
    12  	ID       *string   `json:"id,omitempty"`
    13  	Metadata *Metadata `json:"metadata,omitempty"`
    14  	Results  []Result  `json:"results"`
    15  	Word     *string   `json:"word,omitempty"`
    16  }
    17  
    18  type Metadata struct {
    19  	Operation *string `json:"operation,omitempty"`
    20  	Provider  *string `json:"provider,omitempty"`
    21  	Schema    *string `json:"schema,omitempty"`
    22  }
    23  
    24  type Result struct {
    25  	ID             *string        `json:"id,omitempty"`
    26  	Language       *string        `json:"language,omitempty"`
    27  	LexicalEntries []LexicalEntry `json:"lexicalEntries"`
    28  	Type           *string        `json:"type,omitempty"`
    29  	Word           *string        `json:"word,omitempty"`
    30  }
    31  
    32  type LexicalEntry struct {
    33  	Entries         []Entry          `json:"entries"`
    34  	Language        *string          `json:"language,omitempty"`
    35  	LexicalCategory *LexicalCategory `json:"lexicalCategory,omitempty"`
    36  	Pronunciations  []Pronunciation  `json:"pronunciations"`
    37  	Text            *string          `json:"text,omitempty"`
    38  }
    39  
    40  func (entry *LexicalEntry) RenderLexicalCategory() string {
    41  	return strings.ToUpper(*entry.LexicalCategory.Text)
    42  }
    43  
    44  type Entry struct {
    45  	Etymologies     []string `json:"etymologies"`
    46  	HomographNumber *string  `json:"homographNumber,omitempty"`
    47  	Senses          []Sense  `json:"senses"`
    48  }
    49  
    50  func (e Entry) HasEtymology() bool {
    51  	return len(e.Etymologies) > 0
    52  }
    53  
    54  func (e Entry) RenderEtymology() interface{} {
    55  	var buf bytes.Buffer
    56  	for _, ex := range e.Etymologies {
    57  		fmt.Fprintf(&buf, "%s\n", ex)
    58  	}
    59  	return buf.String()
    60  }
    61  
    62  type Sense struct {
    63  	Definitions      []string        `json:"definitions"`
    64  	Examples         []Example       `json:"examples"`
    65  	ID               *string         `json:"id,omitempty"`
    66  	ShortDefinitions []string        `json:"shortDefinitions"`
    67  	Subsenses        []Subsense      `json:"subsenses"`
    68  	ThesaurusLinks   []ThesaurusLink `json:"thesaurusLinks"`
    69  }
    70  
    71  func (s Sense) RenderExamples() string {
    72  	var buf bytes.Buffer
    73  	for _, ex := range s.Examples {
    74  		fmt.Fprintf(&buf, "%s\n", ex.Render())
    75  	}
    76  	return buf.String()
    77  }
    78  
    79  func (s Sense) HasExamples() bool {
    80  	return len(s.Examples) > 0
    81  }
    82  
    83  func (ex Example) Render() string {
    84  	return *ex.Text
    85  }
    86  
    87  type Example struct {
    88  	Text *string `json:"text,omitempty"`
    89  }
    90  
    91  type Subsense struct {
    92  	Definitions      []string          `json:"definitions"`
    93  	Examples         []Example         `json:"examples"`
    94  	ID               *string           `json:"id,omitempty"`
    95  	Regions          []LexicalCategory `json:"regions"`
    96  	Registers        []LexicalCategory `json:"registers"`
    97  	ShortDefinitions []string          `json:"shortDefinitions"`
    98  	ThesaurusLinks   []ThesaurusLink   `json:"thesaurusLinks"`
    99  }
   100  
   101  func (s Subsense) HasExamples() bool {
   102  	return len(s.Examples) > 0
   103  }
   104  
   105  func (s Subsense) RenderTags() string {
   106  	tags := make([]string, 0)
   107  
   108  	for _, t := range s.Regions {
   109  		tags = append(tags, cleanTag(*t.Text))
   110  	}
   111  
   112  	for _, t := range s.Registers {
   113  		tags = append(tags, cleanTag(*t.Text))
   114  	}
   115  
   116  	if len(tags) == 0 {
   117  		return ""
   118  	}
   119  	return color.GreenString(fmt.Sprintf("%s ", strings.Join(tags, ", ")))
   120  }
   121  
   122  func (s Subsense) RenderExamples() string {
   123  	var buf bytes.Buffer
   124  	for _, ex := range s.Examples {
   125  		fmt.Fprintf(&buf, "%s\n", ex.Render())
   126  	}
   127  	return buf.String()
   128  }
   129  
   130  func cleanTag(s string) (output string) {
   131  	output = s
   132  	output = strings.Replace(output, "_", " ", -1)
   133  	return
   134  }
   135  
   136  type LexicalCategory struct {
   137  	ID   *string `json:"id,omitempty"`
   138  	Text *string `json:"text,omitempty"`
   139  }
   140  
   141  type ThesaurusLink struct {
   142  	EntryID *string `json:"entry_id,omitempty"`
   143  	SenseID *string `json:"sense_id,omitempty"`
   144  }
   145  
   146  type Pronunciation struct {
   147  	AudioFile        *string  `json:"audioFile,omitempty"`
   148  	Dialects         []string `json:"dialects"`
   149  	PhoneticNotation *string  `json:"phoneticNotation,omitempty"`
   150  	PhoneticSpelling *string  `json:"phoneticSpelling,omitempty"`
   151  }