summaryrefslogtreecommitdiffstats
path: root/src
diff options
context:
space:
mode:
authorPeng Wu <alexepico@gmail.com>2017-01-22 15:32:02 +0800
committerPeng Wu <alexepico@gmail.com>2017-01-22 15:32:02 +0800
commitec83c02c3779a82489829d8284506a88bc73534b (patch)
tree975cc419c9d8d70252ec94f596a51072b6b3e4bf /src
parent70d3d40b972b8b582782d72c05ce2b2d3fcac82c (diff)
downloadlibpinyin-ec83c02c3779a82489829d8284506a88bc73534b.tar.gz
libpinyin-ec83c02c3779a82489829d8284506a88bc73534b.tar.xz
libpinyin-ec83c02c3779a82489829d8284506a88bc73534b.zip
write diff_result method
Diffstat (limited to 'src')
-rw-r--r--src/lookup/phonetic_lookup.cpp39
-rw-r--r--src/lookup/phonetic_lookup.h2
2 files changed, 41 insertions, 0 deletions
diff --git a/src/lookup/phonetic_lookup.cpp b/src/lookup/phonetic_lookup.cpp
index aa1fc60..5c3a6ce 100644
--- a/src/lookup/phonetic_lookup.cpp
+++ b/src/lookup/phonetic_lookup.cpp
@@ -172,4 +172,43 @@ bool ForwardPhoneticConstraints::validate_constraint(PhoneticKeyMatrix * matrix)
}
+bool ForwardPhoneticConstraints::diff_result(MatchResults best,
+ MatchResults other){
+ bool changed = false;
+
+ assert(best->len == other->len);
+
+ for (size_t pos = 0; pos < other->len; ++pos) {
+ phrase_token_t other_token = g_array_index(other, phrase_token_t, pos);
+
+ if (null_token == other_token)
+ continue;
+
+ phrase_token_t best_token = g_array_index(best, phrase_token_t, pos);
+
+ /* the same token */
+ if (best_token == other_token)
+ continue;
+
+ changed = true;
+
+ /* skip the tail node, as not searched in nbest algorithm. */
+ size_t next_pos = other->len - 1;
+ for (size_t i = pos + 1; i < other->len; ++i) {
+ phrase_token_t token = g_array_index(other, phrase_token_t, i);
+
+ if (null_token != token) {
+ next_pos = i;
+ break;
+ }
+ }
+
+ assert(add_constraint(pos, next_pos, other_token));
+ }
+
+ /* best and other results should be different. */
+ assert(changed);
+ return changed;
+}
+
};
diff --git a/src/lookup/phonetic_lookup.h b/src/lookup/phonetic_lookup.h
index f6b0514..3506068 100644
--- a/src/lookup/phonetic_lookup.h
+++ b/src/lookup/phonetic_lookup.h
@@ -431,6 +431,8 @@ public:
return true;
}
+
+ bool diff_result(MatchResults best, MatchResults other);
};