packages feed

conala-dataset-0.1.0.0: src/CoNaLa.hs

{-# language OverloadedStrings #-}
{-# language DeriveGeneric #-}
{-# language GeneralizedNewtypeDeriving #-}
-- | Haskell bindings for the CoNaLa dataset [1], Code/Natural Language Challenge
--
-- This challenge was designed to test systems for generating program snippets from natural language. For example, if the input is sort list x in reverse order, then the system would be required to output x.sort(reverse=True) in Python.
--
-- References
--
-- 1. https://conala-corpus.github.io/
module CoNaLa (sourceDataset
               -- * Types
              , Item(..)
              , QId(..)) where

import GHC.Generics (Generic)

import Data.Aeson (FromJSON(..), ToJSON(..), withObject, (.:))
import Conduit (ConduitT, runConduitRes, sourceFile, (.|), printC, MonadResource)
import Data.Conduit.Aeson (conduitArrayEither, ParserError)
import Data.Text (Text)

t0 :: IO ()
t0 = do
  let
    fpath = "test/cdata.txt"
  runConduitRes $ sourceDataset fpath .| printC

-- | Stream the dataset from file
--
-- As of May 2022 the dataset can be downloaded from http://www.phontron.com/download/conala-corpus-v1.1.zip
sourceDataset
  :: (MonadResource m) =>
     FilePath -- ^ path of dataset file
  -> ConduitT a (Either ParserError Item) m ()
sourceDataset fp = sourceFile fp .|
                   conduitArrayEither

-- | Dataset item
data Item = Item {
  intent :: Text -- ^ Natural Language intent (i.e., the title of a Stack Overflow question)
  , rewritten_intent :: Text -- ^ Crowdsourced revised intents that try to better reflect the full meaning of the code, typically done by incorporating variable names and function arguments that appeared in the code into the intent. This is the input to be used by systems in the CoNaLa challenge.
  , snippet :: Text -- ^ Python code snippet that implements the intent. This is the output of systems in the challenge.
  , question_id :: QId -- ^ Id of the Stack Overflow question
                 } deriving (Eq, Show, Generic)
instance FromJSON Item
instance ToJSON Item

-- | Stack Overflow Question ID
newtype QId = QId Int deriving (Eq, Show, Generic, FromJSON, ToJSON)



{-
    "intent": "How to convert a list of multiple integers into a single integer?",
    "rewritten_intent": "Concatenate elements of a list 'x' of multiple integers to a single integer",
    "snippet": "sum(d * 10 ** i for i, d in enumerate(x[::-1]))",
    "question_id": 41067960
  },
-}