Mahout是否提供了一种确定内容之间相似性的方法(基于内容的推荐)?

use*_*072 1 recommendation-engine mahout content-based-retrieval mahout-recommender

Mahout是否提供了确定内容之间相似性的方法?

我想将基于内容的推荐作为Web应用程序的一部分.我知道Mahout善于利用用户评级矩阵并根据它们提出建议,但我对协作(基于评级)的建议不感兴趣.我想评分两段文本的匹配程度,然后推荐与我在用户个人资料中为用户存储的文本最匹配的项目...

我已经阅读了Mahout的文档,看起来它主要促进了协作(基于评级)的建议,但不是基于内容的建议......这是真的吗?

小智 10

这并非完全正确.Mahout没有基于内容的推荐器,但它确实有基于内容计算项目之间相似性的算法.其中最受欢迎的是TF-IDF和余弦相似性.但是,计算不是动态的,而是离线完成的.你需要hadoop来更快地根据内容计算成对相似性.我要写的步骤是MAHOUT 0.8.我不确定他们是否在0.9中改变了它.

步骤1.您需要将文本文档转换为seq文件.我在MAHOUT-0.8中失去了这个命令,但在0.9中是这样的(请检查你的MAHOUT版本):

$MAHOUT_HOME/bin/mahout seqdirectory
--input <PARENT DIR WHERE DOCS ARE LOCATED> --output <OUTPUT DIRECTORY>
<-c <CHARSET NAME OF THE INPUT DOCUMENTS> {UTF-8|cp1252|ascii...}>
<-chunk <MAX SIZE OF EACH CHUNK in Megabytes> 64>
<-prefix <PREFIX TO ADD TO THE DOCUMENT ID>>
Run Code Online (Sandbox Code Playgroud)

第2步.您需要将序列文件转换为稀疏向量,如下所示:

$MAHOUT_HOME/bin/mahout seq2sparse \
   -i <SEQ INPUT DIR> \
   -o <VECTORS OUTPUT DIR> \
   -ow -chunk 100 \
   -wt tfidf \
   -x 90 \
   -seq \
   -ml 50 \
   -md 3 \
   -n 2 \
   -nv \
   -Dmapred.map.tasks=1000 -Dmapred.reduce.tasks=1000
Run Code Online (Sandbox Code Playgroud)

哪里:

  • chunk是文件的大小.
  • x应该将术语的最大数量视为字典文件的一部分.如果它小于-x,则将其视为停用词.
  • wt是加权方案.
  • md该术语应被视为字典文件一部分的最小文档数.任何频率较低的术语都会被忽略.
  • n在Lp空间中使用的规范化值.标准化的详细说明见8.4节.默认方案是不对权重进行标准化.2对于余弦距离是有利的,我们在聚类和相似性中使用它
  • nv获取命名向量使更多数据文件更容易检查.

步骤3.从向量创建矩阵:

$MAHOUT_HOME/bin/mahout rowid -i <VECTORS OUTPUT DIR>/tfidf-vectors/part-r-00000 -o <MATRIX OUTPUT DIR>
Run Code Online (Sandbox Code Playgroud)

步骤4.为上面矩阵的每一行创建一个类似文档的集合.这将为集合中的每个文档生成50个最相似的文档.

 $MAHOUT_HOME/bin/mahout rowsimilarity -i <MATRIX OUTPUT DIR>/matrix -o <SIMILARITY OUTPUT DIR> -r <NUM OF COLUMNS FROM THE OUTPUT IN STEP 3> --similarityClassname SIMILARITY_COSINE -m 50 -ess -Dmapred.map.tasks=1000 -Dmapred.reduce.tasks=1000
Run Code Online (Sandbox Code Playgroud)

这将生成一个文件,其中每个项目与基于内容的前50个文件具有相似性.

现在,要在推荐过程中使用它,您需要读取文件或将其加载到数据库中,具体取决于您拥有多少资源.我使用加载到主内存中Collection<GenericItemSimilarity.ItemItemSimilarity>.以下是两个为我完成工作的简单函数:

public static Collection<GenericItemSimilarity.ItemItemSimilarity> correlationMatrix(final File folder, TIntLongHashMap docIndex) throws IOException{
        Collection<GenericItemSimilarity.ItemItemSimilarity> corrMatrix = 
                new ArrayList<GenericItemSimilarity.ItemItemSimilarity>();

        ItemItemSimilarity itemItemCorrelation = null;

        Configuration conf = new Configuration();
        FileSystem fs = FileSystem.get(conf);

        int n=0;
        for (final File fileEntry : folder.listFiles()) {
            if (fileEntry.isFile()) {
                if(fileEntry.getName().startsWith("part-r")){

                    SequenceFile.Reader reader = new SequenceFile.Reader(fs, new Path(fileEntry.getAbsolutePath()), conf);

                    IntWritable key = new IntWritable();
                    VectorWritable value = new VectorWritable();
                    while (reader.next(key, value)) {

                        long itemID1 = docIndex.get(Integer.parseInt(key.toString()));

                        Iterator<Element> it = value.get().nonZeroes().iterator();

                        while(it.hasNext()){
                            Element next = it.next();
                            long itemID2 =  docIndex.get(next.index());
                            double similarity =  next.get();
                            //System.out.println(itemID1+ " : "+itemID2+" : "+similarity);

                            if (similarity < -1.0) {
                                similarity = -1.0;
                            } else if (similarity > 1.0) {
                                similarity = 1.0;
                            }


                            itemItemCorrelation = new GenericItemSimilarity.ItemItemSimilarity(itemID1, itemID2, similarity);

                            corrMatrix.add(itemItemCorrelation);
                        }
                    }
                    reader.close();
                    n++;
                    logger.info("File "+fileEntry.getName()+" readed ("+n+"/"+folder.listFiles().length+")");
                }
            }
        }

        return corrMatrix;
    }


public static TIntLongHashMap getDocIndex(String docIndex) throws IOException{
        Configuration conf = new Configuration();
        FileSystem fs = FileSystem.get(conf);

        TIntLongHashMap map = new TIntLongHashMap();
        SequenceFile.Reader docIndexReader = new SequenceFile.Reader(fs, new Path(docIndex), conf);

        IntWritable key = new IntWritable();
        Text value = new Text();
        while (docIndexReader.next(key, value)) {
            map.put(key.get(), Long.parseLong(value.toString()));
        }

        return map;
    }
Run Code Online (Sandbox Code Playgroud)

最后,在您的推荐课程中,您将其称为:

TIntLongHashMap docIndex = ItemPairwiseSimilarityUtil.getDocIndex(filename);
TLongObjectHashMap<TLongDoubleHashMap> correlationMatrix = ItemPairwiseSimilarityUtil.correlatedItems(folder, docIndex);
Run Code Online (Sandbox Code Playgroud)

filename您的docIndex文件名在哪里,并且folder是项目相似性文件的文件夹.最后,这只不过是基于项目的推荐.

希望这可以帮到你