日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 > 编程资源 > 编程问答 >内容正文

编程问答

hashmap实现倒排索引——查询多个单词出现在多个句子中

發布時間:2024/9/30 编程问答 31 豆豆
生活随笔 收集整理的這篇文章主要介紹了 hashmap实现倒排索引——查询多个单词出现在多个句子中 小編覺得挺不錯的,現在分享給大家,幫大家做個參考.

1.問題描述

給定三條句子:

d1=I like to watch the sun set with my friend.

d2=The Best Places to Watch The Sunset.

d3=My friend watches the sun come up.

輸入兩個單詞,輸出它的在哪些句子中出現過

?

2.利用hashmap<String,List<Integer>>來實現

import java.util.ArrayList; import java.util.HashMap; import java.util.List; import java.util.Scanner;public class Main {public static HashMap<String, List> map=new HashMap<>();public static void way(String str,int word) {String str2="\\.";str=str.replaceAll("\\.", " .");String strs[]=str.split(" ");for (String string : strs) {string=string.toLowerCase();List<Integer> key=map.get(string);if (key==null) {key=new ArrayList<>();map.put(string, key);}if (!key.contains(string)) {key.add(word);}}}public static List<Integer> Intersect(List<Integer> p1,List<Integer> p2){int res=0;List<Integer> list=new ArrayList<>();int i=0;while (!p1.isEmpty()&&!p2.isEmpty()) {if (p1.get(i).equals(p2.get(i))) {list.add(p1.get(i));p1.remove(i);p2.remove(i);}else if(p1.get(i)<p2.get(i)){p1.remove(i);}else {p2.remove(i);}} return list;}public static void main(String[] args) {// TODO Auto-generated method stubScanner sc=new Scanner(System.in);String strs[]= {"I like to watch the sun set with my friend.","The Best Places to Watch The Sunset.","My friend watches the sun come up."};System.out.println("請輸入多關鍵字:");String word=sc.next().toLowerCase();String word2=sc.next().toLowerCase();way(strs[0], 1);way(strs[1], 2);way(strs[2], 3);//System.out.println(map);//System.out.println(map.get(word));//System.out.println(map.get(word2));List<Integer> list=Intersect(map.get(word),map.get(word2));System.out.println(list);} }

3.實驗結果

總結

以上是生活随笔為你收集整理的hashmap实现倒排索引——查询多个单词出现在多个句子中的全部內容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。