public List <Sentence> Sentences(string text) { lock (lockObj) { requireValidHandle(); List <Sentence> result = new List <Sentence>(); if (!isValidInput(text)) { result.Add(new Sentence(text, SentenceStartType.NONE)); return(result); } byte[] textBytes = ByteArray.s2n(text); int textLen = textBytes.Length - 1; IntPtr sentenceLenByRef = new IntPtr(); while (textLen > 0) { int sentenceTypeInt = Libvoikko.voikkoNextSentenceStartCstr(handle, textBytes, new IntPtr(textLen), ref sentenceLenByRef); int sentenceLen = sentenceLenByRef.ToInt32(); SentenceStartType sentenceType = (SentenceStartType)Enum.ToObject(typeof(SentenceStartType), sentenceTypeInt); string tokenText = text.Substring(0, sentenceLen); result.Add(new Sentence(tokenText, sentenceType)); text = text.Substring(sentenceLen); textBytes = ByteArray.s2n(text); textLen = textBytes.Length - 1; } return(result); } }
public List <string> Suggest(string word) { lock (lockObj) { requireValidHandle(); List <string> suggestions = new List <string>(); if (!isValidInput(word)) { return(suggestions); } IntPtr voikkoSuggestCstr = Libvoikko.voikkoSuggestCstr(handle, ByteArray.s2n(word)); if (voikkoSuggestCstr == IntPtr.Zero) { return(suggestions); } unsafe { for (byte **cStr = (byte **)voikkoSuggestCstr; *cStr != (byte *)0; cStr++) { suggestions.Add(ByteArray.n2s(new IntPtr(*cStr))); } } Libvoikko.voikkoFreeCstrArray(voikkoSuggestCstr); return(suggestions); } }
public void Dispose() { if (handle != IntPtr.Zero) { Libvoikko.voikkoTerminate(handle); handle = IntPtr.Zero; } }
public Voikko(String language, String path) { IntPtr error = new IntPtr(); handle = Libvoikko.voikkoInit(ref error, ByteArray.s2n(language), ByteArray.s2ansi(path)); if (handle == IntPtr.Zero && error != IntPtr.Zero) { throw new VoikkoException(ByteArray.n2s(error)); } }
private void setIntegerOption(int option, int val) { lock (lockObj) { requireValidHandle(); int result = Libvoikko.voikkoSetIntegerOption(handle, option, val); if (result == 0) { throw new VoikkoException("Could not set integer option " + option + " to value " + val + "."); } } }
private void setBoolOption(int option, bool val) { lock (lockObj) { requireValidHandle(); int result = Libvoikko.voikkoSetBooleanOption(handle, option, boolToInt(val)); if (result == 0) { throw new VoikkoException("Could not set boolean option " + option + " to value " + val + "."); } } }
public bool Spell(string word) { lock (lockObj) { requireValidHandle(); if (!isValidInput(word)) { return(false); } int spellResult = Libvoikko.voikkoSpellCstr(handle, ByteArray.s2n(word)); return(spellResult == Libvoikko.VOIKKO_SPELL_OK); } }
public string GetHyphenationPattern(string word) { lock (lockObj) { requireValidHandle(); if (!isValidInput(word)) { // return string of spaces return(new string(' ', word.Length)); } IntPtr cPattern = Libvoikko.voikkoHyphenateCstr(handle, ByteArray.s2n(word)); string pattern = ByteArray.n2s(cPattern); Libvoikko.voikkoFreeCstr(cPattern); return(pattern); } }
private void appendErrorsFromParagraph(List <GrammarError> errorList, string paragraph, int offset, string language) { int paragraphLen = ByteArray.s2n(paragraph).Length - 1; int skipErrors = 0; while (true) { IntPtr cError = Libvoikko.voikkoNextGrammarErrorCstr(handle, ByteArray.s2n(paragraph), new IntPtr(paragraphLen), IntPtr.Zero, skipErrors); if (cError == IntPtr.Zero) { return; } errorList.Add(getGrammarError(cError, offset, language)); Libvoikko.voikkoFreeGrammarError(cError); skipErrors++; } }
public static List <VoikkoDictionary> listDicts(string path) { List <VoikkoDictionary> dicts = new List <VoikkoDictionary>(); IntPtr cDicts = Libvoikko.voikko_list_dicts(ByteArray.s2ansi(path)); unsafe { for (void **cDict = (void **)cDicts; *cDict != (void *)0; cDict++) { dicts.Add(new VoikkoDictionary(ByteArray.n2s(Libvoikko.voikko_dict_language(new IntPtr(*cDict))), ByteArray.n2s(Libvoikko.voikko_dict_variant(new IntPtr(*cDict))), ByteArray.n2s(Libvoikko.voikko_dict_description(new IntPtr(*cDict))))); } } Libvoikko.voikko_free_dicts(cDicts); return(dicts); }
private List <Token> tokensNonNull(String text) { lock (lockObj) { List <Token> result = new List <Token>(); byte[] textBytes = ByteArray.s2n(text); int textLen = textBytes.Length - 1; IntPtr tokenLenByRef = new IntPtr(); while (textLen > 0) { int tokenTypeInt = Libvoikko.voikkoNextTokenCstr(handle, textBytes, new IntPtr(textLen), ref tokenLenByRef); int tokenLen = tokenLenByRef.ToInt32(); TokenType tokenType = (TokenType)Enum.ToObject(typeof(TokenType), tokenTypeInt); String tokenText = text.Substring(0, tokenLen); result.Add(new Token(tokenType, tokenText)); text = text.Substring(tokenLen); textBytes = ByteArray.s2n(text); textLen = textBytes.Length - 1; } return(result); } }
public List <Analysis> Analyze(string word) { lock (lockObj) { requireValidHandle(); List <Analysis> analysisList = new List <Analysis>(); if (!isValidInput(word)) { return(analysisList); } IntPtr cAnalysisList = Libvoikko.voikkoAnalyzeWordCstr(handle, ByteArray.s2n(word)); if (cAnalysisList == IntPtr.Zero) { return(analysisList); } unsafe { for (void **cAnalysis = (void **)cAnalysisList; *cAnalysis != (void *)0; cAnalysis++) { IntPtr cKeys = Libvoikko.voikko_mor_analysis_keys(new IntPtr(*cAnalysis)); Analysis analysis = new Analysis(); for (byte **cKey = (byte **)cKeys; *cKey != (byte *)0; cKey++) { string key = ByteArray.n2s(new IntPtr(*cKey)); IntPtr val = Libvoikko.voikko_mor_analysis_value_cstr(new IntPtr(*cAnalysis), ByteArray.s2n(key)); analysis[key] = ByteArray.n2s(val); Libvoikko.voikko_free_mor_analysis_value_cstr(val); } analysisList.Add(analysis); } } Libvoikko.voikko_free_mor_analysis(cAnalysisList); return(analysisList); } }
private GrammarError getGrammarError(IntPtr cError, int offset, string language) { int errorCode = Libvoikko.voikkoGetGrammarErrorCode(cError); IntPtr startPos = Libvoikko.voikkoGetGrammarErrorStartPos(cError); IntPtr errorLength = Libvoikko.voikkoGetGrammarErrorLength(cError); IntPtr cSuggestions = Libvoikko.voikkoGetGrammarErrorSuggestions(cError); List <string> suggestions = new List <string>(); if (cSuggestions != IntPtr.Zero) { unsafe { for (byte **cStr = (byte **)cSuggestions; *cStr != (byte *)0; cStr++) { suggestions.Add(ByteArray.n2s(new IntPtr(*cStr))); } } } IntPtr cShortDescription = Libvoikko.voikkoGetGrammarErrorShortDescription(cError, ByteArray.s2n(language)); string shortDescription = ByteArray.n2s(cShortDescription); Libvoikko.voikkoFreeErrorMessageCstr(cShortDescription); return(new GrammarError(errorCode, offset + (int)startPos, (int)errorLength, suggestions, shortDescription)); }