• 如果您觉得本站非常有看点,那么赶紧使用Ctrl+D 收藏吧

Java Cosine类的典型用法和代码示例

java 2次浏览

本文整理汇总了Java中info.debatty.java.stringsimilarity.Cosine的典型用法代码示例。如果您正苦于以下问题:Java Cosine类的具体用法?Java Cosine怎么用?Java Cosine使用的例子?那么恭喜您, 这里精选的类代码示例或许可以为您提供帮助。

Cosine类属于info.debatty.java.stringsimilarity包,在下文中一共展示了Cosine类的5个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于我们的系统推荐出更棒的Java代码示例。

示例1: getTeamByNameKeywords

点赞 3

import info.debatty.java.stringsimilarity.Cosine; //导入依赖的package包/类
/**
 * @param teamNameKeywords
 * @return A team found using keywords of its name
 * @throws TeamNotFoundException
 */
public static Team getTeamByNameKeywords(String teamNameKeywords) throws TeamNotFoundException {
	try {
		String teamId = null;
		Double bestSimilarity = 1.0; // better if closer to 0
		Cosine  algorithm = new Cosine();
		HttpResponse leagueTeamsResponse = QueryManager.getHttpResponse(sourceBaseUrl + "/" + currentSeason + "/teams.json");
		TeamSummaryResponse teamSummaries = objectMapper.readValue(leagueTeamsResponse.getEntity().getContent(), TeamSummaryResponse.class);
		
		for (TeamSummary teamSummary : teamSummaries.getLeague().getStandard()) {
			String fullName = teamSummary.getFullName();
			Double currentSimilarity = algorithm.distance(fullName.toLowerCase(), teamNameKeywords.toLowerCase());
			if (currentSimilarity < bestSimilarity) {
				bestSimilarity = currentSimilarity;
				teamId = teamSummary.getTeamId();
			}
		}
		
		if (teamId == null) {
			throw new TeamNotFoundException("Could not find a team using keywords " + "'" + teamNameKeywords + "'");
		}
		return new Team(teamId);
	} catch (Exception e) {
		throw new TeamNotFoundException("Could not find a team using keywords " + "'" + teamNameKeywords + "'");
	}
}
 

开发者ID:antoineguay1,
项目名称:nbawrapper-stats,
代码行数:31,
代码来源:Team.java

示例2: main

点赞 3

import info.debatty.java.stringsimilarity.Cosine; //导入依赖的package包/类
/**
 * @param args the command line arguments
 */
public static void main(String[] args) {
    String s1 = "My first string";
    String s2 = "My other string...";

    // Let's work with sequences of 2 characters...
    Cosine cosine = new Cosine(2);

    // Pre-compute the profile of strings
    Map<String, Integer> profile1 = cosine.getProfile(s1);
    Map<String, Integer> profile2 = cosine.getProfile(s2);

    // Prints 0.516185
    System.out.println(cosine.similarity(profile1, profile2));

}
 

开发者ID:tdebatty,
项目名称:java-string-similarity,
代码行数:19,
代码来源:PrecomputedCosine.java

示例3: main

点赞 3

import info.debatty.java.stringsimilarity.Cosine; //导入依赖的package包/类
/**
 * @param args the command line arguments
 */
public static void main(String[] args) throws IOException {
    // Read the file
    List<String> nodes = GraphBuilder.readFile(
            NNCTPHExample.class.getClassLoader()
                    .getResource("726-unique-spams").getFile());

    NNCTPH builder = new NNCTPH();
    builder.setNPartitions(20);
    builder.setOversampling(2);
    builder.setSimilarity(new SimilarityInterface<String>() {

        public double similarity(String value1, String value2) {
            Cosine cosine = new Cosine(3);
            return cosine.similarity(value1, value2);
        }
    });

    Graph<String> graph = builder.computeGraph(nodes);

    for (Map.Entry<String, NeighborList> entry : graph.entrySet()) {
        System.out.println(entry);
    }

    builder.test(nodes);
}
 

开发者ID:tdebatty,
项目名称:java-graphs,
代码行数:29,
代码来源:NNCTPHExample.java

示例4: getPlayerByNameKeywords

点赞 2

import info.debatty.java.stringsimilarity.Cosine; //导入依赖的package包/类
/**
 * @param playerNameKeywords
 * @return A player found using keywords about his name
 * @throws PlayerNotFoundException
 */
public static Player getPlayerByNameKeywords(String playerNameKeywords) throws PlayerNotFoundException {
	try {
		String personId = null;
		LeaguePlayer leaguePlayer = null;
		Double bestSimilarity = 1.0; // better if closer to 0
		Cosine  algorithm = new Cosine();
		HttpResponse leaguePlayersResponse = QueryManager.getHttpResponse(sourceBaseUrl + "/" + currentSeason + "/players.json");
		LeaguePlayersResponse leaguePlayers = objectMapper.readValue(leaguePlayersResponse.getEntity().getContent(), LeaguePlayersResponse.class);
		
		for (LeaguePlayer standard : leaguePlayers.getLeague().getStandard()) {
			String fullName = standard.getFirstName() + " " + standard.getLastName();
			Double currentSimilarity = algorithm.distance(fullName.toLowerCase(), playerNameKeywords.toLowerCase());
			if (currentSimilarity < bestSimilarity) {
				bestSimilarity = currentSimilarity;
				personId = standard.getPersonId();
				leaguePlayer = standard;
			}
		}
		
		if (personId == null) {
			throw new PlayerNotFoundException("Could not find a player using keywords " + "'" + playerNameKeywords + "'");
		}
		return new Player(personId, leaguePlayer);
	} catch (Exception e) {
		throw new PlayerNotFoundException("Could not find a player using keywords " + "'" + playerNameKeywords + "'");
	}
}
 

开发者ID:antoineguay1,
项目名称:nbawrapper-stats,
代码行数:33,
代码来源:Player.java

示例5: validate

点赞 2

import info.debatty.java.stringsimilarity.Cosine; //导入依赖的package包/类
/**
 * Checks the congruence of an answer with the right answer of a given question.
 * Compares:
 * - Normalized Levenshtein similarity
 * - Jaro-Winkler similarity
 * - Cosine similarity
 * - Jaccard similarity
 * - Sorensen-Dice similarity
 *
 * @param question The question with the right answer in it
 * @param userAnswer The answer to check
 * @return A double value where 0 <= x <= 1.
 * Zero should be returned with no congruence. One should be returned if the answer {@code equals()} the right answer.
 */
public Pair<String, Double> validate(Question question, String userAnswer) {
    Log.d(MainActivity.TAG, "User answer: " + userAnswer);

    Pair<String, Double> maxSimilarity = new Pair<>(null, 0.0);

    for(String answer : question.getAnswers()){
        Log.d(MainActivity.TAG, "Answer: " + answer);

        NormalizedLevenshtein normalizedLevenshtein = new NormalizedLevenshtein();
        JaroWinkler jaroWinkler = new JaroWinkler();
        Cosine cosine = new Cosine();
        Jaccard jaccard = new Jaccard();
        SorensenDice sorensenDice = new SorensenDice();

        double similarity = (
                normalizedLevenshtein.similarity(userAnswer, answer) * WEIGHT_NORMALIZED_LEVENSHTEIN
                        + jaroWinkler.similarity(userAnswer, answer) * WEIGHT_JARO_WINKLER
                        + cosine.similarity(userAnswer, answer) * WEIGHT_COSINE
                        + jaccard.similarity(userAnswer, answer) * WEIGHT_JACCARD
                        + sorensenDice.similarity(userAnswer, answer) * WEIGHT_SORENSEN_DICE
        ) / (
                WEIGHT_NORMALIZED_LEVENSHTEIN
                        + WEIGHT_JARO_WINKLER
                        + WEIGHT_COSINE
                        + WEIGHT_JACCARD
                        + WEIGHT_SORENSEN_DICE
        );

        Log.d(MainActivity.TAG, "Similarity: \"" + answer + "\"->" + similarity);

        if (similarity > maxSimilarity.second) {
            maxSimilarity = new Pair<>(answer, similarity);
        }
    }

    Log.d(MainActivity.TAG, "Maximal similarity: \"" + maxSimilarity.first + "\"->" + maxSimilarity.second);

    return maxSimilarity;
}
 

开发者ID:Wilhelm-Gymnasium,
项目名称:Quiz,
代码行数:54,
代码来源:Validator.java


版权声明:本文转自网络文章,转载此文章仅为分享知识,如有侵权,请联系管理员进行删除。
喜欢 (0)