java HashMap 'cannot be resolved'。可以执行方法但无法打印

标签 java hash printing hashmap output

我有一个程序,其中以元组形式作为键(表示从输入文件读取的句子)的 HashMap 和整数(在数据中观察到的次数)是能够填充数据,但无法响应我打印其内容的尝试。它填充在下面代码中的“for”循环内部,并且该代码片段的底部是它要打印的位置。

public static void main(String[] args) throws IOException 
{
    Ontology ontology = new Ontology();
    BufferedReader br = new BufferedReader(new FileReader("/home/matthias/Workbench/SUTD/2_January/learning_first-order_horn_clauses_from_web_text/reverb/code/input_data/stackoverflow_test.txt"));
    Pattern p = Pattern.compile("'(.*?)'\\('(.*?)',\\s*'(.*?)'\\)\\.");
    String line;
    while ((line = br.readLine()) != null) 
    {
        Matcher m = p.matcher(line);
        if( m.matches() ) 
        {
            String verb    = m.group(1);
            String object  = m.group(2);
            String subject = m.group(3);
            ontology.addSentence( new Sentence( verb, object, subject ) );
        }
    }

    for( String joint: ontology.getJoints() )
    {
        for( Integer subind: ontology.getSubjectIndices( joint ) )
        {
            Sentence xaS = ontology.getSentence( subind );

            for( Integer obind: ontology.getObjectIndices( joint ) )
            {

                Sentence yOb = ontology.getSentence( obind );

                Sentence s = new Sentence( xaS.getVerb(),
                                           xaS.getObject(),
                                           yOb.getSubject() );

                //System.out.println( s );                
                ontology.numberRules( s ); 

            }
        }
    }
    for (Map.Entry<Sentence, Integer> entry : ontology.numberRules.entrySet()) 
    {
        System.out.println(entry.getKey()+" : "+entry.getValue());
    }     
}

以下文件的底部是实现 HashMap 的位置。这还获取输入句子并搜索句子主语和宾语中的重叠值。系统试图做的是通过输入数据推断来学习“规则”,即 contains(vitamin c, Oranges), prevents(scurvy, Vitamin c) 将产生输出防止(坏血病,橙子),问题是,在我的测试数据中有许多相同的规则,所以我想跟踪观察它们的次数,同时也只存储一个独特“规则”的副本。这就是为什么 HashMap 将句子存储为键,将整数(计数)存储为值。

private List<Sentence> sentences = new ArrayList<>();
/*
 * The following maps store the relation of a string occurring
 * as a subject or object, respectively, to the list of Sentence
 * ordinals where they occur.
 */
private Map<String,List<Integer>> subject2index = new HashMap<>();
private Map<String,List<Integer>> object2index = new HashMap<>();

/*
 * This set contains strings that occur as both,
 * subject and object. This is useful for determining strings
 * acting as an in-between connecting two relations. 
 */
private Set<String> joints = new HashSet<>();

public void addSentence( Sentence s )
{

    // add Sentence to the list of all Sentences
    sentences.add( s );

    // add the Subject of the Sentence to the map mapping strings
    // occurring as a subject to the ordinal of this Sentence
    List<Integer> subind = subject2index.get( s.getSubject() );
    if( subind == null )
    {
        subind = new ArrayList<>();
        subject2index.put( s.getSubject(), subind );
    }
    subind.add( sentences.size() - 1 );

    // add the Object of the Sentence to the map mapping strings
    // occurring as an object to the ordinal of this Sentence
    List<Integer> objind = object2index.get( s.getObject() );
    if( objind == null )
    {
        objind = new ArrayList<>();
        object2index.put( s.getObject(), objind );
    }
    objind.add( sentences.size() - 1 );

    // determine whether we've found a "joining" string
    if( subject2index.containsKey( s.getObject() ) )
    {
        joints.add( s.getObject() );
    }
    if( object2index.containsKey( s.getSubject() ) )
    {
        joints.add( s.getSubject() );
    }
}

public Collection<String> getJoints()
{
    return joints;
}
public List<Integer> getSubjectIndices( String subject )
{
    return subject2index.get( subject );
}
public List<Integer> getObjectIndices( String object )
{
    return object2index.get( object );
}
public Sentence getSentence( int index )
{
    return sentences.get( index );
}

//map to store learned 'rules'
Map<Sentence, Integer> ruleCount = new HashMap<>();

//store data
public void numberRules(Sentence sentence) 
{
    if (!ruleCount.containsKey(sentence))
    {
        ruleCount.put(sentence, 0);
    }
    ruleCount.put(sentence, ruleCount.get(sentence) + 1);
}   

这是存储句子的对象。

public class Sentence 
{
private String verb;
private String object;
private String subject;
public Sentence(String verb, String object, String subject )
{
    this.verb = verb;
    this.object = object;
    this.subject = subject;
}

public String getVerb()
{
    return verb; 
}

public String getObject()
{
    return object; 
}

public String getSubject()
{
    return subject;
}

public String toString()
{
    return verb + "(" + object + ", " + subject + ").";
}

}

输入数据如下所示

'prevents'('scurvy','vitamin C').
'contains'('vitamin C','orange').
'contains'('vitamin C','sauerkraut').
'isa'('fruit','orange').
'improves'('health','fruit').

例如,我希望输出数据可以告诉我

prevents(scurvy, orange).      2
prevents(scurvy, sauerkraut).  4
improves(health, orange).      1

其中句子是 HashMap 的键,整数是关联值,对应于该句子在数据中被观察到的次数。

最佳答案

我没有看到 numberRules您本体类中的成员。

也许您想使用 ruleCount成员,这是 Map<Sentence, Integer> 类型的唯一变量我在你的代码中看到了。

for (Map.Entry<Sentence, Integer> entry : ontology.ruleCount.entrySet()) 
{
    System.out.println(entry.getKey()+" : "+entry.getValue());
}  

关于赫克托的评论,这是一个不同的问题。当您使用自定义类之一作为 HashMap 中的键时( Sentence 在您的情况下是类),您必须覆盖 equalshashCode 。如果你不这样做a.equals(b)仅当 a==b 时才返回 true ,这可能不是您想要的行为。您可能想要a.equals(b)当两个比较句子的动词、宾语和主语分别相等时返回 true。 hashCode应该以 if a.equals(b) 的方式实现是真的,a.hashCode() == b.hashCode() .

关于java HashMap 'cannot be resolved'。可以执行方法但无法打印,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/28134546/

相关文章:

java.lang.ClassNotFoundException : org. apache.struts2.dispatcher.FilterDispatcher

Java Scanner hasNext() 字符串正则表达式验证

javascript - 强制网页打印在法律文件中

Java Swing - 分组组件

java - 如何在 Android 中以编程方式查找当前正在运行的应用程序?

jquery - 使用 URL 哈希和 jQuery 处理单页网站并维护状态

ruby-on-rails - 从数组 ruby​​ rails 中的哈希中检索特定值

javascript - jquery 哈希如果没有哈希

html - 如何让div按列换行?

java - 将二叉树打印到标准输出 Java