首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >使用haskell显示词干词干和词干

使用haskell显示词干词干和词干
EN

Stack Overflow用户
提问于 2014-01-09 14:39:35
回答 1查看 175关注 0票数 1

嗨,我是哈斯克尔和功能编程的新手。

我希望在字符串中找到词干词,并在词干删除后显示单词和单词。

代码语言:javascript
复制
eg if the string is : "he is a good fisher man. he is fishing and cached two fish"
output should be : [(fisher,fish), (fishing, fish), (cached, catch)]

我试着做这个

代码语言:javascript
复制
hasEnding endings w = any (`isSuffixOf` w) endings
wordsWithEndings endings ws = filter (hasEnding endings) ws
wordsEndingEdOrIng ws = wordsWithEndings ["ed","ing","er"] . words $ ws


stemming :: String -> String
stemming []        = []
stemming (x:"ing") = [x]
stemming (x:"ed")  = [x] 
stemming (x:"er")  = [x]
stemming (x:xs)    = x : stemming xs

removestemmings :: String -> String
removestemmings = unwords . map stemming . words


findwords = wordsEndingEdOrIng .removestemmings

这个不行..。这个结果是 。

有人能帮我做这个吗。

EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2014-01-09 15:23:19

您的findwords函数正按照您的要求执行。首先,它从每个单词中删除词干,然后过滤掉每个没有词干的单词,这就是所有的词干。

相反,您要做的是删除所有词干,用原始单词列表压缩列表,然后过滤原始单词有词干的列表:

代码语言:javascript
复制
-- Operate on a single word only.
hasStem :: String -> Bool
hasStem w = or $ zipWith isSuffixOf ["ed", "ing", "er"] $ repeat w

-- Let this function work on a list of words instead
removeStemmings :: [String] -> [String]
removeStemmings = map stemming

-- findWords now takes a sentence, splits into words, remove the stemmings,
-- zips with the original word list, and filters that list by which had stems
findWords :: String -> [(String, String)]
findWords sentence = filter (hasStem . fst) . zip ws $ removeStemmings ws
    where ws = words sentence

> findWords "he is a good fisher man. he is fishing and catched two fish"
[("fisher","fish"),("fishing","fish"),("catched","catch")]
票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/21023201

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档