Skip to content
GitLab
Explore
Sign in
Primary navigation
Search or go to…
Project
T
teiwa
Manage
Activity
Members
Labels
Plan
Issues
0
Issue boards
Milestones
Wiki
Code
Merge requests
0
Repository
Branches
Commits
Tags
Repository graph
Compare revisions
Snippets
Build
Pipelines
Jobs
Pipeline schedules
Artifacts
Deploy
Releases
Model registry
Operate
Environments
Monitor
Incidents
Analyze
Value stream analytics
Contributor analytics
CI/CD analytics
Repository analytics
Model experiments
Help
Help
Support
GitLab documentation
Compare GitLab plans
Community forum
Contribute to GitLab
Provide feedback
Keyboard shortcuts
?
Snippets
Groups
Projects
Show more breadcrumbs
Alice Brenon
teiwa
Commits
4b5d0966
Commit
4b5d0966
authored
4 years ago
by
Alice Brenon
Browse files
Options
Downloads
Patches
Plain Diff
Make Config accessible from within the TEIWAParser monad
parent
92ca859d
No related branches found
No related tags found
No related merge requests found
Changes
1
Hide whitespace changes
Inline
Side-by-side
Showing
1 changed file
lib/Text/TEIWA/Source.hs
+18
-16
18 additions, 16 deletions
lib/Text/TEIWA/Source.hs
with
18 additions
and
16 deletions
lib/Text/TEIWA/Source.hs
+
18
−
16
View file @
4b5d0966
...
@@ -13,6 +13,7 @@ module Text.TEIWA.Source (
...
@@ -13,6 +13,7 @@ module Text.TEIWA.Source (
)
where
)
where
import
Control.Monad.Except
(
MonadError
(
..
))
import
Control.Monad.Except
(
MonadError
(
..
))
import
Control.Monad.Reader
(
MonadReader
(
..
),
ReaderT
(
..
))
import
Control.Monad.IO.Class
(
MonadIO
(
..
))
import
Control.Monad.IO.Class
(
MonadIO
(
..
))
import
Data.Text.Lazy
as
Text
(
Text
,
unpack
)
import
Data.Text.Lazy
as
Text
(
Text
,
unpack
)
import
Data.Text.Lazy.IO
as
Text
(
readFile
)
import
Data.Text.Lazy.IO
as
Text
(
readFile
)
...
@@ -26,10 +27,10 @@ import Text.TEIWA.Source.Common (AnnotationContext(..), Row)
...
@@ -26,10 +27,10 @@ import Text.TEIWA.Source.Common (AnnotationContext(..), Row)
import
qualified
Text.TEIWA.Source.ConLLX
as
ConLLX
(
getContext
,
sentences
)
import
qualified
Text.TEIWA.Source.ConLLX
as
ConLLX
(
getContext
,
sentences
)
import
qualified
Text.TEIWA.Source.CSV
as
CSV
(
body
,
getContext
)
import
qualified
Text.TEIWA.Source.CSV
as
CSV
(
body
,
getContext
)
type
TEIWAParser
=
ParsecT
Text
()
(
Either
Error
)
type
TEIWAParser
=
ParsecT
Text
()
(
ReaderT
Config
(
Either
Error
)
)
type
Format
=
Config
->
TEIWAParser
Annotation
type
Format
=
TEIWAParser
Annotation
annotateToken
::
MonadError
Error
m
=>
annotateToken
::
(
MonadError
Error
m
,
MonadReader
Config
m
)
=>
AnnotationContext
->
Row
->
m
TokenAnnotation
AnnotationContext
->
Row
->
m
TokenAnnotation
annotateToken
(
AnnotationContext
{
columnIndex
,
columnName
,
header
})
(
atLine
,
record
)
=
annotateToken
(
AnnotationContext
{
columnIndex
,
columnName
,
header
})
(
atLine
,
record
)
=
case
splitAt
columnIndex
record
of
case
splitAt
columnIndex
record
of
...
@@ -38,23 +39,24 @@ annotateToken (AnnotationContext {columnIndex, columnName, header}) (atLine, rec
...
@@ -38,23 +39,24 @@ annotateToken (AnnotationContext {columnIndex, columnName, header}) (atLine, rec
return
$
TokenAnnotation
{
form
,
annotated
=
zip
header
(
before
++
after
)}
return
$
TokenAnnotation
{
form
,
annotated
=
zip
header
(
before
++
after
)}
coNLLX
::
Format
coNLLX
::
Format
coNLLX
(
Config
{
formColumn
})
=
do
coNLLX
=
do
context
<-
ConLLX
.
getContext
formColumn
context
<-
ConLLX
.
getContext
=<<
reader
formColumn
SentenceLevel
<$>
(
SentenceLevel
<$>
(
ConLLX
.
sentences
>>=
mapM
(
ConLLX
.
sentences
>>=
mapM
(
fmap
SentenceAnnotation
.
mapM
(
annotateToken
context
)
fmap
SentenceAnnotation
.
mapM
(
annotateToken
context
)
)
)
)
)
ssv
::
Char
->
Format
ssv
separator
=
do
context
<-
CSV
.
getContext
separator
=<<
reader
formColumn
TokenLevel
<$>
(
CSV
.
body
separator
>>=
mapM
(
annotateToken
context
))
csv
::
Format
csv
::
Format
csv
(
Config
{
formColumn
})
=
do
csv
=
ssv
','
context
<-
CSV
.
getContext
','
formColumn
TokenLevel
<$>
(
CSV
.
body
','
>>=
mapM
(
annotateToken
context
))
tsv
::
Format
tsv
::
Format
tsv
(
Config
{
formColumn
})
=
do
tsv
=
ssv
'
\t
'
context
<-
CSV
.
getContext
'
\t
'
formColumn
TokenLevel
<$>
(
CSV
.
body
'
\t
'
>>=
mapM
(
annotateToken
context
))
data
Origin
=
File
FilePath
|
Text
Text
data
Origin
=
File
FilePath
|
Text
Text
...
@@ -64,14 +66,14 @@ data Source = Source {
...
@@ -64,14 +66,14 @@ data Source = Source {
}
}
runTEIWAParser
::
MonadError
Error
m
=>
runTEIWAParser
::
MonadError
Error
m
=>
TEIWAParser
a
->
SourceName
->
Text
->
m
a
Config
->
TEIWAParser
a
->
SourceName
->
Text
->
m
a
runTEIWAParser
p
s
=
flattenErrors
.
runParserT
p
()
s
runTEIWAParser
config
p
s
=
flattenErrors
.
(`
runReaderT
`
config
)
.
runParserT
p
()
s
where
where
flattenErrors
=
either
throwError
(
either
(
throwError
.
ParsingError
)
pure
)
flattenErrors
=
either
throwError
(
either
(
throwError
.
ParsingError
)
pure
)
parse
::
(
MonadIO
m
,
MonadError
Error
m
)
=>
parse
::
(
MonadIO
m
,
MonadError
Error
m
)
=>
Config
->
Source
->
m
Annotation
Config
->
Source
->
m
Annotation
parse
config
(
Source
{
format
,
origin
})
=
parseFrom
(
format
config
)
origin
parse
config
(
Source
{
format
,
origin
})
=
parseFrom
format
origin
where
where
parseFrom
p
(
File
f
)
=
liftIO
(
Text
.
readFile
f
)
>>=
runTEIWAParser
p
f
parseFrom
p
(
File
f
)
=
liftIO
(
Text
.
readFile
f
)
>>=
runTEIWAParser
config
p
f
parseFrom
p
(
Text
t
)
=
runTEIWAParser
p
""
t
parseFrom
p
(
Text
t
)
=
runTEIWAParser
config
p
""
t
This diff is collapsed.
Click to expand it.
Preview
0%
Loading
Try again
or
attach a new file
.
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Save comment
Cancel
Please
register
or
sign in
to comment