1
/*
2
 * Copyright (C) 2010 Igalia, S.L.
3
 * 
4
 * Author: Joaquim Rocha <jrocha@igalia.com>
5
 *
6
 * This library is free software; you can redistribute it and/or
7
 * modify it under the terms of the GNU Lesser General Public
8
 * License as published by the Free Software Foundation; either
9
 * version 2.1 of the License, or (at your option) any later version.
10
 *
11
 * This library is distributed in the hope that it will be useful,
12
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
14
 * Lesser General Public License for more details.
15
 *
16
 * You should have received a copy of the GNU Lesser General Public
17
 * License along with this library; if not, write to the Free Software
18
 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
19
 */
20
21
#include <glib.h>
22
#include <string>
23
#include <iostream>
24
#include "presage.h"
25
#ifdef __cplusplus
26
extern "C" 
27
{
28
#include "text-predictor.h"
29
}
30
#endif
31
32
static gchar *string_to_gchar (std::string str);
33
GList *vector_string_to_glist (std::vector<std::string> string_vector);
34
35
36
class PredictCallback : public PresageCallback
37
{
38
 public:
39
  PredictCallback(gchar* new_contents, gint cursor_pos)
40
  { 
41
    contents = std::string(new_contents);
42
    cursor_position = cursor_pos;
43
  }
44
45
  void set_contents (gchar *new_contents, gint cursor_pos)
46
  {
47
    cursor_position = cursor_pos;
48
    contents = std::string(new_contents);
49
  }
50
  std::string get_past_stream () const
51
  {
52
    gint length = 0;
53
    if (cursor_position > 0)
54
    {
55
      length = cursor_position;
56
    }
57
    return contents.substr(0, length);
58
  }
59
  std::string get_future_stream () const
60
  {
61
    gint length = contents.length();
62
    gint position = 0;
63
    if (cursor_position > 0)
64
    {
65
      position = cursor_position;
66
    }
67
    return contents.substr(position, length);
68
  }
69
70
 private:
71
  std::string contents;
72
  gint cursor_position;
73
};
74
75
struct _TextPredictor
76
{
77
  PredictCallback *callback;
78
  Presage *presage;
79
};
80
81
void
82
text_predictor_free (TextPredictor *text_predictor)
83
{
84
  delete text_predictor->presage;
85
  delete text_predictor->callback;
86
  g_free (text_predictor);
87
}
88
89
gchar *
90
text_predictor_get_single_candidate_for_surrounding (TextPredictor *tp,
91
						     gchar *surrounding,
92
						     guint cursor_pos)
93
{
94
  tp->callback->set_contents (surrounding, cursor_pos);
95
  std::vector<std::string> predictions = tp->presage->predict();
96
97
  if (predictions.size() > 0)
98
  {
99
    return string_to_gchar (predictions[0]);
100
  }
101
  return NULL;
102
}
103
104
GList *
105
text_predictor_get_candidates_for_surrounding (TextPredictor *tp,
106
					       gchar *surrounding,
107
					       guint cursor_pos)
108
{
109
  tp->callback->set_contents (surrounding, cursor_pos);
110
  std::vector<std::string> predictions = tp->presage->predict();
111
112
  return vector_string_to_glist (predictions);
113
}
114
115
TextPredictor *
116
text_predictor_new (void)
117
{
118
  TextPredictor *tp = g_new0 (TextPredictor, 1);
119
  tp->callback = new PredictCallback((gchar *) "", -1);
120
  tp->presage = new Presage(tp->callback);
121
  return tp;
122
}
123
124
static gchar *
125
string_to_gchar (std::string str)
126
{
127
  const char *c_str = str.c_str();
128
  return g_strdup (c_str);
129
}
130
131
GList *
132
vector_string_to_glist (std::vector<std::string> string_vector)
133
{
134
  GList *list = NULL;
135
  size_t i;
136
137
  for (size_t i = 0; i < string_vector.size(); i++)
138
  {
139
    if (string_vector[i].length())
140
    {
141
      list = g_list_append (list, g_strdup ((gchar *) string_vector[i].c_str()));
142
    }
143
  }
144
145
  return list;
146
}