Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

add TranslationReg vars_get exhausted #218

Open
wants to merge 4 commits into
base: master
Choose a base branch
from

Conversation

shewer
Copy link
Contributor

@shewer shewer commented Dec 29, 2022

tran = Translation(
    function() 
         for i=1,5 do
              yield( Candidate('test',0,2,'test' .. i, i) )
         end 
      end)

for cand in tran:iter() do  
    print(tran.exhausted, cand.text ) 
end 

--[[ res
false   test1
false   test2
false   test3
false   test4
true    test5
--]]

'''lua
tran = Translation(
    function() 
         for i=1,5 do
              yield( Candidate('test',0,2,'test' .. i, i) )
         end 
      end)

for cand in tran:iter() do  print(tran.exhausted, cand.text ) end
--[[ res
false   test1
false   test2
false   test3
false   test4
true    test5
--]]
```lu
tran=Translation(function() for i=1,5 do yield(Candidate("t",0,2,"test"..i,i)) end end)
repeat
   local cand = tran:next()
   if cand then print(tran.exhausted,cand.text) end
until tran.exhausted
--[[
false   test1
false   test2
false   test3
false   test4
true    test5
--]]

```
@hchunhui
Copy link
Owner

hchunhui commented Jan 8, 2023

小写的 next() 是为了实现 iter() 而做的内部函数,不应该再暴露来。

另外原意是 lua 中只提供 iter() 就够用了。有点好奇 exhausted() Peek() Next() 这三者在什么情形下需要使用。

@shewer
Copy link
Contributor Author

shewer commented Jan 8, 2023

小写的 next() 是为了实现 iter() 而做的内部函数,不应该再暴露来。

另外原意是 lua 中只提供 iter() 就够用了。有点好奇 exhausted() Peek() Next() 这三者在什么情形下需要使用。

在多個 translation 排序時可以用Peek 檢查而不取出來

@shewer
Copy link
Contributor Author

shewer commented Jan 8, 2023

使用 Translation 實現

-- 從n個transltion 中取出當前cand.quility 最大值 的tran
local function pickup_tran(...)
    local first_tran
    local trans = {...}
    for i,tran in next, trans do 
      if not tran.exhausted then
          if not first_tran then
             first_tran = tran
         else
            first_tran =  first_tran:Peek().quality < tran:Peek().quality and tran or first_tran
          end         
      end
  end
  return first_tran
end
-- LuaTranslation func( tran_1,tran_2....tran_n)
local function tran_sort(...)
   repeat
      local tran= pickup_tran(...)
      if tran then
         yield( tran:Peek())
         tran:Next()
     end
   until (tran) 
end


   env.tran1
   env.tran2
   tn1 = env.tran1:query(inp,seg)
   tn2 = env.tran2:query(inp,seg)
   tn3 = env.tran1:query(inp .. 'a', seg)
   for cand in Translation(tran_sort, tn1,tn2,tn3):iter() do
       yield(cand)
   end

remove  next()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants