for git v1.5.2 (and below): chdir to the directory of the target file before executin...
[translate_toolkit.git] / search / test_match.py
bloba9f94592573a81bacae4b32ee7872ac6f267e649
1 from translate.search import match
2 from translate.storage import csvl10n
4 class TestMatch:
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"""
12 if targets is None:
13 targets = sources
14 else:
15 assert len(sources) == len(targets)
16 csvfile = csvl10n.csvfile()
17 for source, target in zip(sources, targets):
18 unit = csvfile.addsourceunit(source)
19 unit.target = target
20 return csvfile
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"))
27 candidates.sort()
28 assert candidates == ["hand", "pond"]
29 message = "Ek skop die bal"
30 csvfile = self.buildcsv(
31 ["Hy skop die bal",
32 message,
33 "Jannie skop die bal",
34 "Ek skop die balle",
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
41 candidates.sort()
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"))
50 candidates.sort()
51 assert candidates == ["hand", "pond"]
52 message = "Ek skop die bal"
53 csvfile1 = self.buildcsv(
54 ["Hy skop die bal",
55 message,
56 "Jannie skop die bal"])
57 csvfile2 = self.buildcsv(
58 ["Ek skop die balle",
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
65 candidates.sort()
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"))
85 candidates.sort()
86 assert candidates == ["computer", "file"]