gpt4 book ai didi

最后解析带有可选数据的文本

转载 作者:行者123 更新时间:2023-12-04 16:01:55 25 4
gpt4 key购买 nike

请注意,在发布此问题后,我自己设法得出了一个解决方案。请参阅此问题的结尾以获取我的最终答案。

我目前正在为 org-mode 开发一个小解析器。文档,并且在这些文档中,标题可以有一个标题,并且可以选择包含标题的标签列表:

* Heading          :foo:bar:baz:

但是,我很难为此编写解析器。以下是我现在正在使用的内容:
import Control.Applicative
import Text.ParserCombinators.Parsec

data Node = Node String [String]
deriving (Show)

myTest = parse node "" "Some text here :tags:here:"

node = Node <$> (many1 anyChar) <*> tags

tags = (char ':') >> (sepEndBy1 (many1 alphaNum) (char ':'))
<?> "Tag list"

虽然我的简单 tags解析器工作,它在 node 的上下文中不起作用因为所有字符都用完了解析标题的标题( many1 anyChar )。此外,我无法更改此解析器以使用 noneOf ":"因为 :在标题中有效。事实上,只有在 taglist 中,在行尾,它才是特别的。

有什么想法可以解析这些可选数据吗?

顺便说一句,这是我第一个真正的 Haskell 项目,所以如果 Parsec 甚至不是适合这项工作的工具 - 请随时指出这一点并提出其他选择!

好的,我现在有了一个完整的解决方案,但它需要重构。以下作品:
import Control.Applicative hiding (many, optional, (<|>))
import Control.Monad
import Data.Char (isSpace)
import Text.ParserCombinators.Parsec

data Node = Node { level :: Int, keyword :: Maybe String, heading :: String, tags :: Maybe [String] }
deriving (Show)

parseNode = Node <$> level <*> (optionMaybe keyword) <*> name <*> (optionMaybe tags)
where level = length <$> many1 (char '*') <* space
keyword = (try (many1 upper <* space))
name = noneOf "\n" `manyTill` (eof <|> (lookAhead (try (tags *> eof))))
tags = char ':' *> many1 alphaNum `sepEndBy1` char ':'

myTest = parse parseNode "org-mode" "** Some : text here :tags: JUST KIDDING :tags:here:"
myTest2 = parse parseNode "org-mode" "* TODO Just a node"

最佳答案

import Control.Applicative hiding (many, optional, (<|>))
import Control.Monad
import Text.ParserCombinators.Parsec

instance Applicative (GenParser s a) where
pure = return
(<*>) = ap

data Node = Node { name :: String, tags :: Maybe [String] }
deriving (Show)

parseNode = Node <$> name <*> tags
where tags = optionMaybe $ optional (string " :") *> many (noneOf ":\n") `sepEndBy` (char ':')
name = noneOf "\n" `manyTill` try (string " :" <|> string "\n")

myTest = parse parseNode "" "Some:text here :tags:here:"
myTest2 = parse parseNode "" "Sometext here :tags:here:"

结果:
*Main> myTest
Right (Node {name = "Some:text here", tags = Just ["tags","here",""]})
*Main> myTest2
Right (Node {name = "Sometext here", tags = Just ["tags","here",""]})

关于最后解析带有可选数据的文本,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/4996514/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com