Softwaredesign: Komplexe Anfragen |
1module Query
2where
3
4import Data.Map(Map)
5import qualified Data.Map as M
6import Data.Set(Set)
7import qualified Data.Set as S
8
9import Index
10
11-- ------------------------------------------------------------
12--
13-- the query data types
14
15data Query
16 = QWord Word
17 | QPrefix Word
18 | QBin QOp Query Query
19 deriving (Show)
20
21data QOp
22 = QAnd
23 | QOr
24 | QPhrase
25 | QFollow Int
26 | QContext Int
27 deriving (Show)
28
29-- ------------------------------------------------------------
30--
31-- the query evaluator
32
33selectDocs :: Query -> Index -> DocumentNames
34selectDocs q = documentNames . select q
35
36select :: Query -> Index -> Occurences
37
38select (QWord w) ix
39 = searchIndex w ix
40
41select (QPrefix p) ix
42 = prefixSearchIndex p ix
43
44select (QBin op q1 q2) ix
45 = f r1 r2
46 where
47 f = op2func op
48 r1 = select q1 ix
49 r2 = select q2 ix
50
51-- ------------------------------------------------------------
52--
53-- operator -> result set combinator
54
55op2func QAnd
56 = intersectOcc
57
58op2func QOr
59 = unionOcc
60
61op2func QPhrase
62 = followedByOcc
63
64op2func (QFollow n)
65 = \ r1 r2 -> r1 `S.intersection` (n `nearByOcc` r2)
66
67op2func (QContext n)
68 = \ r1 r2 ->
69 ( ( r1 `S.intersection` (n `nearByOcc` r2) )
70 `S.union`
71 ( r2 `S.intersection` (n `nearByOcc` r1) )
72 )
73
74-- ------------------------------------------------------------
75--
76-- result set combination
77
78unionOcc :: Occurences -> Occurences -> Occurences
79intersectOcc :: Occurences -> Occurences -> Occurences
80followedByOcc :: Occurences -> Occurences -> Occurences
81shiftOcc :: Int -> Occurences -> Occurences
82nearByOcc :: Int -> Occurences -> Occurences
83
84occ1 `unionOcc` occ2
85 = occ1 `S.union` occ2
86
87occ1 `intersectOcc` occ2
88 = S.filter ((`S.member` docs) . fst) occ1
89 `S.union`
90 S.filter ((`S.member` docs) . fst) occ2
91 where
92 docs = documentNames occ1
93 `S.intersection`
94 documentNames occ2
95
96i `shiftOcc` occ
97 = S.fold (\ (n, p) s -> S.insert (n, p - i) s) S.empty occ
98
99occ1 `followedByOcc` occ2
100 = occ1 `S.intersection` (1 `shiftOcc` occ2)
101
102i `nearByOcc` occ
103 = S.unions . map (\ i -> i `shiftOcc` occ) $ [1..i]
|
1module IndexExample2
2where
3
4import qualified Data.Set as S
5
6import IndexExample
7import Query
8
9-- ------------------------------------------------------------
10
11suche :: Query -> IO ()
12suche q
13 = gefundeneZitate (`S.member` res)
14 where
15 res = selectDocs q zitateIx
16
17-- ------------------------------------------------------------
18--
19-- wichtige zusammegesetzte Anfragen
20
21werder = suche ( QWord "Werder" )
22
23werderBremen = suche ( QBin QPhrase
24 ( QWord "Werder" )
25 ( QWord "Bremen" )
26 )
27
28lm = suche ( QBin QPhrase
29 ( QPrefix "Lot" )
30 ( QPrefix "Mat" )
31 )
32bayernUli = suche ( QBin (QContext 3)
33 ( QPrefix "U" )
34 ( QPrefix "Ho" )
35 )
36
37ente = suche ( QBin QAnd
38 ( QWord "Ente" )
39 ( QWord "Willi" )
40 )
41
42wot = suche ( QBin QAnd
43 ( QWord "Werder")
44 ( QBin QOr
45 ( QWord "Otto" )
46 ( QWord "Thomas" )
47 )
48 )
49
50wbbvb = suche ( QBin QAnd
51 ( QWord "Werder" )
52 ( QWord "BVB" )
53 )
54
55bayernHsv = suche ( QBin QAnd
56 ( QWord "Bayern" )
57 ( QBin QOr
58 ( QWord "HSV" )
59 ( QBin QPhrase
60 ( QPrefix "Hamburg" )
61 ( QWord "SV" )
62 )
63 )
64 )
65
66-- ------------------------------------------------------------
67
|
Letzte Änderung: 10.07.2012 | © Prof. Dr. Uwe Schmidt |