1 from translate
.search
import match
2 from translate
.storage
import csvl10n
5 """Test the matching class"""
6 def candidatestrings(self
, units
):
7 """returns only the candidate strings out of the list with (score, string) tuples"""
8 return [unit
.source
for unit
in units
]
10 def buildcsv(self
, sources
, targets
=None):
11 """Build a csvfile store with the given source and target strings"""
15 assert len(sources
) == len(targets
)
16 csvfile
= csvl10n
.csvfile()
17 for source
, target
in zip(sources
, targets
):
18 unit
= csvfile
.addsourceunit(source
)
22 def test_matching(self
):
23 """Test basic matching"""
24 csvfile
= self
.buildcsv(["hand", "asdf", "fdas", "haas", "pond"])
25 matcher
= match
.matcher(csvfile
)
26 candidates
= self
.candidatestrings(matcher
.matches("hond"))
28 assert candidates
== ["hand", "pond"]
29 message
= "Ek skop die bal"
30 csvfile
= self
.buildcsv(
33 "Jannie skop die bal",
35 "Niemand skop die bal nie"])
36 matcher
= match
.matcher(csvfile
)
37 candidates
= self
.candidatestrings(matcher
.matches(message
))
38 assert len(candidates
) == 3
39 #test that the 100% match is indeed first:
40 assert candidates
[0] == message
42 assert candidates
[1:] == ["Ek skop die balle", "Hy skop die bal"]
44 def test_multiple_store(self
):
45 """Test using multiple datastores"""
46 csvfile1
= self
.buildcsv(["hand", "asdf", "fdas"])
47 csvfile2
= self
.buildcsv(["haas", "pond"])
48 matcher
= match
.matcher([csvfile1
, csvfile2
])
49 candidates
= self
.candidatestrings(matcher
.matches("hond"))
51 assert candidates
== ["hand", "pond"]
52 message
= "Ek skop die bal"
53 csvfile1
= self
.buildcsv(
56 "Jannie skop die bal"])
57 csvfile2
= self
.buildcsv(
59 "Niemand skop die bal nie"])
60 matcher
= match
.matcher([csvfile1
, csvfile2
])
61 candidates
= self
.candidatestrings(matcher
.matches(message
))
62 assert len(candidates
) == 3
63 #test that the 100% match is indeed first:
64 assert candidates
[0] == message
66 assert candidates
[1:] == ["Ek skop die balle", "Hy skop die bal"]
68 def test_extendtm(self
):
69 """Test that we can extend the TM after creation."""
70 message
= "Open file..."
71 csvfile1
= self
.buildcsv(["Close application", "Do something"])
72 matcher
= match
.matcher([csvfile1
])
73 candidates
= self
.candidatestrings(matcher
.matches(message
))
74 assert len(candidates
) == 0
75 csvfile2
= self
.buildcsv(["Open file"])
76 matcher
.extendtm(csvfile2
.units
, store
=csvfile2
)
77 candidates
= self
.candidatestrings(matcher
.matches(message
))
78 assert len(candidates
) == 1
79 assert candidates
[0] == "Open file"
81 def test_terminology(self
):
82 csvfile
= self
.buildcsv(["file", "computer", "directory"])
83 matcher
= match
.terminologymatcher(csvfile
)
84 candidates
= self
.candidatestrings(matcher
.matches("Copy the files from your computer"))
86 assert candidates
== ["computer", "file"]