LIMA
Libre Multilingual Analyzer — C++ API
Loading...
Searching...
No Matches
NameIndexResource.cpp
Go to the documentation of this file.
1// Copyright 2002-2013 CEA LIST
2// SPDX-FileCopyrightText: 2022 CEA LIST <gael.de-chalendar@cea.fr>
3//
4// SPDX-License-Identifier: MIT
5
6/***************************************************************************
7 * Copyright (C) 2004-2012 by CEA LIST *
8 * *
9 ***************************************************************************/
10#include "NameIndexResource.h"
16#include <iostream>
17#include <fstream>
18#include <sys/types.h>
19#include <sys/stat.h>
20
21/*
22#include <QReadLocker>
23#include <QWriteLocker>
24#include <QFileInfo>
25*/
27using namespace Lima::Common;
28using namespace std;
29
30namespace Lima
31{
32namespace LinguisticProcessing
33{
34namespace MorphologicAnalysis
35{
36
37#define NAMEINDEXRESSOURCE_CLASSID "NameIndex"
38
40/*
41NameIndexResource::NameIndexResource(QObject* parent)
42 : AbstractResource(parent),m_fsaAccess(0)
43*/
45 : AbstractResource(0), m_withIndex(false)
46{
47/*
48 connect(this,SIGNAL(resourceFileChanged(QString)),this,SLOT(accessFileChanged(QString)));
49*/
50}
51
55
56void NameIndexResource::readNames( const std::string& filepath ) {
58 std::ifstream names(filepath.data());
59 char strbuff[200];
60
61 for( int counter = 0 ; ; counter++ ) {
62 // lecture d'une ligne du fichier
63 names.getline(strbuff, 200, '\n' );
64 string line(strbuff);
65 if( line.size() == 0 ) {
66 LDEBUG << "end of list of words. counter= " << counter;
67 break;
68 }
69 else {
70 //
72 int i=namels.indexOf('\t');
73 if (i==-1) {
74 insert(std::pair<std::basic_string<wchar_t>,std::basic_string<wchar_t> >(
75 L"",LimaStr2wcharStr( namels )));
76 }
77 else {
78 insert(std::pair<std::basic_string<wchar_t>,std::basic_string<wchar_t> >(
79 LimaStr2wcharStr(namels.mid(i+1)),LimaStr2wcharStr(namels.left(i))));
80 }
81 }
82 }
83}
84
85std::basic_string<wchar_t> NameIndexResource::LimaStr2wcharStr( const QString& limastr ) {
86 // convert QString to std::basic_string<wchar_t>
87 wchar_t warray[limastr.length()+1];
88 int warray_len = limastr.toWCharArray(warray);
89 warray[warray_len]=0;
90 return std::basic_string<wchar_t>(warray, warray_len);
91}
92
93
96 Manager* manager)
97{
104 try
105 {
106 QString filepath = Misc::findFileInPaths(Common::MediaticData::MediaticData::single().getResourcesPath().c_str(),
107 unitConfiguration.getParamsValueAtKey("filename").c_str());
108 struct stat sb;
109 if( stat(filepath.toUtf8().constData(),&sb) == 0)
110 {
111 readNames(filepath.toUtf8().constData());
112 }
113 else {
114 // FIXME: In this case, the m_fsaAccess pointer is still NULL. Try to access to
115 // this ressource will crash the application.
116 LERROR << "resource file" << filepath << "not found";
117 }
118 }
119 catch (NoSuchParam& )
120 {
121 LERROR << "no param 'filename' in NameIndex group for language " << (int) manager->getInitializationParameters().language;
122 throw InvalidConfiguration();
123 }
125 {
126 LERROR << "filename "
128 << "/"
129 << unitConfiguration.getParamsValueAtKey("filename")
130 << " no found for language "
131 << (int) manager->getInitializationParameters().language;
132 throw InvalidConfiguration();
133 }
134 try
135 {
136 std::string withIndex = (unitConfiguration.getParamsValueAtKey("withIndex"));
137 m_withIndex = (withIndex.compare("true") == 0);
138 }
139 catch (NoSuchParam& )
140 {
141 // parameter 'withIndex' is optional, default is false
142 }
143}
144
145/*
146void FsaAccessResource::accessFileChanged ( const QString & path )
147{
148 ANALYSISDICTLOGINIT;
149 // Check if the file exists as, when a file is replaced, accessFileChanged can be triggered
150 // two times, when it is first suppressed and when the new version is available. One should not
151 // try to load the missing file
152 if (QFileInfo::exists(path))
153 {
154 LINFO << "FsaAccessResource::accessFileChanged reload" << path;
155 FsaAccess::FsaAccessSpare16* fsaAccess=new FsaAccess::FsaAccessSpare16();
156 QWriteLocker locker(&m_lock);
157 fsaAccess->read(path.toUtf8().constData());
158 delete m_fsaAccess;
159 m_fsaAccess=fsaAccess;
160 Q_EMIT accessFileReloaded(m_fsaAccess);
161 }
162 else
163 {
164 LINFO << "FsaAccessResource::accessFileChanged deleted, ignoring" << path;
165 }
166}
167*/
168
169} // AnalysisDict
170} // LinguisticProcessing
171} // Lima
#define LDEBUG
Definition LimaCommon.h:157
#define LERROR
Definition LimaCommon.h:161
#define ANALYSISDICTLOGINIT
#define MORPHOLOGINIT
#define NAMEINDEXRESSOURCE_CLASSID
Defines a Factory to create Object of type Base.
Use this exception to signal the used of a wrongly initialized LIMA dictionary.
Definition LimaCommon.h:428
const std::string & getResourcesPath() const
return a message when a 'param' was not found
Use this exception to signal an error in one of the configuration files.
Definition LimaCommon.h:345
virtual void init(Common::XMLConfigurationFiles::GroupConfigurationStructure &unitConfiguration, Manager *manager) override
initialize with parameters from configuration file.
static std::basic_string< wchar_t > LimaStr2wcharStr(const QString &limastr)
static const MediaticData & single()
const singleton accessor
Definition Singleton.h:51
QString findFileInPaths(const QString &paths, const QString &fileName, const QChar &separator)
Find the given file in the given paths.
LimaString utf8stdstring2limastring(const std::string &src)
SimpleFactory< AbstractResource, NameIndexResource > fsaAccessResourceFactory(NAMEINDEXRESSOURCE_CLASSID)
NAUTITIA.
QString LimaString
Definition LimaString.h:33
STL namespace.
launch exception related to the configuration file parsing