{"id":19153221,"url":"https://github.com/ayoubzulfiqar/cambridge-dictionary-api","last_synced_at":"2026-06-20T11:32:21.045Z","repository":{"id":206768009,"uuid":"686008891","full_name":"ayoubzulfiqar/cambridge-dictionary-api","owner":"ayoubzulfiqar","description":"This is Cambridge Dictionary API","archived":false,"fork":false,"pushed_at":"2025-03-14T05:10:51.000Z","size":18,"stargazers_count":1,"open_issues_count":0,"forks_count":0,"subscribers_count":1,"default_branch":"main","last_synced_at":"2025-03-14T06:23:10.894Z","etag":null,"topics":["api","api-rest","bot","cambridge-dictionary","dictionary","dictionary-api","go","golang","rest-api"],"latest_commit_sha":null,"homepage":"","language":"Go","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":null,"status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/ayoubzulfiqar.png","metadata":{"files":{"readme":"README.md","changelog":null,"contributing":null,"funding":null,"license":null,"code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null,"governance":null,"roadmap":null,"authors":null,"dei":null}},"created_at":"2023-09-01T14:15:46.000Z","updated_at":"2025-03-14T05:10:55.000Z","dependencies_parsed_at":null,"dependency_job_id":"9911b6a6-586a-4021-848c-a3b5f0a9ab93","html_url":"https://github.com/ayoubzulfiqar/cambridge-dictionary-api","commit_stats":null,"previous_names":["ayoubzulfiqar/cambridge-dictionary-api"],"tags_count":0,"template":false,"template_full_name":null,"purl":"pkg:github/ayoubzulfiqar/cambridge-dictionary-api","repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/ayoubzulfiqar%2Fcambridge-dictionary-api","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/ayoubzulfiqar%2Fcambridge-dictionary-api/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/ayoubzulfiqar%2Fcambridge-dictionary-api/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/ayoubzulfiqar%2Fcambridge-dictionary-api/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/ayoubzulfiqar","download_url":"https://codeload.github.com/ayoubzulfiqar/cambridge-dictionary-api/tar.gz/refs/heads/main","sbom_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/ayoubzulfiqar%2Fcambridge-dictionary-api/sbom","scorecard":null,"host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":286080680,"owners_count":34568741,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2026-05-26T15:22:16.424Z","status":"online","status_checked_at":"2026-06-20T02:00:06.407Z","response_time":98,"last_error":null,"robots_txt_status":"success","robots_txt_updated_at":"2025-07-24T06:49:26.215Z","robots_txt_url":"https://github.com/robots.txt","online":true,"can_crawl_api":true,"host_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub","repositories_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories","repository_names_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repository_names","owners_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners"}},"keywords":["api","api-rest","bot","cambridge-dictionary","dictionary","dictionary-api","go","golang","rest-api"],"created_at":"2024-11-09T08:22:19.875Z","updated_at":"2026-06-20T11:32:21.025Z","avatar_url":"https://github.com/ayoubzulfiqar.png","language":"Go","funding_links":[],"categories":[],"sub_categories":[],"readme":"# Cambridge Dictionary API\n\n Let's Build a fully functional  Cambridge Dictionary API by crawling it's data based on word search\n We will use  colly to crawl the result data and  Regex to format our result and encode and Encode the data using JSON Marshalling.\n\nCreate a folder a dictionary-api in your computer and create a `main.go` file and run `go mod init dictionary-api` and then `go mod tidy` when ever you add a new dependencies run `go mod tidy` in terminal\n\n## Step - 1 Installing Dependencies\n\nWe will install Colly a Excellent Web Scraping framework Written in GO\n\n```go\ngo get github.com/gocolly/colly\n```\n\n### Writing Our Scraping logic\n\n1. **Initialization:**\n   - The code starts by initializing two important variables: `c` (Collector) and `parsingResult`.\n   - `c` is an instance of a web scraping tool called Colly, which is used to navigate and extract data from web pages.\n   - `parsingResult` is a structure that will hold the results of the web scraping.\n\n2. **Initialization Function (`init`):**\n   - In the `init` function, the Colly collector `c` is set up.\n   - It is configured to only visit web pages from the domain \"dictionary.cambridge.org\".\n   - `AllowURLRevisit` is set to true, which means the collector can revisit the same URL if needed.\n   - The `settingSearch` function is called, which sets up the rules for data extraction.\n\n3. **Setting Up Rules for Data Extraction (`settingSearch` Function):**\n   - This function defines the rules for extracting specific data from the Cambridge Dictionary web pages.\n   - It uses Colly's `OnHTML` method to specify what to do when certain HTML elements are found on the page.\n   - For example, when an HTML element with the class \"pos-header\" is found, it extracts the pronunciation (KK) and part of speech (POS).\n   - When an HTML element with the class \"def-block\" is found, it extracts the meanings and example sentences.\n\n4. **Search Function (`Search` Function):**\n   - The `Search` function is used to perform a word search on the Cambridge Dictionary website.\n   - It takes a `wordToSearch` parameter, which is the word you want to look up.\n   - It resets `parsingResult` to empty before starting a new search.\n   - It uses Colly to visit the Cambridge Dictionary page for the specified word.\n   - After scraping the page, it returns the `parsingResult`, which now contains the word's pronunciation, part of speech, meanings, and example sentences.\n\n```go\n\nvar (\n c             *colly.Collector\n parsingResult wordMeaning\n)\n\nfunc init() {\n c = colly.NewCollector(\n  colly.AllowedDomains(\"dictionary.cambridge.org\"),\n )\n c.AllowURLRevisit = true\n settingSearch()\n}\n\nfunc settingSearch() {\n c.OnHTML(\".pos-header.dpos-h\", func(e *colly.HTMLElement) {\n  // KK\n  e.ForEach(\".us.dpron-i .pron.dpron\", func(i int, m *colly.HTMLElement) {\n   parsingResult.KK = m.Text\n  })\n  // part of speech\n  e.ForEach(\".posgram.dpos-g.hdib.lmr-5\", func(i int, m *colly.HTMLElement) {\n   parsingResult.POS = m.Text\n  })\n })\n // On every a element which has href attribute call callback\n c.OnHTML(\".def-block.ddef_block\", func(e *colly.HTMLElement) {\n  var newMeaningAndSentence meaningAndSentence\n  // meaning\n  e.ForEach(\".def.ddef_d.db\", func(i int, m *colly.HTMLElement) {\n   newMeaningAndSentence.Meaning = formatCrawlerResult(m.Text)\n  })\n  // sentence\n  e.ForEach(\".def-body.ddef_b .examp.dexamp\", func(i int, m *colly.HTMLElement) {\n   newMeaningAndSentence.Sentence = append(newMeaningAndSentence.Sentence, formatCrawlerResult(m.Text))\n  })\n  parsingResult.ResultList = append(parsingResult.ResultList, newMeaningAndSentence)\n })\n}\n\nfunc Search(wordToSearch string) wordMeaning {\n parsingResult = wordMeaning{}\n parsingResult.WordToSearch = wordToSearch\n err := c.Visit(\"https://dictionary.cambridge.org/dictionary/english/\" + wordToSearch)\n if err != nil {\n  panic(err)\n }\n return parsingResult\n}\n```\n\n## Step - 2 Formatting our Scraping result Using Regex\n\nThis data structures and functions are used to organize and format information about words and their meanings. They help make the data more human-readable and suitable for presentation, such as in a user interface or chatbot response.\n\n1. **Data Structures**:\n   - `meaningAndSentence`: This structure represents a word's meaning and related example sentences. It contains two fields:\n     - `Meaning`: A string that stores the meaning of the word.\n     - `Sentence`: A list of strings that stores example sentences related to the word's meaning.\n\n   - `wordMeaning`: This structure represents information about a word, including its pronunciation, part of speech, and a list of meanings and sentences. It contains the following fields:\n     - `WordToSearch`: A string storing the word being searched.\n     - `KK`: A string storing the word's pronunciation.\n     - `POS`: A string storing the word's part of speech.\n     - `ResultList`: A list of `meaningAndSentence` structures, representing different meanings and sentences associated with the word.\n\n2. **`formatCrawlerResult` Function**:\n   - This function takes a string (`result`) as input and processes it to remove unnecessary spaces and characters.\n   - It uses regular expressions to remove extra spaces, colons, and other unwanted characters from the input string.\n   - The processed string is then returned.\n\n3. **`PreprocessingJSONToString` Function**:\n   - This function takes a `wordMeaning` structure (`preOutput`) as input and prepares a formatted string for display.\n   - It constructs a string by combining various pieces of information, including the word, its part of speech, pronunciation, meanings, and example sentences.\n   - It limits the number of meanings displayed to a maximum of 5 (as specified by `maxMeaningLine`).\n   - The formatted string is returned, which can be used for displaying word information in a more readable format.\n\n```go\npackage main\n\nimport (\n \"fmt\"\n \"regexp\"\n \"strings\"\n)\n\nvar maxMeaningLine int = 5\n\ntype meaningAndSentence struct {\n Meaning  string   `json:\"meaning\"`\n Sentence []string `json:\"sentence\"`\n}\n\ntype wordMeaning struct {\n WordToSearch string               `json:\"word\"`\n KK           string               `json:\"kk\"`\n POS          string               `json:\"pos\"`\n ResultList   []meaningAndSentence `json:\"result\"`\n}\n\nfunc formatCrawlerResult(result string) string {\n space := regexp.MustCompile(`\\s+`)\n removeSpace := space.ReplaceAllString(result, \" \")\n // remove case of [ C ] or [ T ]\n corT := regexp.MustCompile(`\\[\\s+.\\s+\\]|:`)\n removeCorT := corT.ReplaceAllString(removeSpace, \"\")\n // remove leading space\n noSpaceDuplicate := strings.TrimSpace(removeCorT)\n // replace the needed escape character\n // escape := regexp.MustCompile(`\\.|\\'|\\*|\\[|\\]|\\(|\\)|\\~|\\\u003e|\\#|\\+|\\-|\\=|\\||\\{|\\}|\\.|\\!`)\n // removeEscape := escape.ReplaceAllString(noSpaceDuplicate, `\\$0`)\n\n s := noSpaceDuplicate\n return s\n}\n\nfunc PreprocessingJSONToString(preOutput wordMeaning) string {\n output := \"\"\n // title\n output += fmt.Sprintf(`*%s*  (_%s_)`, preOutput.WordToSearch, preOutput.POS) + \"\\n\"\n output += preOutput.KK + \"\\n\"\n\n for i, result := range preOutput.ResultList {\n  if i+1 \u003e maxMeaningLine {\n   break\n  }\n  output += fmt.Sprintf(\"%d\", i+1) + \". *\" + result.Meaning + \"*\\n\"\n  if len(result.Sentence) \u003e 0 {\n   output += `\\* _` + result.Sentence[0] + \"_\\n\"\n  }\n }\n\n return output\n}\n```\n\n### Step - 3 Writing a HTTP Handler with EndPoints\n\nThe `SearchQueryHandler` function handles HTTP requests that include a word to search for in the URL path (e.g., \"/search/word\"). It extracts the word, retrieves its meaning, and sends a JSON response with the meaning information. If there are errors during this process, it may panic and terminate the program.\n\n```go\nfunc SearchQueryHandler() {\n http.HandleFunc(\"/search/\", func(w http.ResponseWriter, r *http.Request) {\n  wordToSearch := strings.TrimPrefix(r.URL.Path, \"/search/\")\n  outputJSON := getMeaning(wordToSearch)\n  w.Header().Set(\"Content-Type\", \"application/json\")\n  w.WriteHeader(http.StatusOK)\n  encodeError := json.NewEncoder(w).Encode(outputJSON)\n  if encodeError != nil {\n   panic(encodeError)\n  }\n })\n}\n```\n\n### Listen and Serve\n\n```go\nfunc main() {\n // Standard JSON REQUEST\n SearchQueryHandler()\n // Start the HTTP server\n // listen and serve on 0.0.0.0:8080 (for windows \"localhost:8080\")\n if err := http.ListenAndServe(\":8080\", nil); err != nil {\n  log.Fatal(err)\n }\n}\n```\n\n### Run\n\n```go\n// it will find all the global functions and run it. It is because it did not step up go env GOPATH\n// So for you - you can try  go run main.go\ngo run .\n// OR\ngo run main.go\n```\n\n## Demo\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fayoubzulfiqar%2Fcambridge-dictionary-api","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fayoubzulfiqar%2Fcambridge-dictionary-api","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fayoubzulfiqar%2Fcambridge-dictionary-api/lists"}