Skip to content

Commit 0692a4f

Browse files
committed
tags clean
0 parents  commit 0692a4f

27 files changed

+14891
-0
lines changed

.gitignore

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
HELP.md
2+
target/
3+
!.mvn/wrapper/maven-wrapper.jar
4+
!**/src/main/**
5+
!**/src/test/**
6+
7+
### STS ###
8+
.apt_generated
9+
.classpath
10+
.factorypath
11+
.project
12+
.settings
13+
.springBeans
14+
.sts4-cache
15+
16+
### IntelliJ IDEA ###
17+
.idea
18+
*.iws
19+
*.iml
20+
*.ipr
21+
22+
### NetBeans ###
23+
/nbproject/private/
24+
/nbbuild/
25+
/dist/
26+
/nbdist/
27+
/.nb-gradle/
28+
build/
29+
30+
### VS Code ###
31+
.vscode/
32+
.DS_Store
33+
src/main/resources/ml-25m

README.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
# Recommended-and-datacleaning-Algorithm

pom.xml

Lines changed: 82 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,82 @@
1+
<?xml version="1.0" encoding="UTF-8"?>
2+
<project xmlns="http://maven.apache.org/POM/4.0.0"
3+
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
4+
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
5+
<modelVersion>4.0.0</modelVersion>
6+
<build>
7+
<plugins>
8+
<plugin>
9+
<groupId>org.apache.maven.plugins</groupId>
10+
<artifactId>maven-compiler-plugin</artifactId>
11+
<configuration>
12+
<source>8</source>
13+
<target>8</target>
14+
</configuration>
15+
</plugin>
16+
</plugins>
17+
</build>
18+
<dependencies>
19+
<!-- https://mvnrepository.com/artifact/org.apache.spark/spark-core -->
20+
21+
<!-- https://mvnrepository.com/artifact/org.apache.spark/spark-mllib -->
22+
<dependency>
23+
<groupId>com.belerweb</groupId>
24+
<artifactId>pinyin4j</artifactId>
25+
<version>2.5.0</version>
26+
</dependency>
27+
<dependency>
28+
<groupId>com.janeluo</groupId>
29+
<artifactId>ikanalyzer</artifactId>
30+
<version>2012_u6</version>
31+
</dependency>
32+
<dependency>
33+
<groupId>org.jsoup</groupId>
34+
<artifactId>jsoup</artifactId>
35+
<version>1.11.2</version>
36+
</dependency>
37+
<!-- https://mvnrepository.com/artifact/mysql/mysql-connector-java -->
38+
<dependency>
39+
<groupId>mysql</groupId>
40+
<artifactId>mysql-connector-java</artifactId>
41+
<version>8.0.20</version>
42+
</dependency>
43+
44+
<dependency>
45+
<groupId>org.apache.mahout</groupId>
46+
<artifactId>mahout</artifactId>
47+
<version>0.11.1</version>
48+
</dependency>
49+
<dependency>
50+
<groupId>org.apache.mahout</groupId>
51+
<artifactId>mahout-examples</artifactId>
52+
<version>0.13.0</version>
53+
</dependency>
54+
<dependency>
55+
<groupId>com.opencsv</groupId>
56+
<artifactId>opencsv</artifactId>
57+
<version>4.4</version>
58+
</dependency>
59+
<!-- https://mvnrepository.com/artifact/net.sourceforge.javacsv/javacsv -->
60+
<dependency>
61+
<groupId>net.sourceforge.javacsv</groupId>
62+
<artifactId>javacsv</artifactId>
63+
<version>2.0</version>
64+
</dependency>
65+
<dependency>
66+
<groupId>com.alibaba</groupId>
67+
<artifactId>fastjson</artifactId>
68+
<version>1.2.5</version>
69+
</dependency>
70+
<!-- 分词器 (可以替换为其他中文分词器)-->
71+
<dependency>
72+
<groupId>com.hankcs</groupId>
73+
<artifactId>hanlp</artifactId>
74+
<version>portable-1.3.4</version>
75+
</dependency>
76+
</dependencies>
77+
<groupId>DataMining</groupId>
78+
<artifactId>RecommendedAlgorithm</artifactId>
79+
<version>1.0-SNAPSHOT</version>
80+
81+
82+
</project>

src/main/java/Abstract/BM25.java

Lines changed: 157 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,157 @@
1+
package Abstract;
2+
3+
/**
4+
* 搜索相关性评分算法
5+
* 评价搜索词和文档之间相关性的算法
6+
* 它是一种基于概率检索模型提出的算法
7+
* https://www.jianshu.com/p/b4f06594d32f
8+
*
9+
* 单词和D之间的相关性
10+
* 单词和query之间的相关性
11+
* 每个单词的权重
12+
* 最后对于每个单词的分数我们做一个求和,就得到了query和文档之间的分数。
13+
*/
14+
15+
import java.util.List;
16+
import java.util.Map;
17+
import java.util.TreeMap;
18+
19+
public class BM25 {
20+
/**
21+
* 文档句子的个数
22+
*/
23+
int D;
24+
25+
/**
26+
* 文档句子的平均长度
27+
*/
28+
double avgdl;
29+
30+
/**
31+
* 拆分为[句子[单词]]形式的文档
32+
*/
33+
List<List<String>> docs;
34+
35+
/**
36+
* 文档中每个句子中的每个词与词频
37+
*/
38+
Map<String, Integer>[] f;
39+
40+
/**
41+
* 文档中全部词语与出现在几个句子中
42+
*/
43+
Map<String, Integer> df;
44+
45+
/**
46+
* IDF
47+
*/
48+
Map<String, Double> idf;
49+
50+
/**
51+
* 调节因子
52+
*/
53+
final static float k1 = 1.5f;
54+
55+
/**
56+
* 调节因子
57+
*/
58+
final static float b = 0.75f;
59+
public BM25(List<List<String>> docs)
60+
{
61+
this.docs = docs;
62+
D = docs.size();
63+
//计算文档中句子的平均长度 总词数/句子总数
64+
for (List<String> sentence : docs)
65+
{
66+
avgdl += sentence.size();
67+
}
68+
avgdl /= D;
69+
f = new Map[D];
70+
df = new TreeMap<String, Integer>();
71+
idf = new TreeMap<String, Double>();
72+
init();
73+
}
74+
75+
/**
76+
* 在构造时初始化自己的所有参数
77+
*
78+
*/
79+
private void init()
80+
{
81+
int index = 0;//index表示现在是文档中的第几句话
82+
83+
for (List<String> sentence : docs)
84+
{
85+
//对于每个句子的分词结果 计算 分词在这个句子中出现的频率 为tf 存成String int的映射形式
86+
Map<String, Integer> tf = new TreeMap<String, Integer>();
87+
for (String word : sentence)
88+
89+
{
90+
//计算每个值出现的频数 作为tf
91+
Integer freq = tf.get(word);
92+
freq = (freq == null ? 0 : freq) + 1;
93+
tf.put(word, freq);
94+
}
95+
f[index] = tf;//存储每句话对应的tf值Map
96+
97+
//根据tf值算df值 计算每个词出现在几个句子中
98+
for (Map.Entry<String, Integer> entry : tf.entrySet())
99+
{
100+
101+
String word = entry.getKey();
102+
Integer freq = df.get(word);
103+
freq = (freq == null ? 0 : freq) + 1;
104+
df.put(word, freq);
105+
}
106+
++index;
107+
}
108+
//根据df计算idf 公司为log(D - freq + 0.5) - Math.log(freq + 0.5) D为文档中句子个数 0.5为平滑项
109+
for (Map.Entry<String, Integer> entry : df.entrySet())
110+
{
111+
//计算逆文档频率 idf
112+
String word = entry.getKey();
113+
Integer freq = entry.getValue();
114+
idf.put(word, Math.log(D - freq + 0.5) - Math.log(freq + 0.5));
115+
}
116+
}
117+
118+
/**
119+
* 计算相似度 最终得到一个句子 与对应index句子的相关性得分
120+
* @param sentence
121+
* @param index
122+
* @return
123+
*/
124+
public double sim(List<String> sentence, int index)
125+
{
126+
double score = 0;
127+
//对于一句话中的每一个单词 计算这个单词 与其他句子的相关性得分 这个得分用BM25计算出
128+
for (String word : sentence)
129+
{
130+
if (!f[index].containsKey(word)) continue;
131+
int d = docs.get(index).size();//index对应句子的词的个数
132+
Integer wf = f[index].get(word);//在index对应句子中 词word出现的次数
133+
//,参数b的作用是调整文档长度对相关性影响的大小。b越大,文档长度的对相关性得分的影响越大,反之越小。而文档的相对长度越长,K值将越大,则相关性得
134+
//分会越小。这可以理解为,当文档较长时,包含qi的机会越大,因此,同等fi的情况下,长文档与qi的相关性应该比短文档与qi的相关性弱。
135+
score += (idf.get(word) * wf * (k1 + 1)
136+
/ (wf + k1 * (1 - b + b * d
137+
/ avgdl)));
138+
}
139+
//最终得到一个句子 与对应index句子的相关性得分
140+
return score;
141+
}
142+
143+
/**
144+
* 计算整体的相似度 计算每一个句子与其他所有句子的相似度
145+
* @param sentence
146+
* @return
147+
*/
148+
public double[] simAll(List<String> sentence)
149+
{
150+
double[] scores = new double[D];
151+
for (int i = 0; i < D; ++i)
152+
{
153+
scores[i] = sim(sentence, i);
154+
}
155+
return scores;
156+
}
157+
}
Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
package Abstract;
2+
3+
import com.hankcs.hanlp.HanLP;
4+
5+
import java.util.List;
6+
7+
public class ExtractAbstract {
8+
public static void main(String [] args)
9+
{
10+
String document = "四海网讯网讯,近日,有媒体报道称:章子怡真怀孕了!报道还援引知情人士消息称," +
11+
"“章子怡怀孕大概四五个月,预产期是年底前后,现在已经不接工作了。”这到底是怎么回事?消息是真是假?针对此消息," +
12+
"23日晚8时30分,华西都市报记者迅速联系上了与章子怡家里关系极好的知情人士,这位人士向华西都市报记者证实说:“子怡这次确实怀孕了。" +
13+
"她已经36岁了,也该怀孕了。章子怡怀上汪峰的孩子后,子怡的父母亲十分高兴。子怡的母亲,已开始悉心照料女儿了。子怡的预产期大概是今年12月底。" +
14+
"”当晚9时,华西都市报记者为了求证章子怡怀孕消息,又电话联系章子怡的亲哥哥章子男,但电话通了,一直没有人接听。有关章子怡怀孕的新闻自从2013年9月份章子怡和汪峰恋情以来," +
15+
"就被传N遍了!不过,时间跨入2015年,事情却发生着微妙的变化。2015年3月21日,章子怡担任制片人的电影《从天儿降》开机,在开机发布会上几张合影,让网友又燃起了好奇心:" +
16+
"“章子怡真的怀孕了吗?”但后据证实,章子怡的“大肚照”只是影片宣传的噱头。过了四个月的7月22日,《太平轮》新一轮宣传,章子怡又被发现状态不佳,不时深呼吸,不自觉想捂住肚子" +
17+
",又觉得不妥。然后在8月的一天,章子怡和朋友吃饭,在酒店门口被风行工作室拍到了,疑似有孕在身!今年7月11日,汪峰本来在上海要举行演唱会,后来因为台风“灿鸿”取消了。" +
18+
"而消息人士称,汪峰原来打算在演唱会上当着章子怡的面宣布重大消息," +
19+
"而且章子怡已经赴上海准备参加演唱会了,怎知遇到台风,只好延期,相信9月26日的演唱会应该还会有惊喜大白天下吧。";
20+
List<String> sentenceList = HanLP.extractSummary(document, 3);
21+
System.out.println(sentenceList);
22+
}
23+
}

0 commit comments

Comments
 (0)