使用人类语言词典动态填充 hashmap 以进行文本分析
dynamically populate hashmap with human language dictionary for text analysis
我正在编写一个软件项目,将人类语言的文本作为输入并确定它是用什么语言编写的。
我的想法是,我将把字典存储在 hashmaps 中,以 word 作为键,以 bool 作为值。
如果文档有那个词,我会将 bool 翻转为 ture。
现在我正在想一个好的方法来阅读这些字典,把它们放到hashmaps中,我现在的做法很幼稚,看起来很笨重,有没有更好的方法填充这些哈希图?
而且,这些词典很大。也许这不是最好的方法,即像这样连续填充它们。
我在想一次只考虑一个字典可能会更好,然后创建一个分数,输入文本中有多少个单词在该文档中注册,保存它,然后处理下一个字典。那会节省 RAM,不是吗?这是一个好的解决方案吗?
到目前为止的代码如下所示:
static HashMap<String, Boolean> de_map = new HashMap<String, Boolean>();
static HashMap<String, Boolean> fr_map = new HashMap<String, Boolean>();
static HashMap<String, Boolean> ru_map = new HashMap<String, Boolean>();
static HashMap<String, Boolean> eng_map = new HashMap<String, Boolean>();
public static void main(String[] args) throws IOException
{
ArrayList<File> sub_dirs = new ArrayList<File>();
final String filePath = "/home/matthias/Desktop/language_detective/word_lists_2";
listf( filePath, sub_dirs );
for(File dir : sub_dirs)
{
String word_holding_directory_path = dir.toString().toLowerCase();
BufferedReader br = new BufferedReader(new FileReader( dir ));
String line = null;
while ((line = br.readLine()) != null)
{
//System.out.println(line);
if(word_holding_directory_path.toLowerCase().contains("/de/") )
{
de_map.put(line, false);
}
if(word_holding_directory_path.toLowerCase().contains("/ru/") )
{
ru_map.put(line, false);
}
if(word_holding_directory_path.toLowerCase().contains("/fr/") )
{
fr_map.put(line, false);
}
if(word_holding_directory_path.toLowerCase().contains("/eng/") )
{
eng_map.put(line, false);
}
}
}
因此,我正在寻求有关如何一次填充它们的建议,以及关于这是否是一种好的方法的意见,或者关于实现这一目标的可能更好的方法的建议。
可以找到完整的程序 here on my GitHub page。
27th
语言识别任务研究得很好,有很多好的库。
Java,试试TIKA, or Language Detection Library for Java (they report "99% over precision for 53 languages"), or TextCat, or LingPipe - 我建议从第一个开始,好像有最详细的教程。
如果您的任务对于现有库而言过于具体(尽管我怀疑是这种情况),请参考此survey paper并采用最接近的技术。
如果你确实想重新发明轮子,例如为了自学,注意识别可以作为文本分类的一个特例,阅读这个基础tutorial for text classification.
我正在编写一个软件项目,将人类语言的文本作为输入并确定它是用什么语言编写的。
我的想法是,我将把字典存储在 hashmaps 中,以 word 作为键,以 bool 作为值。
如果文档有那个词,我会将 bool 翻转为 ture。
现在我正在想一个好的方法来阅读这些字典,把它们放到hashmaps中,我现在的做法很幼稚,看起来很笨重,有没有更好的方法填充这些哈希图?
而且,这些词典很大。也许这不是最好的方法,即像这样连续填充它们。
我在想一次只考虑一个字典可能会更好,然后创建一个分数,输入文本中有多少个单词在该文档中注册,保存它,然后处理下一个字典。那会节省 RAM,不是吗?这是一个好的解决方案吗?
到目前为止的代码如下所示:
static HashMap<String, Boolean> de_map = new HashMap<String, Boolean>();
static HashMap<String, Boolean> fr_map = new HashMap<String, Boolean>();
static HashMap<String, Boolean> ru_map = new HashMap<String, Boolean>();
static HashMap<String, Boolean> eng_map = new HashMap<String, Boolean>();
public static void main(String[] args) throws IOException
{
ArrayList<File> sub_dirs = new ArrayList<File>();
final String filePath = "/home/matthias/Desktop/language_detective/word_lists_2";
listf( filePath, sub_dirs );
for(File dir : sub_dirs)
{
String word_holding_directory_path = dir.toString().toLowerCase();
BufferedReader br = new BufferedReader(new FileReader( dir ));
String line = null;
while ((line = br.readLine()) != null)
{
//System.out.println(line);
if(word_holding_directory_path.toLowerCase().contains("/de/") )
{
de_map.put(line, false);
}
if(word_holding_directory_path.toLowerCase().contains("/ru/") )
{
ru_map.put(line, false);
}
if(word_holding_directory_path.toLowerCase().contains("/fr/") )
{
fr_map.put(line, false);
}
if(word_holding_directory_path.toLowerCase().contains("/eng/") )
{
eng_map.put(line, false);
}
}
}
因此,我正在寻求有关如何一次填充它们的建议,以及关于这是否是一种好的方法的意见,或者关于实现这一目标的可能更好的方法的建议。
可以找到完整的程序 here on my GitHub page。
27th
语言识别任务研究得很好,有很多好的库。 Java,试试TIKA, or Language Detection Library for Java (they report "99% over precision for 53 languages"), or TextCat, or LingPipe - 我建议从第一个开始,好像有最详细的教程。
如果您的任务对于现有库而言过于具体(尽管我怀疑是这种情况),请参考此survey paper并采用最接近的技术。
如果你确实想重新发明轮子,例如为了自学,注意识别可以作为文本分类的一个特例,阅读这个基础tutorial for text classification.