File tree Expand file tree Collapse file tree 2 files changed +18
-11
lines changed
Expand file tree Collapse file tree 2 files changed +18
-11
lines changed Original file line number Diff line number Diff line change @@ -33,22 +33,24 @@ VP-trees are a space-partitioning data structure that significantly accelerates
3333``` haskell
3434{-# LANGUAGE OverloadedStrings #-}
3535
36- import qualified Data.ByteString.Lazy.Char8 as BL
37- import Data.NCDTree
36+ from Data. NCDTree import Document , mkVPTree, knnSearch
3837
39- -- Create documents from bytestrings
40- let docs = [ Document (BL. pack " hello world" )
41- , Document (BL. pack " hello universe" )
42- , Document (BL. pack " goodbye world" )
43- , Document (BL. pack " hello there" )
44- ]
38+ -- Create documents using string literals (thanks to IsString instance)
39+ docs :: [Document ]
40+ docs = [ " hello world"
41+ , " hello universe"
42+ , " goodbye world"
43+ , " hello there"
44+ ]
4545
4646-- Build the VP-tree index with a leaf threshold of 4
47- let tree = mkVPTree 4 docs
47+ tree = mkVPTree 4 docs
4848
4949-- Search for the 2 nearest neighbors of "hello"
50- let query = Document (BL. pack " hello" )
51- let results = knnSearch 2 query tree
50+ query :: Document
51+ query = " hello"
52+
53+ results = knnSearch 2 query tree
5254
5355-- results is a max-heap with the closest documents
5456```
Original file line number Diff line number Diff line change @@ -10,9 +10,11 @@ module Data.NCDTree
1010 ) where
1111
1212import Control.Monad.ST
13+ import Data.String (IsString (.. ))
1314import Data.Ord (Down (.. ), comparing )
1415
1516import qualified Data.ByteString.Lazy as BL
17+ import qualified Data.ByteString.Lazy.Char8 as BLC
1618
1719import qualified Data.Heap as H
1820
@@ -26,6 +28,9 @@ newtype Document = Document
2628 { docText :: BL. ByteString
2729 } deriving (Eq , Show )
2830
31+ instance IsString Document where
32+ fromString = Document . BLC. pack
33+
2934data VPTree = VPNode { pivot :: Document
3035 , threshold :: Double
3136 , inside :: VPTree
You can’t perform that action at this time.
0 commit comments